1
0
Fork 0
This commit is contained in:
namedkitten 2020-07-09 17:01:59 +01:00
parent b9f0762dfb
commit 20d3dc4dfc
7 changed files with 144 additions and 77 deletions

View file

@ -6,37 +6,88 @@ pub const Bar = struct {
allocator: *std.mem.Allocator,
widgets: []const *Widget,
running: bool,
infos: std.ArrayList(Info),
mutex: std.Mutex,
out_file: std.fs.File,
pub fn start(self: *Bar) !void {
self.running = true;
try self.out_file.writer().writeAll("{\"version\": 1,\"click_events\": true}\n[\n");
for (self.widgets) |w| {
std.debug.warn("Adding Initial Info: {}\n", .{w.name()});
try self.infos.append(try self.dupe_info(w.initial_info()));
std.debug.warn("Starting widget: {}\n", .{w.name()});
var thread = try std.Thread.spawn(w, Widget.start);
}
var thread = try std.Thread.spawn(self, Bar.process);
std.time.sleep(10000 * std.time.ns_per_ms);
self.running = false;
std.time.sleep(1000 * std.time.ns_per_ms);
return;
thread.wait();
for (self.infos.items) |info| {
try self.free_info(info);
}
self.infos.deinit();
}
fn process(self: *Bar) !void {
const out_file = std.io.getStdOut();
try out_file.writer().writeAll("{\"version\": 1,\"click_events\": true}\n[\n");
while (self.running) {
//std.debug.warn("I am a Square!\n", .{});
std.time.sleep(250 * std.time.ns_per_ms);
try out_file.writer().writeAll("[");
for (self.widgets) |w, i| {
try std.json.stringify(w.info(), .{}, out_file.writer());
if (i < self.widgets.len - 1) {
try out_file.writer().writeAll(",");
}
fn print_infos(self: *Bar, should_lock: bool) !void {
if (should_lock) {
const lock = self.mutex.acquire();
defer lock.release();
}
try self.out_file.writer().writeAll("[");
for (self.infos.items) |info, i| {
try std.json.stringify(info, .{}, self.out_file.writer());
if (i < self.infos.items.len - 1) {
try self.out_file.writer().writeAll(",");
}
try out_file.writer().writeAll("],\n");
}
try self.out_file.writer().writeAll("],\n");
}
fn process(self: *Bar) !void {
var i: i32 = 0;
while (self.running) {
//try self.print_infos(true);
std.time.sleep(1000 * std.time.ns_per_ms);
}
}
pub fn keep_running(self: Bar) bool {
pub fn keep_running(self: *Bar) bool {
return self.running;
}
pub fn free_info(self: *Bar, info: Info) !void {
self.allocator.free(info.name);
self.allocator.free(info.full_text);
}
pub fn dupe_info(self: *Bar, info: Info) !Info {
const new_name = try self.allocator.alloc(u8, info.name.len);
std.mem.copy(u8, new_name, info.name);
const new_text = try self.allocator.alloc(u8, info.full_text.len);
std.mem.copy(u8, new_text, info.full_text);
var i = Info{
.name = new_name,
.full_text = new_text,
.markup = "pango",
};
return i;
}
pub fn add(self: *Bar, info: Info) !void {
const lock = self.mutex.acquire();
defer lock.release();
//std.debug.warn("info: {}\n", .{info.name});
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;
}
// If we reach here then it changed.
try self.free_info(infoItem);
self.infos.items[index] = try self.dupe_info(info);
try self.print_infos(false);
}
}
}
};
pub fn InitBar(allocator: *std.mem.Allocator) Bar {
@ -44,5 +95,8 @@ pub fn InitBar(allocator: *std.mem.Allocator) Bar {
.allocator = allocator,
.widgets = undefined,
.running = false,
.infos = std.ArrayList(Info).init(allocator),
.mutex = std.Mutex.init(),
.out_file = std.io.getStdOut(),
};
}

View file

@ -8,22 +8,23 @@ const DebugAllocator = @import("debug_allocator.zig");
const colour = @import("formatting/colour.zig").colour;
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();
}
var allocator = &dbgAlloc.allocator;
//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();
//}
//var allocator = &dbgAlloc.allocator;
var allocator = std.heap.page_allocator;
var bar = barImpl.InitBar(allocator);
var br = Bar.init(&bar);
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
const widgets = [_]*Widget{
&Widget.init(&textWidget.New("owo", "potato")),
&Widget.init(&weatherWidget.New(&arena.allocator, &br, "London")),
&Widget.init(&weatherWidget.New(allocator, &br, "London")),
&Widget.init(&weatherWidget.New(allocator, &br, "Newcastle")),
};
bar.widgets = widgets[0..];
try br.start();

View file

@ -7,6 +7,7 @@ pub const Bar = struct {
const IFace = Interface(struct {
start: fn (*SelfType) anyerror!void,
keep_running: fn (*SelfType) bool,
add: fn (*SelfType, Info) anyerror!void,
}, interface.Storage.NonOwning);
iface: IFace,
pub fn init(impl_ptr: var) Bar {
@ -18,4 +19,7 @@ pub const Bar = struct {
pub fn start(self: *Bar) anyerror!void {
return try self.iface.call("start", .{});
}
pub fn add(self: *Bar, info: Info) anyerror!void {
return try self.iface.call("add", .{info});
}
};

View file

@ -2,5 +2,4 @@ pub const Info = struct {
name: []const u8,
markup: []const u8,
full_text: []const u8,
color: []const u8,
};

View file

@ -0,0 +1,28 @@
const std = @import("std");
const Info = @import("../../types/info.zig").Info;
pub const MemoryWidget = struct {
pub fn name(self: *MemoryWidget) []const u8 {
return self.name;
}
pub fn initial_info(self: *MemoryWidget) Info {
return Info{
.name = "mem",
.full_text = self.text,
.markup = "pango",
.color = "#ffaaff",
};
}
pub fn info(self: *MemoryWidget) Info {
return self.initial_info();
}
pub fn start(self: *MemoryWidget) anyerror!void {}
};
pub inline fn New(name: []const u8, Memory: []const u8) MemoryWidget {
return MemoryWidget{
.name = name,
.Memory = Memory,
};
}

View file

@ -13,7 +13,6 @@ pub const TextWidget = struct {
.name = self.name,
.full_text = self.text,
.markup = "pango",
.color = "#ffaaff",
};
}
pub fn info(self: *TextWidget) Info {

View file

@ -15,37 +15,28 @@ const WeatherData = struct {
pub const WeatherWidget = struct {
allocator: *std.mem.Allocator,
bar: *Bar,
name: []const u8,
weather_api_url: []const u8,
info: ?Info,
mutex: std.Mutex,
pub fn name(self: *WeatherWidget) []const u8 {
return "weather";
return self.name;
}
pub fn initial_info(self: *WeatherWidget) Info {
return Info{
.name = "weather",
.name = self.name,
.full_text = "weather",
.markup = "pango",
.color = "#ffffff",
};
}
pub fn info(self: *WeatherWidget) Info {
const lock = self.mutex.acquire();
defer lock.release();
if (self.info == null) {
return self.initial_info();
} else {
return self.info.?;
}
return self.initial_info();
}
fn get_weather_info(self: *WeatherWidget) !WeatherData {
fn get_weather_info(self: *WeatherWidget, allocator: *std.mem.Allocator) !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", .{});
var file = try net.tcpConnectToHost(allocator, "api.openweathermap.org", 80);
var read_buffer: [512]u8 = undefined;
var read_buffer: [512 * 512]u8 = undefined;
var client = hzzp.BaseClient.create(&read_buffer, &file.reader(), &file.writer());
try client.writeHead("GET", self.weather_api_url);
@ -55,8 +46,6 @@ pub const WeatherWidget = struct {
try client.writeHeader("Accept", "*/*");
try client.writeHeadComplete();
std.debug.print("Wrote Data, reading response.\n", .{});
var isNextTemp: bool = false;
var isNextMain: bool = false;
var foundMain: bool = false;
@ -103,22 +92,31 @@ pub const WeatherWidget = struct {
}
fn update_info(self: *WeatherWidget) anyerror!void {
std.debug.print("uwu!!\n", .{});
var inf: WeatherData = undefined;
if (self.get_weather_info()) |i| {
var arena = std.heap.ArenaAllocator.init(self.allocator);
defer arena.deinit();
var arenacator = &arena.allocator;
if (self.get_weather_info(arenacator)) |i| {
inf = i;
} else |err| switch (err) {
error.TemporaryNameServerFailure => {
const lock = self.mutex.acquire();
self.info = Info{
.name = "weather",
try self.bar.add(Info{
.name = self.name,
.full_text = "weather DNS Error with a chance of WiFi",
.markup = "pango",
.color = "#ffffff",
};
lock.release();
});
},
error.InvalidIPAddressFormat => {
try self.bar.add(Info{
.name = self.name,
.full_text = "invalid IP",
.markup = "pango",
});
},
else => |e| {
return e;
std.debug.print("\n\n\n\n\nError!: {}\n\n\n\n\n", .{@errorName(e)});
},
}
@ -133,12 +131,9 @@ pub const WeatherWidget = struct {
} else if (temp == 18) {
tempColour = "yellow";
}
var arena = std.heap.ArenaAllocator.init(self.allocator);
var arenacator = &arena.allocator;
var i = Info{
.name = "weather",
.full_text = try std.fmt.allocPrint(self.allocator, "{} {}{}{} {}", .{
.name = self.name,
.full_text = try std.fmt.allocPrint(arenacator, "{} {}{}{} {}", .{
colour(arenacator, "accentlight", "weather"),
colour(arenacator, tempColour, try std.fmt.allocPrint(arenacator, "{}", .{temp})),
colour(arenacator, "accentlight", "°"),
@ -146,25 +141,13 @@ pub const WeatherWidget = struct {
colour(arenacator, "green", main),
}),
.markup = "pango",
.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();
try self.bar.add(i);
}
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);
}
}
};
@ -173,8 +156,7 @@ pub inline fn New(allocator: *std.mem.Allocator, bar: *Bar, comptime location: [
return WeatherWidget{
.allocator = allocator,
.bar = bar,
.name = "weather " ++ location,
.weather_api_url = "/data/2.5/weather?q=" ++ location ++ "&appid=dcea3595afe693d1c17846141f58ea10&units=metric",
.info = null,
.mutex = std.Mutex.init(),
};
}