const std = @import("std"); pub const std_options = struct { pub const log_level = .info; }; pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); const allocator = gpa.allocator(); const args = try std.process.argsAlloc(allocator); 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")) { const stdout = std.io.getStdOut().writer(); try stdout.print( \\Usage: bat-alert [options] \\ \\Options: \\ -t, --target NUM Target battery level \\ , .{}); std.process.exit(0); } if (std.mem.eql(u8, arg, "--target") or std.mem.eql(u8, arg, "-t")) { if (i + 1 == args.len) { std.log.err("--target requires an argument", .{}); std.process.exit(1); } const parsedTarget = std.fmt.parseInt(u8, args[i + 1], 10) catch |err| { std.log.err("invalid target '{s}': {}", .{ args[i + 1], err }); std.process.exit(1); }; 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(batteryPath, .{}); var capacityStr = std.ArrayList(u8).init(allocator); try capacityFile.reader().streamUntilDelimiter(capacityStr.writer(), '\n', null); capacityFile.close(); // std.log.info("battery: {s}", .{capacityStr.items}); const capacity = try std.fmt.parseInt(u8, capacityStr.items, 10); // std.log.info("battery (but parsed): {}", .{capacity}); if (capacity <= target) { if (!sent) { sent = true; _ = try std.process.Child.exec(.{ .allocator = allocator, .argv = &[_][]const u8{ "notify-send", "-t", "5000", "-u", "critical", "bat-alert", "battery running low!" }, }); std.log.info("[NOTIFY] battery running low!", .{}); } } else { sent = false; } capacityStr.deinit(); std.time.sleep(10 * 1e9); } }