bat-alert/src/main.zig

73 lines
2.3 KiB
Zig
Raw Normal View History

2023-10-10 18:03:39 +00:00
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;
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;
}
}
var sent = false;
while (true) {
const capacityFile = try std.fs.openFileAbsolute("/sys/class/power_supply/BAT0/capacity", .{});
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;
}
std.time.sleep(10 * 1e9);
}
}