forked from ptrcnull/bat-alert
feat: allow specifying battery name
This commit is contained in:
parent
4e80ad4617
commit
f5101f8915
1 changed files with 13 additions and 1 deletions
14
src/main.zig
14
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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue