forked from ptrcnull/bat-alert
more crimes
This commit is contained in:
parent
d483eb1bcd
commit
4e69ce3cc5
1 changed files with 11 additions and 15 deletions
26
src/main.zig
26
src/main.zig
|
@ -18,19 +18,13 @@ pub fn main() !void {
|
||||||
std.process.exit(1);
|
std.process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
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 target: u8 = 15;
|
||||||
var battery: []const u8 = "BAT0";
|
var battery: []const u8 = "BAT0";
|
||||||
|
|
||||||
|
var argsIter = std.process.args();
|
||||||
|
|
||||||
var parse_mode = ParseMode.none;
|
var parse_mode = ParseMode.none;
|
||||||
for (args, 0..) |arg, i| {
|
while (argsIter.next()) |arg| {
|
||||||
_ = i;
|
|
||||||
switch (parse_mode) {
|
switch (parse_mode) {
|
||||||
ParseMode.none => {
|
ParseMode.none => {
|
||||||
if (std.mem.eql(u8, arg, "--help")) {
|
if (std.mem.eql(u8, arg, "--help")) {
|
||||||
|
@ -72,20 +66,23 @@ pub fn main() !void {
|
||||||
}
|
}
|
||||||
|
|
||||||
var sent = false;
|
var sent = false;
|
||||||
const batteryPath = try std.fmt.allocPrint(allocator, "/sys/class/power_supply/{s}/capacity", .{battery});
|
var batBuf: [256]u8 = undefined;
|
||||||
|
var capBuf: [8]u8 = undefined;
|
||||||
|
|
||||||
|
const batteryPath = try std.fmt.bufPrint(&batBuf, "/sys/class/power_supply/{s}/capacity", .{battery});
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const capacityFile = std.fs.openFileAbsolute(batteryPath, .{}) catch |err| {
|
const capacityFile = std.fs.openFileAbsolute(batteryPath, .{}) catch |err| {
|
||||||
std.log.err("open {s}: {}", .{ batteryPath, err });
|
std.log.err("open {s}: {}", .{ batteryPath, err });
|
||||||
std.process.exit(1);
|
std.process.exit(1);
|
||||||
};
|
};
|
||||||
var capacityStr = std.ArrayList(u8).init(allocator);
|
_ = try capacityFile.reader().readAll(&capBuf);
|
||||||
try capacityFile.reader().streamUntilDelimiter(capacityStr.writer(), '\n', null);
|
|
||||||
capacityFile.close();
|
capacityFile.close();
|
||||||
|
var splat = std.mem.splitScalar(u8, &capBuf, '\n');
|
||||||
|
|
||||||
// std.log.info("battery: {s}", .{capacityStr.items});
|
// std.log.info("battery: {s}", .{splat.first()});
|
||||||
|
|
||||||
const capacity = try std.fmt.parseInt(u8, capacityStr.items, 10);
|
const capacity = try std.fmt.parseInt(u8, splat.first(), 10);
|
||||||
// std.log.info("battery (but parsed): {}", .{capacity});
|
// std.log.info("battery (but parsed): {}", .{capacity});
|
||||||
|
|
||||||
if (capacity <= target) {
|
if (capacity <= target) {
|
||||||
|
@ -108,7 +105,6 @@ pub fn main() !void {
|
||||||
} else {
|
} else {
|
||||||
sent = false;
|
sent = false;
|
||||||
}
|
}
|
||||||
capacityStr.deinit();
|
|
||||||
|
|
||||||
std.time.sleep(10 * 1e9);
|
std.time.sleep(10 * 1e9);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue