feat: allow specifying battery name

This commit is contained in:
ptrcnull 2023-11-02 21:02:14 +01:00
parent 4e80ad4617
commit f5101f8915
Signed by untrusted user: ptrcnull
GPG key ID: 411F7B30801DD9CA

View file

@ -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);
}