From b4353adc0ceac56291a5349fbe55e1ad13b578b8 Mon Sep 17 00:00:00 2001 From: namedkitten Date: Thu, 9 Jul 2020 11:44:36 +0100 Subject: [PATCH] Move to memory. --- src/bar/bar.zig | 2 +- src/main.zig | 7 ++++++- src/widgets/weather/weather.zig | 16 +++++++--------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/bar/bar.zig b/src/bar/bar.zig index b866cb8..59eb57f 100644 --- a/src/bar/bar.zig +++ b/src/bar/bar.zig @@ -13,7 +13,7 @@ pub const Bar = struct { var thread = try std.Thread.spawn(w, Widget.start); } var thread = try std.Thread.spawn(self, Bar.process); - std.time.sleep(100000 * std.time.ns_per_ms); + std.time.sleep(10000 * std.time.ns_per_ms); self.running = false; std.time.sleep(1000 * std.time.ns_per_ms); return; diff --git a/src/main.zig b/src/main.zig index 1f40265..6da401e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -18,10 +18,15 @@ pub fn main() !void { var allocator = &dbgAlloc.allocator; var bar = barImpl.InitBar(allocator); var br = Bar.init(&bar); + + var arena = std.heap.ArenaAllocator.init(allocator); + var arenacator = &arena.allocator; + const widgets = [_]*Widget{ &Widget.init(&textWidget.New("owo", "potato")), - &Widget.init(&weatherWidget.New(&br, "London")), + &Widget.init(&weatherWidget.New(arenacator, &br, "London")), }; bar.widgets = widgets[0..]; try br.start(); + arena.deinit(); } diff --git a/src/widgets/weather/weather.zig b/src/widgets/weather/weather.zig index b175242..1902a8f 100644 --- a/src/widgets/weather/weather.zig +++ b/src/widgets/weather/weather.zig @@ -8,6 +8,7 @@ const colour = @import("../../formatting/colour.zig").colour; const DebugAllocator = @import("../../debug_allocator.zig"); pub const WeatherWidget = struct { + allocator: *std.mem.Allocator, bar: *Bar, weather_api_url: []const u8, info: ?Info, @@ -32,15 +33,11 @@ pub const WeatherWidget = struct { } pub fn start(self: *WeatherWidget) anyerror!void { - var buffer: [@sizeOf(u8) * 1024 * 8]u8 = undefined; - var fba = std.heap.FixedBufferAllocator.init(&buffer); - var allocator = &fba.allocator; - while (true) { std.time.sleep(2000 * std.time.ns_per_ms); std.debug.print("Starting Weather Widget.\n", .{}); - var file = try net.tcpConnectToHost(allocator, "api.openweathermap.org", 80); + var file = try net.tcpConnectToHost(self.allocator, "api.openweathermap.org", 80); std.debug.print("Connected to OpenWeatherMap.\n", .{}); var read_buffer: [512]u8 = undefined; @@ -105,15 +102,15 @@ pub const WeatherWidget = struct { } else if (temp == 18) { tempColour = "yellow"; } - var arena = std.heap.ArenaAllocator.init(allocator); + var arena = std.heap.ArenaAllocator.init(self.allocator); var arenacator = &arena.allocator; if (self.info != null) { - allocator.free(self.info.?.full_text); + self.allocator.free(self.info.?.full_text); } self.info = Info{ .name = "weather", - .full_text = try std.fmt.allocPrint(allocator, "{} {}{}{} {}", .{ + .full_text = try std.fmt.allocPrint(self.allocator, "{} {}{}{} {}", .{ colour(arenacator, "accentlight", "weather"), colour(arenacator, tempColour, try std.fmt.allocPrint(arenacator, "{}", .{temp})), colour(arenacator, "accentlight", "°"), @@ -128,8 +125,9 @@ pub const WeatherWidget = struct { } }; -pub inline fn New(bar: *Bar, comptime location: []const u8) WeatherWidget { +pub inline fn New(allocator: *std.mem.Allocator, bar: *Bar, comptime location: []const u8) WeatherWidget { return WeatherWidget{ + .allocator = allocator, .bar = bar, .weather_api_url = "/data/2.5/weather?q=" ++ location ++ "&appid=dcea3595afe693d1c17846141f58ea10&units=metric", .info = null,