add loadavg module

This commit is contained in:
Jeeves 2024-03-16 10:15:04 -06:00
parent a16e0801ea
commit 99dbc6899e
2 changed files with 31 additions and 0 deletions

View file

@ -9,6 +9,7 @@ const display = @import("modules/display.zig");
const uptime = @import("modules/uptime.zig");
const volume = @import("modules/volume.zig");
const memory = @import("modules/memory.zig");
const loadavg = @import("modules/loadavg.zig");
const Module = @import("module.zig");
pub fn main() !void {
@ -24,6 +25,7 @@ pub fn main() !void {
var modules = [_]Module{
uptime.init(arena.allocator()).module,
loadavg.init(arena.allocator()).module,
memory.init(arena.allocator()).module,
volume.init(arena.allocator()).module,
display.init(arena.allocator()).module,

29
src/modules/loadavg.zig Normal file
View file

@ -0,0 +1,29 @@
const std = @import("std");
const Module = @import("../module.zig");
const Self = @This();
module: Module,
pub fn init(allocator: std.mem.Allocator) Self {
return .{
.module = .{
.allocator = allocator,
.getJsonFn = getJson,
},
};
}
pub fn getJson(module: *const Module) !Module.JSON {
const self = @fieldParentPtr(Self, "module", module);
var loadavg_file = try std.fs.openFileAbsolute("/proc/loadavg", .{});
defer loadavg_file.close();
const loadavg_string = try loadavg_file.reader().readAllAlloc(self.module.allocator, 64);
var loadavg_split = std.mem.splitScalar(u8, loadavg_string[0 .. loadavg_string.len - 1], ' ');
var loadavg = try std.fmt.parseFloat(f32, loadavg_split.first());
return .{
.full_text = try std.fmt.allocPrint(self.module.allocator, "{d:0>1.2}", .{loadavg}),
};
}