diff --git a/src/render/custom.zig b/src/render/custom.zig index 4a3a227..71d6ea2 100644 --- a/src/render/custom.zig +++ b/src/render/custom.zig @@ -37,7 +37,7 @@ pub fn render(allocator: std.mem.Allocator, weather: types.WeatherData, format: } }, 'w' => { - const wind = if (use_imperial) weather.current.wind_kph * 0.621371 else weather.current.wind_kph; + const wind = if (use_imperial) weather.current.windMph() else weather.current.wind_kph; const unit = if (use_imperial) "mph" else "km/h"; try writer.print("{d:.0} {s} {s}", .{ wind, unit, utils.degreeToDirection(weather.current.wind_deg) }); }, diff --git a/src/render/formatted.zig b/src/render/formatted.zig index 081405c..86fdf7c 100644 --- a/src/render/formatted.zig +++ b/src/render/formatted.zig @@ -48,7 +48,7 @@ fn renderCurrent(w: *std.Io.Writer, current: types.CurrentCondition, options: Re const feels_like = if (options.use_imperial) current.feelsLikeFahrenheit() else current.feels_like_c; const temp_unit = if (options.use_imperial) "°F" else "°C"; const wind_unit = if (options.use_imperial) "mph" else "km/h"; - const wind_speed = if (options.use_imperial) current.wind_kph * 0.621371 else current.wind_kph; + const wind_speed = if (options.use_imperial) current.windMph() else current.wind_kph; const precip_unit = if (options.use_imperial) "in" else "mm"; const precip = if (options.use_imperial) current.precip_mm * 0.0393701 else current.precip_mm; @@ -62,8 +62,8 @@ fn renderCurrent(w: *std.Io.Writer, current: types.CurrentCondition, options: Re try w.print("{s} {s}\n", .{ art[0], current.condition }); try w.print("{s} {c}{d:.0}({c}{d:.0}) {s}\n", .{ art[1], sign, abs_temp, fl_sign, abs_fl, temp_unit }); try w.print("{s} {s} {d:.0} {s}\n", .{ art[2], degreeToArrow(current.wind_deg), wind_speed, wind_unit }); - if (current.visibility_km) |vis_km| { - const visibility = if (options.use_imperial) vis_km * 0.621371 else vis_km; + if (current.visibility_km) |_| { + const visibility = if (options.use_imperial) current.visiblityMph().? else current.visibility_km.?; const vis_unit = if (options.use_imperial) "mi" else "km"; try w.print("{s} {d:.0} {s}\n", .{ art[3], visibility, vis_unit }); } else { @@ -79,8 +79,8 @@ fn renderCurrent(w: *std.Io.Writer, current: types.CurrentCondition, options: Re try w.print("{s}{s}{s} {s}\n", .{ cloud_color, art[0], reset, current.condition }); try w.print("{s}{s}{s} \x1b[38;5;{d}m{c}{d:.0}({c}{d:.0}){s} {s}\n", .{ cloud_color, art[1], reset, temp_color_code, sign, abs_temp, fl_sign, abs_fl, reset, temp_unit }); try w.print("{s}{s}{s} {s} \x1b[38;5;{d}m{d:.0}{s} {s}\n", .{ cloud_color, art[2], reset, degreeToArrow(current.wind_deg), wind_color_code, wind_speed, reset, wind_unit }); - if (current.visibility_km) |vis_km| { - const visibility = if (options.use_imperial) vis_km * 0.621371 else vis_km; + if (current.visibility_km) |_| { + const visibility = if (options.use_imperial) current.visiblityMph().? else current.visibility_km.?; const vis_unit = if (options.use_imperial) "mi" else "km"; try w.print("{s}{s}{s} {d:.0} {s}\n", .{ cloud_color, art[3], reset, visibility, vis_unit }); } else { @@ -170,15 +170,15 @@ fn renderHourlyCell(w: *std.Io.Writer, hour: types.HourlyForecast, line: usize, display_width_byte_length_offset = 1; }, .wind => { - const wind_speed = if (options.use_imperial) hour.wind_kph * 0.621371192237 else hour.wind_kph; + const wind_speed = if (options.use_imperial) hour.windMph() else hour.wind_kph; const wind_unit = if (options.use_imperial) "mph" else "km/h"; const arrow = degreeToArrow(hour.wind_deg); try cell_writer.print("{s} {d:.0} {s}", .{ arrow, wind_speed, wind_unit }); display_width_byte_length_offset = arrow.len - 1; }, .visibility => { - if (hour.visibility_km) |vis_km| { - const visibility = if (options.use_imperial) vis_km * 0.621371 else vis_km; + if (hour.visibility_km) |_| { + const visibility = if (options.use_imperial) hour.visiblityMph().? else hour.visibility_km.?; const vis_unit = if (options.use_imperial) "mi" else "km"; try cell_writer.print("{d:.0} {s}", .{ visibility, vis_unit }); } diff --git a/src/render/line.zig b/src/render/line.zig index ba78b89..cca9a5b 100644 --- a/src/render/line.zig +++ b/src/render/line.zig @@ -16,7 +16,7 @@ pub fn render(allocator: std.mem.Allocator, data: types.WeatherData, format: []c } else if (std.mem.eql(u8, format, "2")) { const temp = if (use_imperial) data.current.tempFahrenheit() else data.current.temp_c; const unit = if (use_imperial) "°F" else "°C"; - const wind = if (use_imperial) data.current.wind_kph * 0.621371 else data.current.wind_kph; + const wind = if (use_imperial) data.current.windMph() else data.current.wind_kph; 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, @@ -31,7 +31,7 @@ pub fn render(allocator: std.mem.Allocator, data: types.WeatherData, format: []c } else if (std.mem.eql(u8, format, "3")) { const temp = if (use_imperial) data.current.tempFahrenheit() else data.current.temp_c; const unit = if (use_imperial) "°F" else "°C"; - const wind = if (use_imperial) data.current.wind_kph * 0.621371 else data.current.wind_kph; + const wind = if (use_imperial) data.current.windMph() else data.current.wind_kph; 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, @@ -48,7 +48,7 @@ pub fn render(allocator: std.mem.Allocator, data: types.WeatherData, format: []c } else if (std.mem.eql(u8, format, "4")) { const temp = if (use_imperial) data.current.tempFahrenheit() else data.current.temp_c; const unit = if (use_imperial) "°F" else "°C"; - const wind = if (use_imperial) data.current.wind_kph * 0.621371 else data.current.wind_kph; + const wind = if (use_imperial) data.current.windMph() else data.current.wind_kph; 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, @@ -89,7 +89,7 @@ fn renderCustom(allocator: std.mem.Allocator, data: types.WeatherData, format: [ try output.writer(allocator).print("{d:.0}", .{temp}); }, 'w' => { - const wind = if (use_imperial) data.current.wind_kph * 0.621371 else data.current.wind_kph; + const wind = if (use_imperial) data.current.windMph() else data.current.wind_kph; const wind_unit = if (use_imperial) "mph" else "km/h"; try output.writer(allocator).print("{s}{d:.0}{s}", .{ utils.degreeToDirection(data.current.wind_deg), wind, wind_unit }); }, diff --git a/src/render/v2.zig b/src/render/v2.zig index b9eedbc..f6706f9 100644 --- a/src/render/v2.zig +++ b/src/render/v2.zig @@ -23,7 +23,7 @@ pub fn render(allocator: std.mem.Allocator, weather: types.WeatherData, use_impe try writer.print("{d}%\n", .{weather.current.humidity}); try writer.writeAll(" 🌬️ "); if (use_imperial) { - const wind_mph = weather.current.wind_kph * 0.621371; + const wind_mph = weather.current.windMph(); try writer.print("{d:.1} mph {s}\n", .{ wind_mph, utils.degreeToDirection(weather.current.wind_deg) }); } else { try writer.print("{d:.1} km/h {s}\n", .{ weather.current.wind_kph, utils.degreeToDirection(weather.current.wind_deg) }); diff --git a/src/weather/types.zig b/src/weather/types.zig index 1ac79c2..6d60307 100644 --- a/src/weather/types.zig +++ b/src/weather/types.zig @@ -106,6 +106,8 @@ pub const WeatherData = struct { fn celsiusToFahrenheit(c: f32) f32 { return c * 9.0 / 5.0 + 32.0; } +const miles_per_km = 0.621371192237; + pub const CurrentCondition = struct { temp_c: f32, feels_like_c: f32, @@ -124,6 +126,12 @@ pub const CurrentCondition = struct { pub fn feelsLikeFahrenheit(self: CurrentCondition) f32 { return celsiusToFahrenheit(self.feels_like_c); } + pub fn windMph(self: CurrentCondition) f32 { + return self.wind_kph * miles_per_km; + } + pub fn visiblityMph(self: CurrentCondition) ?f32 { + return if (self.visibility_km) |k| k * miles_per_km else null; + } }; pub const ForecastDay = struct { @@ -152,4 +160,11 @@ pub const HourlyForecast = struct { wind_deg: f32, precip_mm: f32, visibility_km: ?f32, + + pub fn windMph(self: HourlyForecast) f32 { + return self.wind_kph * miles_per_km; + } + pub fn visiblityMph(self: HourlyForecast) ?f32 { + return if (self.visibility_km) |k| k * miles_per_km else null; + } };