add uptime module
This commit is contained in:
parent
4778b5b77a
commit
df38a714e1
2 changed files with 16 additions and 34 deletions
16
src/main.zig
16
src/main.zig
|
@ -20,13 +20,11 @@ pub fn main() !void {
|
||||||
try stdout.print("{{\"version\":1}}\n[\n", .{});
|
try stdout.print("{{\"version\":1}}\n[\n", .{});
|
||||||
try bw.flush();
|
try bw.flush();
|
||||||
|
|
||||||
var _display = display.init(arena.allocator());
|
|
||||||
var _battery = battery.init(arena.allocator());
|
|
||||||
var _calendar = calendar.init(arena.allocator());
|
|
||||||
var modules = [_]Module{
|
var modules = [_]Module{
|
||||||
_display.module,
|
uptime.init(arena.allocator()).module,
|
||||||
_battery.module,
|
display.init(arena.allocator()).module,
|
||||||
_calendar.module,
|
battery.init(arena.allocator()).module,
|
||||||
|
calendar.init(arena.allocator()).module,
|
||||||
};
|
};
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
@ -35,12 +33,6 @@ pub fn main() !void {
|
||||||
try json.stringify(try module.getJson(), .{ .emit_null_optional_fields = false }, stdout);
|
try json.stringify(try module.getJson(), .{ .emit_null_optional_fields = false }, stdout);
|
||||||
try stdout.print(",", .{});
|
try stdout.print(",", .{});
|
||||||
}
|
}
|
||||||
// try stdout.print("[", .{});
|
|
||||||
// try json.stringify(battery_json, .{ .emit_null_optional_fields = false }, stdout);
|
|
||||||
// try stdout.print(",", .{});
|
|
||||||
// try json.stringify(calendar_json, .{ .emit_null_optional_fields = false }, stdout);
|
|
||||||
// try stdout.print(",", .{});
|
|
||||||
// try stdout.print("{{\"full_text\":\"test\"}}", .{});
|
|
||||||
try stdout.print("],\n", .{});
|
try stdout.print("],\n", .{});
|
||||||
|
|
||||||
try bw.flush();
|
try bw.flush();
|
||||||
|
|
|
@ -4,7 +4,7 @@ const Self = @This();
|
||||||
|
|
||||||
module: Module,
|
module: Module,
|
||||||
|
|
||||||
pub fn init(allocator: std.mem.Allocator) Module {
|
pub fn init(allocator: std.mem.Allocator) Self {
|
||||||
return .{
|
return .{
|
||||||
.module = .{
|
.module = .{
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
|
@ -16,30 +16,20 @@ pub fn init(allocator: std.mem.Allocator) Module {
|
||||||
pub fn getJson(module: *const Module) !Module.JSON {
|
pub fn getJson(module: *const Module) !Module.JSON {
|
||||||
const self = @fieldParentPtr(Self, "module", module);
|
const self = @fieldParentPtr(Self, "module", module);
|
||||||
|
|
||||||
var energy_full_file = try std.fs.openFileAbsolute("/sys/class/power_supply/BAT0/energy_full", .{});
|
var uptime_file = try std.fs.openFileAbsolute("/proc/uptime", .{});
|
||||||
defer energy_full_file.close();
|
defer uptime_file.close();
|
||||||
var energy_now_file = try std.fs.openFileAbsolute("/sys/class/power_supply/BAT0/energy_now", .{});
|
|
||||||
defer energy_now_file.close();
|
|
||||||
var status_file = try std.fs.openFileAbsolute("/sys/class/power_supply/BAT0/status", .{});
|
|
||||||
defer status_file.close();
|
|
||||||
|
|
||||||
const energy_full_string = try energy_full_file.reader().readAllAlloc(self.module.allocator, 32);
|
const uptime_string = try uptime_file.reader().readAllAlloc(self.module.allocator, 256);
|
||||||
const energy_now_string = try energy_now_file.reader().readAllAlloc(self.module.allocator, 32);
|
var uptime_split = std.mem.splitScalar(u8, uptime_string[0 .. uptime_string.len - 1], ' ');
|
||||||
const status_string = try status_file.reader().readAllAlloc(self.module.allocator, 32);
|
var uptime = try std.fmt.parseFloat(f32, uptime_split.first());
|
||||||
const energy_full = try std.fmt.parseInt(u32, energy_full_string[0 .. energy_full_string.len - 1], 10);
|
|
||||||
const energy_now = try std.fmt.parseInt(u32, energy_now_string[0 .. energy_now_string.len - 1], 10);
|
|
||||||
|
|
||||||
const status = if (std.mem.eql(u8, status_string[0 .. status_string.len - 1], "Full"))
|
const days = uptime / std.time.s_per_day;
|
||||||
"="
|
uptime -= std.time.s_per_day * @floor(days);
|
||||||
else if (std.mem.eql(u8, status_string[0 .. status_string.len - 1], "Discharging"))
|
const hours = uptime / std.time.s_per_hour;
|
||||||
"v"
|
uptime -= std.time.s_per_hour * @floor(hours);
|
||||||
else if (std.mem.eql(u8, status_string[0 .. status_string.len - 1], "Charging"))
|
const mins = uptime / std.time.s_per_min;
|
||||||
"^"
|
|
||||||
else
|
|
||||||
"?";
|
|
||||||
const percent_left = @as(f32, @floatFromInt(energy_now)) / @as(f32, @floatFromInt(energy_full)) * 100;
|
|
||||||
|
|
||||||
return .{
|
return .{
|
||||||
.full_text = try std.fmt.allocPrint(self.module.allocator, "{s} {d:.2}%", .{ status, percent_left }),
|
.full_text = try std.fmt.allocPrint(self.module.allocator, "{d:0>1.0}d {d:0>1.0}h {d:.0}m", .{ days, hours, mins }),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue