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 br = Bar.init(&bar);
|
||||||
|
|
||||||
var arena = std.heap.ArenaAllocator.init(allocator);
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
||||||
var arenacator = &arena.allocator;
|
defer arena.deinit();
|
||||||
|
|
||||||
const widgets = [_]*Widget{
|
const widgets = [_]*Widget{
|
||||||
&Widget.init(&textWidget.New("owo", "potato")),
|
&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..];
|
bar.widgets = widgets[0..];
|
||||||
try br.start();
|
try br.start();
|
||||||
arena.deinit();
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,11 @@ const Bar = @import("../../types/bar.zig").Bar;
|
||||||
const colour = @import("../../formatting/colour.zig").colour;
|
const colour = @import("../../formatting/colour.zig").colour;
|
||||||
const DebugAllocator = @import("../../debug_allocator.zig");
|
const DebugAllocator = @import("../../debug_allocator.zig");
|
||||||
|
|
||||||
|
const WeatherData = struct {
|
||||||
|
temp: u16,
|
||||||
|
main: []const u8,
|
||||||
|
};
|
||||||
|
|
||||||
pub const WeatherWidget = struct {
|
pub const WeatherWidget = struct {
|
||||||
allocator: *std.mem.Allocator,
|
allocator: *std.mem.Allocator,
|
||||||
bar: *Bar,
|
bar: *Bar,
|
||||||
|
@ -35,12 +40,8 @@ pub const WeatherWidget = struct {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start(self: *WeatherWidget) anyerror!void {
|
fn get_weather_info(self: *WeatherWidget) !WeatherData {
|
||||||
defer self.mutex.deinit();
|
// this will allocate some memory but it will be freed by the time it is returned.
|
||||||
while (self.bar.keep_running()) {
|
|
||||||
std.time.sleep(2000 * std.time.ns_per_ms);
|
|
||||||
|
|
||||||
std.debug.print("Starting Weather Widget.\n", .{});
|
|
||||||
var file = try net.tcpConnectToHost(self.allocator, "api.openweathermap.org", 80);
|
var file = try net.tcpConnectToHost(self.allocator, "api.openweathermap.org", 80);
|
||||||
std.debug.print("Connected to OpenWeatherMap.\n", .{});
|
std.debug.print("Connected to OpenWeatherMap.\n", .{});
|
||||||
|
|
||||||
|
@ -98,6 +99,32 @@ pub const WeatherWidget = struct {
|
||||||
.status, .header, .head_complete, .closed, .end, .invalid => continue,
|
.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";
|
var tempColour: []const u8 = "green";
|
||||||
if (temp >= 20) {
|
if (temp >= 20) {
|
||||||
tempColour = "red";
|
tempColour = "red";
|
||||||
|
@ -108,9 +135,6 @@ pub const WeatherWidget = struct {
|
||||||
}
|
}
|
||||||
var arena = std.heap.ArenaAllocator.init(self.allocator);
|
var arena = std.heap.ArenaAllocator.init(self.allocator);
|
||||||
var arenacator = &arena.allocator;
|
var arenacator = &arena.allocator;
|
||||||
if (self.info != null) {
|
|
||||||
self.allocator.free(self.info.?.full_text);
|
|
||||||
}
|
|
||||||
|
|
||||||
var i = Info{
|
var i = Info{
|
||||||
.name = "weather",
|
.name = "weather",
|
||||||
|
@ -125,11 +149,23 @@ pub const WeatherWidget = struct {
|
||||||
.color = "#ffffff",
|
.color = "#ffffff",
|
||||||
};
|
};
|
||||||
const lock = self.mutex.acquire();
|
const lock = self.mutex.acquire();
|
||||||
|
if (self.info != null) {
|
||||||
|
self.allocator.free(self.info.?.full_text);
|
||||||
|
}
|
||||||
self.info = i;
|
self.info = i;
|
||||||
lock.release();
|
lock.release();
|
||||||
|
|
||||||
arena.deinit();
|
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