diff --git a/src/main.zig b/src/main.zig index 60e71af..1ff61b7 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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, diff --git a/src/modules/loadavg.zig b/src/modules/loadavg.zig new file mode 100644 index 0000000..0eb65b2 --- /dev/null +++ b/src/modules/loadavg.zig @@ -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}), + }; +}