From 4c95017e02ac97f538a4b9cc5618033c123bf165 Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Fri, 19 Dec 2025 09:07:35 -0800 Subject: [PATCH] centralize emoji logic --- src/render/custom.zig | 15 ++------------- src/render/emoji.zig | 16 ++++++++++++++++ src/render/line.zig | 26 ++++++-------------------- 3 files changed, 24 insertions(+), 33 deletions(-) create mode 100644 src/render/emoji.zig diff --git a/src/render/custom.zig b/src/render/custom.zig index 65c718b..ba44822 100644 --- a/src/render/custom.zig +++ b/src/render/custom.zig @@ -1,17 +1,6 @@ const std = @import("std"); const types = @import("../weather/types.zig"); - -const weather_icons = [_][]const u8{ - "✨", // 0-99 - "🌑", // 100-199 - "⛅", // 200-299 - "☁️", // 300-399 -}; - -fn getWeatherIcon(code: types.WeatherCode) []const u8 { - const idx = @min(@intFromEnum(code) / 100, weather_icons.len - 1); - return weather_icons[idx]; -} +const emoji = @import("emoji.zig"); pub fn render(allocator: std.mem.Allocator, weather: types.WeatherData, format: []const u8, use_imperial: bool) ![]const u8 { var output: std.ArrayList(u8) = .empty; @@ -23,7 +12,7 @@ pub fn render(allocator: std.mem.Allocator, weather: types.WeatherData, format: if (format[i] == '%' and i + 1 < format.len) { const code = format[i + 1]; switch (code) { - 'c' => try writer.print("{s}", .{getWeatherIcon(weather.current.weather_code)}), + 'c' => try writer.print("{s}", .{emoji.getWeatherEmoji(weather.current.weather_code)}), 'C' => try writer.print("{s}", .{weather.current.condition}), 'h' => try writer.print("{d}%", .{weather.current.humidity}), 't' => { diff --git a/src/render/emoji.zig b/src/render/emoji.zig new file mode 100644 index 0000000..e2bbd06 --- /dev/null +++ b/src/render/emoji.zig @@ -0,0 +1,16 @@ +const types = @import("../weather/types.zig"); + +pub fn getWeatherEmoji(code: types.WeatherCode) []const u8 { + return switch (@intFromEnum(code)) { + 800 => "☀️", // Clear + 801, 802 => "⛅️", // Few/scattered clouds + 803, 804 => "☁️", // Broken/overcast clouds + 701, 741 => "🌫", // Mist/fog + 300...321 => "🌦", // Drizzle + 500...531 => "🌧", // Rain + 200...232 => "⛈", // Thunderstorm + 611...616 => "🌧", // Sleet + 600...610, 617...622 => "🌨", // Snow + else => "✨", // Unknown + }; +} diff --git a/src/render/line.zig b/src/render/line.zig index 16e2abf..4eda4f6 100644 --- a/src/render/line.zig +++ b/src/render/line.zig @@ -1,5 +1,6 @@ const std = @import("std"); const types = @import("../weather/types.zig"); +const emoji = @import("emoji.zig"); pub fn render(allocator: std.mem.Allocator, data: types.WeatherData, format: []const u8, use_imperial: bool) ![]const u8 { if (std.mem.eql(u8, format, "1")) { @@ -7,7 +8,7 @@ pub fn render(allocator: std.mem.Allocator, data: types.WeatherData, format: []c const unit = if (use_imperial) "°F" else "°C"; return std.fmt.allocPrint(allocator, "{s}: {s} {d:.0}{s}", .{ data.location, - getConditionEmoji(data.current.weather_code), + emoji.getWeatherEmoji(data.current.weather_code), temp, unit, }); @@ -18,7 +19,7 @@ pub fn render(allocator: std.mem.Allocator, data: types.WeatherData, format: []c const wind_unit = if (use_imperial) "mph" else "km/h"; return std.fmt.allocPrint(allocator, "{s}: {s} {d:.0}{s} {s}{s}{d:.0}{s}", .{ data.location, - getConditionEmoji(data.current.weather_code), + emoji.getWeatherEmoji(data.current.weather_code), temp, unit, "🌬️", @@ -33,7 +34,7 @@ pub fn render(allocator: std.mem.Allocator, data: types.WeatherData, format: []c const wind_unit = if (use_imperial) "mph" else "km/h"; return std.fmt.allocPrint(allocator, "{s}: {s} {d:.0}{s} {s}{s}{d:.0}{s} {s}{d}%%", .{ data.location, - getConditionEmoji(data.current.weather_code), + emoji.getWeatherEmoji(data.current.weather_code), temp, unit, "🌬️", @@ -50,7 +51,7 @@ pub fn render(allocator: std.mem.Allocator, data: types.WeatherData, format: []c const wind_unit = if (use_imperial) "mph" else "km/h"; return std.fmt.allocPrint(allocator, "{s}: {s} {d:.0}{s} {s}{s}{d:.0}{s} {s}{d}%% {s}", .{ data.location, - getConditionEmoji(data.current.weather_code), + emoji.getWeatherEmoji(data.current.weather_code), temp, unit, "🌬️", @@ -75,7 +76,7 @@ fn renderCustom(allocator: std.mem.Allocator, data: types.WeatherData, format: [ if (format[i] == '%' and i + 1 < format.len) { const code = format[i + 1]; switch (code) { - 'c' => try output.appendSlice(allocator, getConditionEmoji(data.current.weather_code)), + 'c' => try output.appendSlice(allocator, emoji.getWeatherEmoji(data.current.weather_code)), 'C' => try output.appendSlice(allocator, data.current.condition), 'h' => try output.writer(allocator).print("{d}", .{data.current.humidity}), 't' => { @@ -116,21 +117,6 @@ fn renderCustom(allocator: std.mem.Allocator, data: types.WeatherData, format: [ return output.toOwnedSlice(allocator); } -fn getConditionEmoji(code: types.WeatherCode) []const u8 { - return switch (@intFromEnum(code)) { - 800 => "☀️", // Clear - 801, 802 => "⛅️", // Few/scattered clouds - 803, 804 => "☁️", // Broken/overcast clouds - 701, 741 => "🌁", // Mist/fog (alternatively, maybe 🌫️ or 🌫 is better) - 300...321 => "🌦", // Drizzle - 500...531 => "🌧️", // Rain - 200...232 => "⛈️", // Thunderstorm - 611...616 => "❄️", // Sleet/freezing (check before snow) - 600...610, 617...622 => "🌨️", // Snow - else => "🌡️", - }; -} - test "format 1" { const data = types.WeatherData{ .location = "London",