From 22995e9d3483c2488358be105b99214c1a66de04 Mon Sep 17 00:00:00 2001 From: namedkitten Date: Thu, 9 Jul 2020 20:50:06 +0100 Subject: [PATCH] u --- .gitmodules | 2 +- build.zig | 4 ++ deps/time | 1 + src/bar/bar.zig | 6 +-- src/main.zig | 78 +++++++++------------------------ src/widgets/time/time.zig | 39 +++++++++++++++++ src/widgets/weather/weather.zig | 46 ++++++++++++++++--- 7 files changed, 107 insertions(+), 69 deletions(-) create mode 160000 deps/time create mode 100644 src/widgets/time/time.zig diff --git a/.gitmodules b/.gitmodules index 24bb87d..48ae8c6 100644 --- a/.gitmodules +++ b/.gitmodules @@ -3,7 +3,7 @@ url = https://github.com/alexnask/interface.zig [submodule "deps/time"] path = deps/time - url = https://github.com/gernest/time + url = https://github.com/purringChaos/time [submodule "deps/hzzp"] path = deps/hzzp url = https://github.com/truemedian/hzzp diff --git a/build.zig b/build.zig index b13bae7..bd91521 100644 --- a/build.zig +++ b/build.zig @@ -9,6 +9,10 @@ pub fn build(b: *Builder) void { .name = "interfaces", .path = "deps/interfaces/interface.zig", }); + exe.addPackage(.{ + .name = "time", + .path = "deps/time/src/time.zig", + }); exe.addPackage(.{ .name = "hzzp", .path = "deps/hzzp/src/main.zig", diff --git a/deps/time b/deps/time new file mode 160000 index 0000000..04d7cb5 --- /dev/null +++ b/deps/time @@ -0,0 +1 @@ +Subproject commit 04d7cb5a54fbbc0399fd9299b71f461b5bf4ae2c diff --git a/src/bar/bar.zig b/src/bar/bar.zig index 7ae073b..ff64cc3 100644 --- a/src/bar/bar.zig +++ b/src/bar/bar.zig @@ -47,10 +47,8 @@ pub const Bar = struct { } fn process(self: *Bar) !void { - var i: i32 = 0; while (self.running) { - //try self.print_infos(true); - std.time.sleep(10000 * std.time.ns_per_ms); + std.time.sleep(1000 * std.time.ns_per_ms); return; } } @@ -82,8 +80,6 @@ pub const Bar = struct { for (self.infos.items) |infoItem, index| { if (std.mem.eql(u8, infoItem.name, info.name)) { if (std.mem.eql(u8, infoItem.full_text, info.full_text)) { - std.debug.warn("dupe!: {}\n", .{info.name}); - // OK so info is a dupe, we don't care about dupes so we don't do anything. return; } diff --git a/src/main.zig b/src/main.zig index edb46c0..0208ea8 100644 --- a/src/main.zig +++ b/src/main.zig @@ -4,78 +4,40 @@ const Widget = @import("types/widget.zig").Widget; const barImpl = @import("bar/bar.zig"); const textWidget = @import("widgets/text/text.zig"); const weatherWidget = @import("widgets/weather/weather.zig"); -const DebugAllocator = @import("debug_allocator.zig"); -const colour = @import("formatting/colour.zig").colour; +const timeWidget = @import("widgets/time/time.zig"); +const DebugAllocator = @import("debug_allocator.zig"); const Info = @import("types/info.zig").Info; -pub const SpamWidget = struct { - name: []const u8, - bar: *Bar, - - pub fn name(self: *SpamWidget) []const u8 { - return self.name; - } - pub fn initial_info(self: *SpamWidget) Info { - return Info{ - .name = self.name, - .full_text = "uwu", - .markup = "pango", - }; - } - pub fn info(self: *SpamWidget) Info { - return self.initial_info(); - } - - pub fn start(self: *SpamWidget) anyerror!void { - var h: bool = true; - while (self.bar.keep_running()) { - h = !h; - if (h) { - try self.bar.add(Info{ - .name = self.name, - .full_text = "uwu", - .markup = "pango", - }); - } else { - try self.bar.add(Info{ - .name = self.name, - .full_text = "owo", - .markup = "pango", - }); - } - } - } -}; - -pub inline fn NewSpam(bar: *Bar, name: []const u8) SpamWidget { - return SpamWidget{ - .name = name, - .bar = bar, - }; -} - pub fn main() !void { - const dbgAlloc = &DebugAllocator.init(std.heap.page_allocator, 8192 * 512); - defer { - std.debug.print("Finished cleanup, last allocation info.\n", .{}); - std.debug.print("\n{}\n", .{dbgAlloc.info}); - dbgAlloc.printRemainingStackTraces(); - dbgAlloc.deinit(); + const debug: bool = true; + var allocator: *std.mem.Allocator = undefined; + var dbgAlloc: *DebugAllocator = undefined; + if (debug) { + dbgAlloc = &DebugAllocator.init(std.heap.page_allocator, 8192 * 8192); + allocator = &dbgAlloc.allocator; + } else { + allocator = std.heap.page_allocator; } - var allocator = &dbgAlloc.allocator; - //var allocator = std.heap.page_allocator; var bar = barImpl.InitBar(allocator); var br = Bar.init(&bar); const widgets = [_]*Widget{ &Widget.init(&textWidget.New("owo", "potato")), - &Widget.init(&textWidget.New("uwu", "potato")), - &Widget.init(&NewSpam(&br, "h")), + &Widget.init(&textWidget.New("uwu", "tomato")), &Widget.init(&weatherWidget.New(allocator, &br, "London")), + &Widget.init(&timeWidget.New(allocator, &br)), + + //&Widget.init(&weatherWidget.New(allocator, &br, "Oxford")), //&Widget.init(&weatherWidget.New(allocator, &br, "Newcastle")), }; bar.widgets = widgets[0..]; try br.start(); + if (debug) { + std.debug.print("Finished cleanup, last allocation info.\n", .{}); + std.debug.print("\n{}\n", .{dbgAlloc.info}); + dbgAlloc.printRemainingStackTraces(); + dbgAlloc.deinit(); + } } diff --git a/src/widgets/time/time.zig b/src/widgets/time/time.zig new file mode 100644 index 0000000..d5665fb --- /dev/null +++ b/src/widgets/time/time.zig @@ -0,0 +1,39 @@ +const std = @import("std"); +const Info = @import("../../types/info.zig").Info; +const Bar = @import("../../types/bar.zig").Bar; +const time = @import("time"); + +pub const TimeWidget = struct { + bar: *Bar, + allocator: *std.mem.Allocator, + + pub fn name(self: *TimeWidget) []const u8 { + return "time"; + } + pub fn initial_info(self: *TimeWidget) Info { + return Info{ + .name = "time", + .full_text = "TheTimeā„¢", + .markup = "pango", + }; + } + pub fn info(self: *TimeWidget) Info { + return self.initial_info(); + } + + pub fn start(self: *TimeWidget) anyerror!void { + var arena = std.heap.ArenaAllocator.init(self.allocator); + defer arena.deinit(); + var allocator = &arena.allocator; + var local = time.Location.getLocal(allocator); + var now = time.now(&local); + std.debug.print("OwO: {}\n", .{now.date()}); + } +}; + +pub inline fn New(allocator: *std.mem.Allocator, bar: *Bar) TimeWidget { + return TimeWidget{ + .allocator = allocator, + .bar = bar, + }; +} diff --git a/src/widgets/weather/weather.zig b/src/widgets/weather/weather.zig index 26511dd..821c22f 100644 --- a/src/widgets/weather/weather.zig +++ b/src/widgets/weather/weather.zig @@ -10,6 +10,8 @@ const DebugAllocator = @import("../../debug_allocator.zig"); const WeatherData = struct { temp: u16, main: []const u8, + code: i16, + message: []const u8, }; pub const WeatherWidget = struct { @@ -48,10 +50,15 @@ pub const WeatherWidget = struct { var isNextTemp: bool = false; var isNextMain: bool = false; + var isNextCode: bool = false; + var isNextMessage: bool = false; + var foundMain: bool = false; - var temp: u16 = undefined; - var main: []const u8 = undefined; + var temp: u16 = 0; + var code: i16 = 0; + var main: []const u8 = ""; + var message: []const u8 = ""; while (try client.readEvent()) |event| { switch (event) { @@ -69,17 +76,37 @@ pub const WeatherWidget = struct { isNextMain = true; continue; } + if (std.mem.eql(u8, str, "cod")) { + isNextCode = true; + continue; + } + if (std.mem.eql(u8, str, "message")) { + isNextMessage = true; + continue; + } if (isNextMain) { main = str; isNextMain = false; foundMain = true; } + if (isNextMessage) { + message = str; + } + if (isNextCode) { + // why the fuck would you make code both a string and a int are you wanting me to question my sanity??? + isNextCode = false; + code = try std.fmt.parseInt(i16, str, 10); + } }, .Number => |num| { if (isNextTemp) { isNextTemp = false; temp = @floatToInt(u16, std.math.round(try std.fmt.parseFloat(f32, num.slice(tokens.slice, tokens.i - 1)))); } + if (isNextCode) { + isNextCode = false; + code = try std.fmt.parseInt(i16, num.slice(tokens.slice, tokens.i - 1), 10); + } }, else => {}, } @@ -88,18 +115,17 @@ pub const WeatherWidget = struct { .status, .header, .head_complete, .closed, .end, .invalid => continue, } } - return WeatherData{ .temp = temp, .main = main }; + return WeatherData{ .temp = temp, .main = main, .code = code, .message = message }; } fn update_info(self: *WeatherWidget) anyerror!void { - std.debug.print("uwu!!\n", .{}); - var inf: WeatherData = undefined; var arena = std.heap.ArenaAllocator.init(self.allocator); defer arena.deinit(); var arenacator = &arena.allocator; if (self.get_weather_info(arenacator)) |i| { inf = i; + //std.debug.print("{}", .{i}); } else |err| switch (err) { error.TemporaryNameServerFailure => { try self.bar.add(Info{ @@ -120,6 +146,15 @@ pub const WeatherWidget = struct { }, } + if (inf.code != 200) { + try self.bar.add(Info{ + .name = self.name, + .full_text = try std.fmt.allocPrint(arenacator, "Weather API Failed: {}", .{inf.message}), + .markup = "pango", + }); + return; + } + var temp = inf.temp; var main = inf.main; @@ -148,6 +183,7 @@ pub const WeatherWidget = struct { pub fn start(self: *WeatherWidget) anyerror!void { while (self.bar.keep_running()) { try self.update_info(); + std.time.sleep(1000 * std.time.ns_per_ms); } } };