set the location display name in the handler
This commit is contained in:
parent
ff571626db
commit
005874b7bf
5 changed files with 13 additions and 4 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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", .{});
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue