From 43980feb61714654cec93b5c36c97bde3436e647 Mon Sep 17 00:00:00 2001 From: namedkitten Date: Fri, 10 Jul 2020 00:13:07 +0100 Subject: [PATCH] h --- src/bar/bar.zig | 2 +- src/main.zig | 4 +- src/widgets/battery/battery.zig | 129 ++++++++++++++++++++++++++++++++ src/widgets/weather/weather.zig | 12 ++- 4 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 src/widgets/battery/battery.zig diff --git a/src/bar/bar.zig b/src/bar/bar.zig index bcbd2b1..cffa12c 100644 --- a/src/bar/bar.zig +++ b/src/bar/bar.zig @@ -49,7 +49,7 @@ pub const Bar = struct { fn process(self: *Bar) !void { while (self.running) { std.time.sleep(5000 * std.time.ns_per_ms); - //return; + return; } } pub fn keep_running(self: *Bar) bool { diff --git a/src/main.zig b/src/main.zig index 08cd7fd..ac5d2d0 100644 --- a/src/main.zig +++ b/src/main.zig @@ -5,13 +5,12 @@ const barImpl = @import("bar/bar.zig"); const textWidget = @import("widgets/text/text.zig"); const weatherWidget = @import("widgets/weather/weather.zig"); const timeWidget = @import("widgets/time/time.zig"); +const batteryWidget = @import("widgets/battery/battery.zig"); const DebugAllocator = @import("debug_allocator.zig"); const Info = @import("types/info.zig").Info; pub fn main() !void { - std.debug.print("{}\n", .{@import("builtin").link_libc}); - const debug: bool = true; var allocator: *std.mem.Allocator = undefined; var dbgAlloc: *DebugAllocator = undefined; @@ -30,6 +29,7 @@ pub fn main() !void { &Widget.init(&textWidget.New("uwu", "tomato")), &Widget.init(&weatherWidget.New(allocator, &br, "London")), &Widget.init(&timeWidget.New(allocator, &br)), + &Widget.init(&batteryWidget.New(allocator, &br)), //&Widget.init(&weatherWidget.New(allocator, &br, "Oxford")), //&Widget.init(&weatherWidget.New(allocator, &br, "Newcastle")), diff --git a/src/widgets/battery/battery.zig b/src/widgets/battery/battery.zig new file mode 100644 index 0000000..a010cc3 --- /dev/null +++ b/src/widgets/battery/battery.zig @@ -0,0 +1,129 @@ +const std = @import("std"); +const Info = @import("../../types/info.zig").Info; +const Bar = @import("../../types/bar.zig").Bar; +const fs = std.fs; +const cwd = fs.cwd; + +pub fn compare_from_walker(allocator: *std.mem.Allocator, path: []const u8, start_path: []const u8, required_filename: []const u8) !bool { + var full_path = try std.fmt.allocPrint(allocator, "{}/{}", .{ start_path, required_filename }); + defer allocator.free(full_path); + return std.mem.eql(u8, path, full_path); +} + +pub const PowerPaths = struct { + status_path: []const u8 = "", + power_now_path: []const u8 = "", + capacity_path: []const u8 = "", + current_now_path: []const u8 = "", + voltage_now_path: []const u8 = "", +}; + +pub const BatteryWidget = struct { + bar: *Bar, + allocator: *std.mem.Allocator, + + pub fn name(self: *BatteryWidget) []const u8 { + return "battery"; + } + pub fn initial_info(self: *BatteryWidget) Info { + return Info{ + .name = "battery", + .full_text = "bat", + .markup = "pango", + }; + } + pub fn info(self: *BatteryWidget) Info { + return self.initial_info(); + } + pub fn get_power_paths(self: *BatteryWidget, provided_allocator: *std.mem.Allocator) anyerror!PowerPaths { + // remember that PowerPaths fields are allocated with self.allocator and will need to be freed seporately + var arena = std.heap.ArenaAllocator.init(provided_allocator); + defer arena.deinit(); + var allocator = &arena.allocator; + var power_supply_walker = try fs.walkPath(allocator, "/sys/class/power_supply"); + defer power_supply_walker.deinit(); + var power_supply_dirs = std.ArrayList([]const u8).init(self.allocator); + defer power_supply_dirs.deinit(); + + var pp = PowerPaths{}; + + while (try power_supply_walker.next()) |entry| { + switch (entry.kind) { + .SymLink => { + try power_supply_dirs.append(entry.path); + }, + .File, + .BlockDevice, + .CharacterDevice, + .Directory, + .NamedPipe, + .UnixDomainSocket, + .Whiteout, + .Unknown, + => continue, + } + } + for (power_supply_dirs.items) |filepath| { + var power_supply_file_walker = try fs.walkPath(allocator, filepath); + defer power_supply_file_walker.deinit(); + while (try power_supply_file_walker.next()) |entry| { + switch (entry.kind) { + .File => { + if (try compare_from_walker(allocator, entry.path, filepath, "status")) { + pp.status_path = try std.fmt.allocPrint(provided_allocator, "{}", .{entry.path}); + continue; + } + if (try compare_from_walker(allocator, entry.path, filepath, "power_now")) { + pp.power_now_path = try std.fmt.allocPrint(provided_allocator, "{}", .{entry.path}); + continue; + } + if (try compare_from_walker(allocator, entry.path, filepath, "capacity")) { + pp.capacity_path = try std.fmt.allocPrint(provided_allocator, "{}", .{entry.path}); + continue; + } + if (try compare_from_walker(allocator, entry.path, filepath, "current_now")) { + pp.current_now_path = try std.fmt.allocPrint(provided_allocator, "{}", .{entry.path}); + continue; + } + if (try compare_from_walker(allocator, entry.path, filepath, "voltage_now")) { + pp.voltage_now_path = try std.fmt.allocPrint(provided_allocator, "{}", .{entry.path}); + continue; + } + }, + .SymLink, + .BlockDevice, + .CharacterDevice, + .Directory, + .NamedPipe, + .UnixDomainSocket, + .Whiteout, + .Unknown, + => continue, + } + } + } + return pp; + } + + pub fn start(self: *BatteryWidget) anyerror!void { + var arena = std.heap.ArenaAllocator.init(self.allocator); + defer arena.deinit(); + var allocator = &arena.allocator; + const pp = try self.get_power_paths(allocator); + + std.debug.print("{} {} {} {} {}\n", .{ + pp.status_path, + pp.power_now_path, + pp.capacity_path, + pp.current_now_path, + pp.voltage_now_path, + }); + } +}; + +pub inline fn New(allocator: *std.mem.Allocator, bar: *Bar) BatteryWidget { + return BatteryWidget{ + .allocator = allocator, + .bar = bar, + }; +} diff --git a/src/widgets/weather/weather.zig b/src/widgets/weather/weather.zig index 821c22f..90b32ee 100644 --- a/src/widgets/weather/weather.zig +++ b/src/widgets/weather/weather.zig @@ -130,19 +130,27 @@ pub const WeatherWidget = struct { error.TemporaryNameServerFailure => { try self.bar.add(Info{ .name = self.name, - .full_text = "weather DNS Error with a chance of WiFi", + .full_text = "DNS Error Fetching Weather", .markup = "pango", }); }, error.InvalidIPAddressFormat => { try self.bar.add(Info{ .name = self.name, - .full_text = "invalid IP", + .full_text = "Invalid Weather IP", + .markup = "pango", + }); + }, + error.ConnectionResetByPeer => { + try self.bar.add(Info{ + .name = self.name, + .full_text = "Weather Reset", .markup = "pango", }); }, else => |e| { std.debug.print("\n\n\n\n\nError!: {}\n\n\n\n\n", .{@errorName(e)}); + return; }, }