mirror of
https://codeberg.org/fairyglade/ly.git
synced 2026-08-09 15:19:13 +02:00
zig fmt run
This commit is contained in:
parent
bf678017ff
commit
b704640a85
3 changed files with 22 additions and 63 deletions
|
|
@ -7,13 +7,13 @@ pub const CustomCommandBind = struct {
|
|||
cmd: []const u8 = "",
|
||||
};
|
||||
|
||||
pub const UNDEFINED_CMD: []const u8 = "echo \"You forgot to define 'cmd'!\"";
|
||||
pub const UNDEFINED_CMD: []const u8 = "echo \"You forgot to define 'cmd'!\"";
|
||||
|
||||
pub const CustomCommandInfo = struct {
|
||||
name: []const u8 = "",
|
||||
cmd: ?[]const u8 = null,
|
||||
/// To be set to the label's widget ID
|
||||
id: u64 = 0,
|
||||
/// To be set to the label's widget ID
|
||||
id: u64 = 0,
|
||||
|
||||
/// In frames, the refresh rate for the `cmd` to run again
|
||||
/// If 0, only run once.
|
||||
|
|
|
|||
|
|
@ -174,7 +174,7 @@ pub fn configFieldHandler(_: std.mem.Allocator, field: ini.IniField) ?ini.IniFie
|
|||
command.name = temporary_allocator.dupe(u8, field.value) catch "";
|
||||
}
|
||||
if (std.mem.eql(u8, field.key, "cmd")) {
|
||||
command.cmd = temporary_allocator.dupe(u8, field.value) catch "";
|
||||
command.cmd = temporary_allocator.dupe(u8, field.value) catch "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -183,9 +183,7 @@ pub fn configFieldHandler(_: std.mem.Allocator, field: ini.IniField) ?ini.IniFie
|
|||
const key = field.header["lbl:".len..];
|
||||
const keyZ = temporary_allocator.dupe(u8, key) catch "";
|
||||
if (!CustomCommands.labels.contains(keyZ)) {
|
||||
CustomCommands.labels.put(keyZ, .{
|
||||
.name = keyZ
|
||||
}) catch {};
|
||||
CustomCommands.labels.put(keyZ, .{ .name = keyZ }) catch {};
|
||||
}
|
||||
if (CustomCommands.labels.getPtr(keyZ)) |label| {
|
||||
if (std.mem.eql(u8, field.key, "cmd")) {
|
||||
|
|
|
|||
73
src/main.zig
73
src/main.zig
|
|
@ -64,17 +64,9 @@ fn ttyControlTransferSignalHandler(_: c_int) callconv(.c) void {
|
|||
TerminalBuffer.shutdown();
|
||||
}
|
||||
|
||||
const CustomBindLabel = struct {
|
||||
cmd: custom.CustomCommandBind,
|
||||
key: []const u8,
|
||||
lbl: Label
|
||||
};
|
||||
|
||||
const CustomInfoLabel = struct {
|
||||
info: custom.CustomCommandInfo,
|
||||
lbl: Label
|
||||
};
|
||||
const CustomBindLabel = struct { cmd: custom.CustomCommandBind, key: []const u8, lbl: Label };
|
||||
|
||||
const CustomInfoLabel = struct { info: custom.CustomCommandInfo, lbl: Label };
|
||||
|
||||
const UiState = struct {
|
||||
allocator: Allocator,
|
||||
|
|
@ -1084,18 +1076,8 @@ pub fn main() !void {
|
|||
|
||||
var lblIter = custom.labels.iterator();
|
||||
while (lblIter.next()) |i| {
|
||||
try state.custom_info.append(state.allocator, .{
|
||||
.info = i.value_ptr.*,
|
||||
.lbl = .init(
|
||||
"",
|
||||
null,
|
||||
state.buffer.fg,
|
||||
state.buffer.bg,
|
||||
&updateCustomInfo,
|
||||
null
|
||||
)
|
||||
});
|
||||
var latest = &state.custom_info.items[state.custom_info.items.len-1];
|
||||
try state.custom_info.append(state.allocator, .{ .info = i.value_ptr.*, .lbl = .init("", null, state.buffer.fg, state.buffer.bg, &updateCustomInfo, null) });
|
||||
var latest = &state.custom_info.items[state.custom_info.items.len - 1];
|
||||
latest.info.id = latest.lbl.widget().id;
|
||||
latest.info.counter = 2;
|
||||
}
|
||||
|
|
@ -1105,17 +1087,11 @@ pub fn main() !void {
|
|||
item.lbl.deinit();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var iter = custom.binds.iterator();
|
||||
while (iter.next()) |i| {
|
||||
var concat = try std.mem.concat(state.allocator, u8,
|
||||
&[_][]const u8{
|
||||
i.key_ptr.*,
|
||||
" ",
|
||||
i.value_ptr.name
|
||||
}
|
||||
);
|
||||
inline for(@typeInfo(Lang).@"struct".fields) |lang_key| {
|
||||
var concat = try std.mem.concat(state.allocator, u8, &[_][]const u8{ i.key_ptr.*, " ", i.value_ptr.name });
|
||||
inline for (@typeInfo(Lang).@"struct".fields) |lang_key| {
|
||||
const new = try std.mem.replaceOwned(u8, state.allocator, concat, "$" ++ lang_key.name, @field(state.lang, lang_key.name));
|
||||
state.allocator.free(concat);
|
||||
concat = new;
|
||||
|
|
@ -1132,7 +1108,7 @@ pub fn main() !void {
|
|||
.cmd = i.value_ptr.*,
|
||||
.key = i.key_ptr.*,
|
||||
});
|
||||
state.custom_binds.items[state.custom_binds.items.len-1].lbl.allocator = state.allocator;
|
||||
state.custom_binds.items[state.custom_binds.items.len - 1].lbl.allocator = state.allocator;
|
||||
}
|
||||
defer {
|
||||
for (state.custom_binds.items) |*i| {
|
||||
|
|
@ -1140,7 +1116,6 @@ pub fn main() !void {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
if (!state.config.hide_key_hints) {
|
||||
try layer2.append(state.allocator, state.shutdown_label.widget());
|
||||
try layer2.append(state.allocator, state.restart_label.widget());
|
||||
|
|
@ -1182,7 +1157,7 @@ pub fn main() !void {
|
|||
if (!state.config.hide_version_string) {
|
||||
try layer2.append(state.allocator, state.version_label.widget());
|
||||
}
|
||||
|
||||
|
||||
for (state.custom_binds.items) |*item| {
|
||||
try layer2.append(state.allocator, item.lbl.widget());
|
||||
}
|
||||
|
|
@ -1364,11 +1339,7 @@ fn quit(ptr: *anyopaque) !bool {
|
|||
|
||||
fn customCommand(ptr: *anyopaque) !bool {
|
||||
const lbl: *CustomBindLabel = @ptrCast(@alignCast(ptr));
|
||||
var proc = std.process.Child.init(&[_][]const u8 {
|
||||
"/bin/sh",
|
||||
"-c",
|
||||
lbl.cmd.cmd
|
||||
}, lbl.lbl.allocator.?);
|
||||
var proc = std.process.Child.init(&[_][]const u8{ "/bin/sh", "-c", lbl.cmd.cmd }, lbl.lbl.allocator.?);
|
||||
proc.stdout_behavior = .Ignore;
|
||||
proc.stderr_behavior = .Ignore;
|
||||
const res = proc.spawnAndWait() catch return false;
|
||||
|
|
@ -1773,21 +1744,16 @@ fn updateCustomInfo(lbl: *Label, ptr: *anyopaque) !void {
|
|||
if (i.info.id != wid) continue;
|
||||
// Here, a counter ticks down every time `updateCustomInfo` runs on that
|
||||
// particular label. It will only run the command and update the label
|
||||
// once it reaches to 1. If a refresh value is defined it's then reset to
|
||||
// once it reaches to 1. If a refresh value is defined it's then reset to
|
||||
// that refresh value.
|
||||
if (i.info.counter == 1) {
|
||||
var c = std.process.Child.init(&[_][]const u8 {
|
||||
"/bin/sh",
|
||||
"-c",
|
||||
i.info.cmd orelse custom.UNDEFINED_CMD
|
||||
}, state.allocator);
|
||||
var c = std.process.Child.init(&[_][]const u8{ "/bin/sh", "-c", i.info.cmd orelse custom.UNDEFINED_CMD }, state.allocator);
|
||||
c.stderr_behavior = .Pipe;
|
||||
c.stdout_behavior = .Pipe;
|
||||
try c.spawn();
|
||||
|
||||
|
||||
c.collectOutput(state.allocator, &stdout, &stderr, state.buffer.width) catch {
|
||||
try stdout.print(state.allocator, "{s}: [{s}]", .{i.info.name, state.lang.custom_info_err_output_long});
|
||||
try stdout.print(state.allocator, "{s}: [{s}]", .{ i.info.name, state.lang.custom_info_err_output_long });
|
||||
};
|
||||
|
||||
const newlineIdx = std.mem.indexOfAny(u8, stdout.items, "\n");
|
||||
|
|
@ -1797,7 +1763,7 @@ fn updateCustomInfo(lbl: *Label, ptr: *anyopaque) !void {
|
|||
|
||||
if (stdout.items.len > state.buffer.width) {
|
||||
stdout.clearRetainingCapacity();
|
||||
try stdout.print(state.allocator, "{s}: [{s}]", .{i.info.name, state.lang.custom_info_err_output_long});
|
||||
try stdout.print(state.allocator, "{s}: [{s}]", .{ i.info.name, state.lang.custom_info_err_output_long });
|
||||
}
|
||||
|
||||
_ = try c.wait();
|
||||
|
|
@ -1805,14 +1771,10 @@ fn updateCustomInfo(lbl: *Label, ptr: *anyopaque) !void {
|
|||
// Sometimes, the output of a command would have an unprintable character at
|
||||
// the end of its output, causing '<27>' to appear in its place. Here, we check
|
||||
// if this is the case and remove it.
|
||||
if (stdout.items.len != 0 and !std.ascii.isPrint(stdout.items[stdout.items.len-1])) {
|
||||
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.print(state.allocator, "{s}: [{s}{s}]", .{
|
||||
i.info.name,
|
||||
state.lang.custom_info_err_no_output,
|
||||
if (stderr.items.len > 0) state.lang.custom_info_err_no_output_error else ""
|
||||
});
|
||||
try stdout.print(state.allocator, "{s}: [{s}{s}]", .{ i.info.name, state.lang.custom_info_err_no_output, if (stderr.items.len > 0) state.lang.custom_info_err_no_output_error else "" });
|
||||
}
|
||||
state.allocator.free(lbl.text);
|
||||
lbl.text = try state.allocator.dupe(u8, stdout.items);
|
||||
|
|
@ -1824,9 +1786,8 @@ fn updateCustomInfo(lbl: *Label, ptr: *anyopaque) !void {
|
|||
}
|
||||
if (i.info.counter != 0)
|
||||
i.info.counter -= 1;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn calculateClockTimeout(_: *Label, _: *anyopaque) !?usize {
|
||||
const time = try interop.getTimeOfDay();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue