Adding the battery status for the top bar alongside brightness controls (closes #821) (#826)

Reviewed-on: https://codeberg.org/fairyglade/ly/pulls/826
Reviewed-by: AnErrupTion <anerruption@disroot.org>
Co-authored-by: ebits <tspamiitesh@gmail.com>
Co-committed-by: ebits <tspamiitesh@gmail.com>
This commit is contained in:
ebits
2025-10-09 18:48:46 +02:00
committed by AnErrupTion
parent 44faa263b1
commit 339e39d496
23 changed files with 57 additions and 0 deletions

View File

@@ -49,6 +49,7 @@ gameoflife_initial_density: f32 = 0.4,
hide_borders: bool = false,
hide_version_string: bool = false,
hide_key_hints: bool = false,
battery_id: ?[]const u8 = "BAT0",
initial_info_text: ?[]const u8 = null,
input_len: u8 = 34,
lang: []const u8 = "en",

View File

@@ -47,6 +47,7 @@ err_perm_group: []const u8 = "failed to downgrade group permissions",
err_perm_user: []const u8 = "failed to downgrade user permissions",
err_pwnam: []const u8 = "failed to get user info",
err_sleep: []const u8 = "failed to execute sleep command",
err_battery: []const u8 = "failed to load battery status",
err_switch_tty: []const u8 = "failed to switch tty",
err_tty_ctrl: []const u8 = "tty control transfer failed",
err_no_users: []const u8 = "no users found",

View File

@@ -572,6 +572,22 @@ pub fn main() !void {
length += ly_top_str.len + 1;
}
var battery_bar_shown = false;
if (config.battery_id) |id| draw_battery: {
const battery_percentage = getBatteryPercentage(id) catch |err| {
try log_writer.print("failed to get battery percentage: {s}\n", .{@errorName(err)});
try info_line.addMessage(lang.err_battery, config.error_bg, config.error_fg);
break :draw_battery;
};
var battery_buf: [16:0]u8 = undefined;
const battery_str = std.fmt.bufPrintZ(&battery_buf, "BAT: {d}%", .{battery_percentage}) catch break :draw_battery;
const battery_y: usize = 1;
buffer.drawLabel(battery_str, 0, battery_y);
battery_bar_shown = true;
}
if (config.bigclock != .none and buffer.box_height + (bigclock.HEIGHT + 2) * 2 < buffer.height) {
var format_buf: [16:0]u8 = undefined;
var clock_buf: [32:0]u8 = undefined;
@@ -1162,6 +1178,22 @@ fn adjustBrightness(allocator: std.mem.Allocator, cmd: []const u8) !void {
}
}
fn getBatteryPercentage(battery_id: []const u8) !u8 {
const path = try std.fmt.allocPrint(temporary_allocator, "/sys/class/power_supply/{s}/capacity", .{battery_id});
defer temporary_allocator.free(path);
const battery_file = try std.fs.cwd().openFile(path, .{});
defer battery_file.close();
var buffer: [8]u8 = undefined;
const bytes_read = try battery_file.read(&buffer);
const capacity_str = buffer[0..bytes_read];
const trimmed = std.mem.trimRight(u8, capacity_str, "\n\r");
return try std.fmt.parseInt(u8, trimmed, 10);
}
fn getAuthErrorMsg(err: anyerror, lang: Lang) []const u8 {
return switch (err) {
error.GetPasswordNameFailed => lang.err_pwnam,