This commit is contained in:
RadsammyT 2026-03-16 22:42:41 -04:00
commit 1031ba08ab
No known key found for this signature in database
5 changed files with 29 additions and 14 deletions

View file

@ -9,9 +9,11 @@ pub const customCommandBind = struct {
posY: u32 = 0,
};
pub const UNDEFINED_CMD: []const u8 = "echo \"You forgot to define 'cmd'!\"";
pub const customCommandInfo = struct {
cmd: []const u8 = undefined,
/// SET TO LABELS WIDGET ID!!!
cmd: []const u8 = UNDEFINED_CMD,
/// SET TO LABELS LID!!!
id: u64 = 0,
/// In milliseconds, the refresh rate for the `cmd` to run again
@ -20,6 +22,7 @@ pub const customCommandInfo = struct {
counter: u32 = 0,
posX: u32 = 0,
posY: u32 = 0,
leftAlign: bool = false,
};
pub var binds: std.StringHashMap(customCommandBind) = undefined;

View file

@ -214,8 +214,11 @@ pub fn configFieldHandler(_: std.mem.Allocator, field: ini.IniField) ?ini.IniFie
}
if (std.mem.eql(u8, field.key, "refresh")) {
label.refresh = std.fmt.parseInt(u32, field.value, 10) catch blk: { break :blk 0; };
label.refresh += 1;
}
if (std.mem.eql(u8, field.key, "leftAlign")) {
label.leftAlign = if (std.mem.eql(u8, field.value, "true")) true else false;
}
}
}

View file

@ -239,7 +239,8 @@ pub fn main() !void {
var labelIter = CustomCommands.labels.iterator();
while (labelIter.next()) |i| {
temporary_allocator.free(i.key_ptr.*);
temporary_allocator.free(i.value_ptr.*.cmd);
if (i.value_ptr.cmd.ptr != CustomCommands.UNDEFINED_CMD.ptr)
temporary_allocator.free(i.value_ptr.*.cmd);
}
CustomCommands.labels.deinit();
}
@ -1055,14 +1056,14 @@ pub fn main() !void {
var lblIter = CustomCommands.labels.iterator();
while (lblIter.next()) |i| {
var w = Label.init("",
const w = Label.init("",
null,
state.buffer.fg,
state.buffer.bg,
&updateCustomInfo,
null,
);
i.value_ptr.id = w.widget().id;
i.value_ptr.id = w.lid;
i.value_ptr.counter = 2;
try state.custom_info.append(state.allocator, .{
.info = i.value_ptr.*,
@ -1714,7 +1715,7 @@ fn updateClock(self: *Label, ptr: *anyopaque) !void {
fn updateCustomInfo(lbl: *Label, ptr: *anyopaque) !void {
const state: *UiState = @ptrCast(@alignCast(ptr));
const wid = lbl.widget().id;
const wid = lbl.lid;
var stdout = std.ArrayList(u8).empty;
defer stdout.deinit(state.allocator);
var stderr = std.ArrayList(u8).empty;
@ -1732,14 +1733,18 @@ fn updateCustomInfo(lbl: *Label, ptr: *anyopaque) !void {
try c.spawn();
try c.collectOutput(state.allocator, &stdout, &stderr, 128);
_ = try c.wait();
if (stdout.items.len != 0 and !std.ascii.isPrint(stdout.items[stdout.items.len-1]))
_ = stdout.pop()
else if (stdout.items.len == 0)
try stdout.appendSlice(state.allocator, "Something went wrong!");
state.allocator.free(lbl.text);
if (std.ascii.isAscii(stdout.items[stdout.items.len-1])) _ = stdout.pop();
lbl.text = try state.allocator.dupe(u8, stdout.items);
try positionWidgets(state);
if (i.info.refresh != 0)
i.info.counter = i.info.refresh;
break;
}
i.info.counter -= 1;
if (i.info.counter != 0)
i.info.counter -= 1;
}
}
@ -1830,7 +1835,9 @@ fn positionWidgets(ptr: *anyopaque) !void {
for (state.custom_info.items) |*item| {
item.lbl.positionXY(state.edge_margin
.addX(@intCast(item.info.posX))
.addY(@intCast(item.info.posY)));
.addY(@intCast(item.info.posY))
.invertXIf(state.buffer.width, item.info.leftAlign)
.removeXIf(item.lbl.text.len, item.info.leftAlign));
}
}

View file

@ -8,6 +8,7 @@ const Position = @import("../Position.zig");
const TerminalBuffer = @import("../TerminalBuffer.zig");
const Widget = @import("../Widget.zig");
lid: u64, // HACK: we need to differenciate between labels in `updateCustomInfo`.
allocator: ?Allocator,
text: []const u8,
max_width: ?usize,
@ -18,6 +19,8 @@ calculate_timeout_fn: ?*const fn (*Label, *anyopaque) anyerror!?usize,
component_pos: Position,
children_pos: Position,
pub var lidCounter: u64 = 0;
pub fn init(
text: []const u8,
max_width: ?usize,
@ -26,7 +29,9 @@ pub fn init(
update_fn: ?*const fn (*Label, *anyopaque) anyerror!void,
calculate_timeout_fn: ?*const fn (*Label, *anyopaque) anyerror!?usize,
) Label {
lidCounter += 1;
return .{
.lid = lidCounter,
.allocator = null,
.text = text,
.max_width = max_width,

View file

@ -64,19 +64,16 @@ pub fn CyclableLabel(comptime ItemType: type, comptime ChangeItemType: type) typ
pub fn positionX(self: *Self, original_pos: Position) void {
self.component_pos = original_pos;
self.cursor = self.component_pos.x + 2;
self.children_pos = original_pos.addX(self.width);
}
pub fn positionY(self: *Self, original_pos: Position) void {
self.component_pos = original_pos;
self.cursor = self.component_pos.x + 2;
self.children_pos = original_pos.addY(1);
}
pub fn positionXY(self: *Self, original_pos: Position) void {
self.component_pos = original_pos;
self.cursor = self.component_pos.x + 2;
self.children_pos = Position.init(
self.width,
1,