diff --git a/src/bar/bar.zig b/src/bar/bar.zig index cffa12c..bcbd2b1 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 ac5d2d0..5d043b4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -11,7 +11,7 @@ const DebugAllocator = @import("debug_allocator.zig"); const Info = @import("types/info.zig").Info; pub fn main() !void { - const debug: bool = true; + const debug: bool = false; var allocator: *std.mem.Allocator = undefined; var dbgAlloc: *DebugAllocator = undefined; if (debug) { @@ -28,8 +28,8 @@ pub fn main() !void { &Widget.init(&textWidget.New("owo", "potato")), &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(&timeWidget.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 index a010cc3..e581d5d 100644 --- a/src/widgets/battery/battery.zig +++ b/src/widgets/battery/battery.zig @@ -3,6 +3,7 @@ const Info = @import("../../types/info.zig").Info; const Bar = @import("../../types/bar.zig").Bar; const fs = std.fs; const cwd = fs.cwd; +const colour = @import("../../formatting/colour.zig").colour; 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 }); @@ -18,6 +19,22 @@ pub const PowerPaths = struct { voltage_now_path: []const u8 = "", }; +pub fn read_file_to_unsigned_int64(path: []const u8) u64 { + var buffer: [std.math.log10(std.math.maxInt(u64)) + 2]u8 = undefined; + var file = fs.cwd().openFile(path, .{}) catch return 0; + defer file.close(); + const siz = file.read(&buffer) catch return 0; + return std.fmt.parseInt(u64, buffer[0 .. siz - 1], 10) catch return 0; +} + +pub fn read_file(path: []const u8) ![]const u8 { + var buffer: [128]u8 = undefined; + var file = try fs.cwd().openFile(path, .{}); + defer file.close(); + const siz = try file.readAll(&buffer); + return if (buffer[siz - 1] == '\n') buffer[0 .. siz - 1] else buffer[0..siz]; +} + pub const BatteryWidget = struct { bar: *Bar, allocator: *std.mem.Allocator, @@ -106,18 +123,81 @@ pub const BatteryWidget = struct { } 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); + var pparena = std.heap.ArenaAllocator.init(self.allocator); + defer pparena.deinit(); + var ppallocator = &pparena.allocator; + const pp = try self.get_power_paths(ppallocator); + while (self.bar.keep_running()) { + var arena = std.heap.ArenaAllocator.init(self.allocator); + defer arena.deinit(); + var allocator = &arena.allocator; - std.debug.print("{} {} {} {} {}\n", .{ - pp.status_path, - pp.power_now_path, - pp.capacity_path, - pp.current_now_path, - pp.voltage_now_path, - }); + var can_get_watts: bool = false; + + var watts: f64 = 0; + var descriptor: []const u8 = ""; + var sign: []const u8 = "?"; + var power_colour: []const u8 = "#ffffff"; + + const capacity = @intToFloat(f64, read_file_to_unsigned_int64(pp.capacity_path)); + const status = try read_file(pp.status_path); + + if (capacity > 80) { + power_colour = "green"; + } else if (capacity > 60) { + power_colour = "yellow"; + } else if (capacity > 40) { + power_colour = "orange"; + } else { + power_colour = "red"; + } + + if (std.mem.eql(u8, status, "Charging")) { + descriptor = try colour(allocator, "green", "(C)"); + sign = "+"; + } else if (std.mem.eql(u8, status, "Discharging")) { + descriptor = try colour(allocator, power_colour, "(D)"); + sign = "-"; + } else if (std.mem.eql(u8, status, "Unknown")) { + descriptor = try colour(allocator, "yellow", "(U)"); + sign = "?"; + } + + if (pp.power_now_path.len != 0) { + watts = @intToFloat(f64, read_file_to_unsigned_int64(pp.power_now_path)) / 1000000; + can_get_watts = true; + } else if (pp.current_now_path.len != 0 and pp.voltage_now_path.len != 0) { + const current_now = @intToFloat(f64, read_file_to_unsigned_int64(pp.current_now_path)) / 1000000; + const voltage_now = @intToFloat(f64, read_file_to_unsigned_int64(pp.voltage_now_path)) / 1000000; + if (current_now == 0 or voltage_now == 0) { + can_get_watts = false; + } else { + watts = (current_now * voltage_now); + can_get_watts = true; + } + } + + var watts_info: []const u8 = ""; + + if (can_get_watts) { + watts_info = try colour(allocator, "purple", try std.fmt.allocPrint(allocator, " {}{d:0<2}W", .{ sign, watts })); + } + + var bat_info = try std.fmt.allocPrint(allocator, "{} {} {}{}{}", .{ + colour(allocator, "accentlight", "bat"), + descriptor, + colour(allocator, power_colour, try std.fmt.allocPrint(allocator, "{d:0<2}", .{capacity})), + colour(allocator, "accentdark", "%"), + watts_info, + }); + + try self.bar.add(Info{ + .name = "battery", + .full_text = bat_info, + .markup = "pango", + }); + std.time.sleep(std.time.ns_per_s); + } } }; diff --git a/src/widgets/weather/weather.zig b/src/widgets/weather/weather.zig index 90b32ee..d2737a1 100644 --- a/src/widgets/weather/weather.zig +++ b/src/widgets/weather/weather.zig @@ -191,7 +191,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); + std.time.sleep(30 * std.time.ns_per_min); } } };