set the location display name in the handler

This commit is contained in:
Emil Lerch 2026-01-04 10:30:40 -08:00
parent ff571626db
commit 005874b7bf
Signed by: lobo
GPG key ID: A7B62D657EF764F8
5 changed files with 13 additions and 4 deletions

View file

@ -113,7 +113,7 @@ fn handleWeatherInternal(
}; };
// Fetch weather using coordinates // 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) { switch (err) {
error.LocationNotFound => { error.LocationNotFound => {
res.status = 404; res.status = 404;
@ -129,6 +129,9 @@ fn handleWeatherInternal(
}; };
defer weather.deinit(); 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 query_string = req.url.query;
const params = try QueryParams.parse(req_alloc, query_string); const params = try QueryParams.parse(req_alloc, query_string);
defer { defer {

View file

@ -41,7 +41,7 @@ pub fn render(allocator: std.mem.Allocator, weather: types.WeatherData, format:
const unit = if (use_imperial) "mph" else "km/h"; const unit = if (use_imperial) "mph" else "km/h";
try writer.print("{d:.0} {s} {s}", .{ wind, unit, utils.degreeToDirection(weather.current.wind_deg) }); 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' => { 'p' => {
const precip = if (use_imperial) weather.current.precip_mm * 0.0393701 else weather.current.precip_mm; const precip = if (use_imperial) weather.current.precip_mm * 0.0393701 else weather.current.precip_mm;
const unit = if (use_imperial) "in" else "mm"; const unit = if (use_imperial) "in" else "mm";

View file

@ -66,7 +66,7 @@ pub fn render(allocator: std.mem.Allocator, data: types.WeatherData, options: Re
const w = &output.writer; const w = &output.writer;
if (!options.no_caption) 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); try renderCurrent(w, data.current, options);

View file

@ -8,7 +8,7 @@ pub fn render(allocator: std.mem.Allocator, weather: types.WeatherData, use_impe
const writer = output.writer(allocator); const writer = output.writer(allocator);
// Header with location // 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 // Current conditions
try writer.print(" Current conditions\n", .{}); try writer.print(" Current conditions\n", .{});

View file

@ -83,12 +83,14 @@ pub const WeatherError = error{
pub const WeatherData = struct { pub const WeatherData = struct {
location: []const u8, location: []const u8,
display_name: ?[]const u8 = null,
current: CurrentCondition, current: CurrentCondition,
forecast: []ForecastDay, forecast: []ForecastDay,
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
pub fn deinit(self: WeatherData) void { pub fn deinit(self: WeatherData) void {
self.allocator.free(self.location); self.allocator.free(self.location);
if (self.display_name) |name| self.allocator.free(name);
self.allocator.free(self.current.condition); self.allocator.free(self.current.condition);
for (self.forecast) |day| { for (self.forecast) |day| {
self.allocator.free(day.date); self.allocator.free(day.date);
@ -101,6 +103,10 @@ pub const WeatherData = struct {
} }
self.allocator.free(self.forecast); self.allocator.free(self.forecast);
} }
pub fn locationDisplayName(self: WeatherData) []const u8 {
return self.display_name orelse self.location;
}
}; };
fn celsiusToFahrenheit(c: f32) f32 { fn celsiusToFahrenheit(c: f32) f32 {