From f5101f8915db657d3af288ffb318679194033727 Mon Sep 17 00:00:00 2001 From: ptrcnull Date: Thu, 2 Nov 2023 21:02:14 +0100 Subject: [PATCH] feat: allow specifying battery name --- src/main.zig | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main.zig b/src/main.zig index 6665849..7847bd3 100644 --- a/src/main.zig +++ b/src/main.zig @@ -13,6 +13,7 @@ pub fn main() !void { defer std.process.argsFree(allocator, args); var target: u8 = 15; + var battery: []const u8 = "BAT0"; for (args, 0..) |arg, i| { if (std.mem.eql(u8, arg, "--help")) { @@ -39,12 +40,22 @@ pub fn main() !void { }; target = parsedTarget; } + + if (std.mem.eql(u8, arg, "--battery")) { + if (i + 1 == args.len) { + std.log.err("--battery requires an argument", .{}); + std.process.exit(1); + } + + battery = args[i + 1]; + } } var sent = false; + const batteryPath = try std.fmt.allocPrint(allocator, "/sys/class/power_supply/{s}/capacity", .{battery}); while (true) { - const capacityFile = try std.fs.openFileAbsolute("/sys/class/power_supply/BAT0/capacity", .{}); + const capacityFile = try std.fs.openFileAbsolute(batteryPath, .{}); var capacityStr = std.ArrayList(u8).init(allocator); try capacityFile.reader().streamUntilDelimiter(capacityStr.writer(), '\n', null); capacityFile.close(); @@ -66,6 +77,7 @@ pub fn main() !void { } else { sent = false; } + capacityStr.deinit(); std.time.sleep(10 * 1e9); }