implement colors (almost) properly and clean up code

This commit is contained in:
Jeeves 2024-07-22 18:54:10 -06:00
parent 0f42a3e01e
commit e07cf7c353
9 changed files with 150 additions and 103 deletions

View file

@ -4,17 +4,16 @@ const Self = @This();
module: Module,
pub fn init(allocator: std.mem.Allocator) Self {
pub fn init(allocator: std.mem.Allocator) !Self {
_ = allocator;
return .{
.module = .{
.allocator = allocator,
.getJsonFn = getJson,
},
.module = .{ .getBlockFn = getBlock },
};
}
pub fn getJson(module: *const Module) !Module.JSON {
const self: *const Self = @fieldParentPtr("module", module);
pub fn getBlock(module: *Module, allocator: std.mem.Allocator) !Module.Block {
_ = module;
// const self: *const Self = @fieldParentPtr("module", module);
var energy_full_file = try std.fs.openFileAbsolute("/sys/class/power_supply/BAT0/energy_full", .{});
defer energy_full_file.close();
@ -23,9 +22,9 @@ pub fn getJson(module: *const Module) !Module.JSON {
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 energy_now_string = try energy_now_file.reader().readAllAlloc(self.module.allocator, 32);
const status_string = try status_file.reader().readAllAlloc(self.module.allocator, 32);
const energy_full_string = try energy_full_file.reader().readAllAlloc(allocator, 32);
const energy_now_string = try energy_now_file.reader().readAllAlloc(allocator, 32);
const status_string = try status_file.reader().readAllAlloc(allocator, 32);
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);
@ -40,6 +39,6 @@ pub fn getJson(module: *const Module) !Module.JSON {
const percent_left = @as(f32, @floatFromInt(energy_now)) / @as(f32, @floatFromInt(energy_full)) * 100;
return .{
.full_text = try std.fmt.allocPrint(self.module.allocator, "{s} {d:.2}%", .{ status, percent_left }),
.full_text = try std.fmt.allocPrint(allocator, "{s} {d:.2}%", .{ status, percent_left }),
};
}