Change some stuff around.
This commit is contained in:
parent
2c4bfec91c
commit
b9f0762dfb
|
@ -20,13 +20,11 @@ pub fn main() !void {
|
|||
var br = Bar.init(&bar);
|
||||
|
||||
var arena = std.heap.ArenaAllocator.init(allocator);
|
||||
var arenacator = &arena.allocator;
|
||||
|
||||
defer arena.deinit();
|
||||
const widgets = [_]*Widget{
|
||||
&Widget.init(&textWidget.New("owo", "potato")),
|
||||
&Widget.init(&weatherWidget.New(arenacator, &br, "London")),
|
||||
&Widget.init(&weatherWidget.New(&arena.allocator, &br, "London")),
|
||||
};
|
||||
bar.widgets = widgets[0..];
|
||||
try br.start();
|
||||
arena.deinit();
|
||||
}
|
||||
|
|
|
@ -7,6 +7,11 @@ const Bar = @import("../../types/bar.zig").Bar;
|
|||
const colour = @import("../../formatting/colour.zig").colour;
|
||||
const DebugAllocator = @import("../../debug_allocator.zig");
|
||||
|
||||
const WeatherData = struct {
|
||||
temp: u16,
|
||||
main: []const u8,
|
||||
};
|
||||
|
||||
pub const WeatherWidget = struct {
|
||||
allocator: *std.mem.Allocator,
|
||||
bar: *Bar,
|
||||
|
@ -35,12 +40,8 @@ pub const WeatherWidget = struct {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn start(self: *WeatherWidget) anyerror!void {
|
||||
defer self.mutex.deinit();
|
||||
while (self.bar.keep_running()) {
|
||||
std.time.sleep(2000 * std.time.ns_per_ms);
|
||||
|
||||
std.debug.print("Starting Weather Widget.\n", .{});
|
||||
fn get_weather_info(self: *WeatherWidget) !WeatherData {
|
||||
// this will allocate some memory but it will be freed by the time it is returned.
|
||||
var file = try net.tcpConnectToHost(self.allocator, "api.openweathermap.org", 80);
|
||||
std.debug.print("Connected to OpenWeatherMap.\n", .{});
|
||||
|
||||
|
@ -98,6 +99,32 @@ pub const WeatherWidget = struct {
|
|||
.status, .header, .head_complete, .closed, .end, .invalid => continue,
|
||||
}
|
||||
}
|
||||
return WeatherData{ .temp = temp, .main = main };
|
||||
}
|
||||
|
||||
fn update_info(self: *WeatherWidget) anyerror!void {
|
||||
var inf: WeatherData = undefined;
|
||||
if (self.get_weather_info()) |i| {
|
||||
inf = i;
|
||||
} else |err| switch (err) {
|
||||
error.TemporaryNameServerFailure => {
|
||||
const lock = self.mutex.acquire();
|
||||
self.info = Info{
|
||||
.name = "weather",
|
||||
.full_text = "weather DNS Error with a chance of WiFi",
|
||||
.markup = "pango",
|
||||
.color = "#ffffff",
|
||||
};
|
||||
lock.release();
|
||||
},
|
||||
else => |e| {
|
||||
return e;
|
||||
},
|
||||
}
|
||||
|
||||
var temp = inf.temp;
|
||||
var main = inf.main;
|
||||
|
||||
var tempColour: []const u8 = "green";
|
||||
if (temp >= 20) {
|
||||
tempColour = "red";
|
||||
|
@ -108,9 +135,6 @@ pub const WeatherWidget = struct {
|
|||
}
|
||||
var arena = std.heap.ArenaAllocator.init(self.allocator);
|
||||
var arenacator = &arena.allocator;
|
||||
if (self.info != null) {
|
||||
self.allocator.free(self.info.?.full_text);
|
||||
}
|
||||
|
||||
var i = Info{
|
||||
.name = "weather",
|
||||
|
@ -125,11 +149,23 @@ pub const WeatherWidget = struct {
|
|||
.color = "#ffffff",
|
||||
};
|
||||
const lock = self.mutex.acquire();
|
||||
if (self.info != null) {
|
||||
self.allocator.free(self.info.?.full_text);
|
||||
}
|
||||
self.info = i;
|
||||
lock.release();
|
||||
|
||||
arena.deinit();
|
||||
}
|
||||
|
||||
pub fn start(self: *WeatherWidget) anyerror!void {
|
||||
defer self.mutex.deinit();
|
||||
while (self.bar.keep_running()) {
|
||||
try self.update_info();
|
||||
std.time.sleep(std.time.ns_per_min);
|
||||
}
|
||||
if (self.info != null) {
|
||||
self.allocator.free(self.info.?.full_text);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue