diff --git a/src/http/handler.zig b/src/http/handler.zig index 2934162..6540078 100644 --- a/src/http/handler.zig +++ b/src/http/handler.zig @@ -113,7 +113,7 @@ fn handleWeatherInternal( }; // Fetch weather using coordinates - const weather = opts.provider.fetch(req_alloc, location.coords) catch |err| { + var weather = opts.provider.fetch(req_alloc, location.coords) catch |err| { switch (err) { error.LocationNotFound => { res.status = 404; @@ -129,6 +129,9 @@ fn handleWeatherInternal( }; defer weather.deinit(); + // Set display name for rendering + weather.display_name = try req_alloc.dupe(u8, location.name); + const query_string = req.url.query; const params = try QueryParams.parse(req_alloc, query_string); defer { diff --git a/src/render/custom.zig b/src/render/custom.zig index 71d6ea2..7548862 100644 --- a/src/render/custom.zig +++ b/src/render/custom.zig @@ -41,7 +41,7 @@ pub fn render(allocator: std.mem.Allocator, weather: types.WeatherData, format: const unit = if (use_imperial) "mph" else "km/h"; try writer.print("{d:.0} {s} {s}", .{ wind, unit, utils.degreeToDirection(weather.current.wind_deg) }); }, - 'l' => try writer.print("{s}", .{weather.location}), + 'l' => try writer.print("{s}", .{weather.locationDisplayName()}), 'p' => { const precip = if (use_imperial) weather.current.precip_mm * 0.0393701 else weather.current.precip_mm; const unit = if (use_imperial) "in" else "mm"; diff --git a/src/render/formatted.zig b/src/render/formatted.zig index c8c6f30..af810af 100644 --- a/src/render/formatted.zig +++ b/src/render/formatted.zig @@ -66,7 +66,7 @@ pub fn render(allocator: std.mem.Allocator, data: types.WeatherData, options: Re const w = &output.writer; if (!options.no_caption) - try w.print("Weather report: {s}\n\n", .{data.location}); + try w.print("Weather report: {s}\n\n", .{data.locationDisplayName()}); try renderCurrent(w, data.current, options); diff --git a/src/render/v2.zig b/src/render/v2.zig index f6706f9..5f50e1e 100644 --- a/src/render/v2.zig +++ b/src/render/v2.zig @@ -8,7 +8,7 @@ pub fn render(allocator: std.mem.Allocator, weather: types.WeatherData, use_impe const writer = output.writer(allocator); // Header with location - try writer.print("Weather report: {s}\n\n", .{weather.location}); + try writer.print("Weather report: {s}\n\n", .{weather.locationDisplayName()}); // Current conditions try writer.print(" Current conditions\n", .{}); diff --git a/src/weather/types.zig b/src/weather/types.zig index c61631a..4712e0d 100644 --- a/src/weather/types.zig +++ b/src/weather/types.zig @@ -83,12 +83,14 @@ pub const WeatherError = error{ pub const WeatherData = struct { location: []const u8, + display_name: ?[]const u8 = null, current: CurrentCondition, forecast: []ForecastDay, allocator: std.mem.Allocator, pub fn deinit(self: WeatherData) void { self.allocator.free(self.location); + if (self.display_name) |name| self.allocator.free(name); self.allocator.free(self.current.condition); for (self.forecast) |day| { self.allocator.free(day.date); @@ -101,6 +103,10 @@ pub const WeatherData = struct { } self.allocator.free(self.forecast); } + + pub fn locationDisplayName(self: WeatherData) []const u8 { + return self.display_name orelse self.location; + } }; fn celsiusToFahrenheit(c: f32) f32 {