srctree

Gregory Mullen parent 6fa87b46 6d3d42f7
mate battery clickable

src/battery.zig added: 31, removed: 18, total 13
@@ -9,6 +9,7 @@ const Battery = @This();
updated: i64 = 0,
current: usize = 0,
powered: bool = false,
wide: bool = true,
name: []const u8 = "battery",
 
pub fn init() !Battery {
@@ -39,6 +40,10 @@ fn readPowerd(self: *Battery) !void {
}
}
 
pub fn click(bat: *Battery, _: u8) void {
bat.wide = !bat.wide;
}
 
pub fn update(self: *Battery, i: i64) !void {
if (self.updated > i) return;
 
@@ -106,15 +111,17 @@ pub fn format(self: Battery, comptime fmt: []const u8, _: std.fmt.FormatOptions,
return out.print("{}", .{p});
}
 
if (self.powered) {
if (self.current > 97) {
try out.print("Charged", .{});
return;
if (self.wide) {
if (self.powered) {
if (self.current > 97) {
try out.print("Charged", .{});
return;
} else {
try out.print("Charging", .{});
}
} else {
try out.print("Charging", .{});
try out.print("Battery", .{});
}
} else {
try out.print("Battery", .{});
}
 
if (self.current == 69) return out.print(" NICE!", .{});
 
src/main.zig added: 31, removed: 18, total 13
@@ -97,16 +97,22 @@ fn bl(click: ?Click) !Body {
}
}
 
var bat_handle: ?Battery = null;
var bat_buffer: [1024]u8 = undefined;
fn battery(_: ?Click) !Body {
var bat = try Battery.init();
try bat.update(std.time.timestamp());
return Body{
.full_text = try printFull(&bat_buffer, bat),
.markup = "pango",
.name = "battery",
.instance = "battery_0",
};
fn battery(clk: ?Click) !Body {
if (bat_handle) |*bat| {
if (clk) |c| bat.click(c.button);
try bat.update(std.time.timestamp());
return Body{
.full_text = try printFull(&bat_buffer, bat),
.markup = "pango",
.name = "battery",
.instance = "battery_0",
};
} else {
bat_handle = try Battery.init();
return battery(clk);
}
}
 
var ipa_buffer: [1024]u8 = undefined;