1
0
Fork 0
This commit is contained in:
namedkitten 2020-07-09 20:50:06 +01:00
parent 4a027944a3
commit 22995e9d34
7 changed files with 107 additions and 69 deletions

2
.gitmodules vendored
View file

@ -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

View file

@ -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",

1
deps/time vendored Submodule

@ -0,0 +1 @@
Subproject commit 04d7cb5a54fbbc0399fd9299b71f461b5bf4ae2c

View file

@ -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;
}

View file

@ -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();
}
}

39
src/widgets/time/time.zig Normal file
View file

@ -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,
};
}

View file

@ -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);
}
}
};