From e1f0a4d0749d538319c03512599fc4e813166bed Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Tue, 7 Jan 2025 23:08:24 +0100 Subject: [PATCH] feat: add sample binding of shell_execute_log --- src/keybind/builtin/flow.json | 3 ++- src/shell.zig | 14 +++++++++++--- src/tui/mainview.zig | 13 +++++-------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/keybind/builtin/flow.json b/src/keybind/builtin/flow.json index af7dc10..1a83cb5 100644 --- a/src/keybind/builtin/flow.json +++ b/src/keybind/builtin/flow.json @@ -2,7 +2,8 @@ "project": { "press": [ ["f5", ["create_scratch_buffer", "*test*"], ["shell_execute_insert", "zig", "build", "test"]], - ["f7", ["create_scratch_buffer", "*build*"], ["shell_execute_insert", "zig", "build"]] + ["f7", ["create_scratch_buffer", "*build*"], ["shell_execute_insert", "zig", "build"]], + ["alt+d", ["shell_execute_log", "date"]] ] }, "normal": { diff --git a/src/shell.zig b/src/shell.zig index 7896a97..cdcde14 100644 --- a/src/shell.zig +++ b/src/shell.zig @@ -79,7 +79,7 @@ pub fn log_handler(parent: tp.pid_ref, arg0: []const u8, output: []const u8) voi _ = arg0; const logger = log.logger(@typeName(Self)); var it = std.mem.splitScalar(u8, output, '\n'); - while (it.next()) |line| logger.print("{s}", .{line}); + while (it.next()) |line| if (line.len > 0) logger.print("{s}", .{line}); } pub fn log_err_handler(parent: tp.pid_ref, arg0: []const u8, output: []const u8) void { @@ -99,6 +99,14 @@ pub fn log_exit_handler(parent: tp.pid_ref, arg0: []const u8, err_msg: []const u } } +pub fn log_exit_err_handler(parent: tp.pid_ref, arg0: []const u8, err_msg: []const u8, exit_code: i64) void { + _ = parent; + const logger = log.logger(@typeName(Self)); + if (exit_code > 0) { + logger.print_err(arg0, "'{s}' terminated {s} exitcode: {d}", .{ arg0, err_msg, exit_code }); + } +} + const Process = struct { allocator: std.mem.Allocator, arg0: [:0]const u8, @@ -187,11 +195,11 @@ const Process = struct { var err_msg: []const u8 = undefined; var exit_code: i64 = undefined; if (try m.match(.{ tp.any, tp.any, "exited", 0 })) { - self.logger.print("'{s}' exited ok", .{self.arg0}); + self.handlers.exit(self.parent.ref(), self.arg0, "exited", 0); } else if (try m.match(.{ tp.any, tp.any, "error.FileNotFound", 1 })) { self.logger.print_err(self.arg0, "'{s}' executable not found", .{self.arg0}); } else if (try m.match(.{ tp.any, tp.any, tp.extract(&err_msg), tp.extract(&exit_code) })) { - self.logger.print_err(self.arg0, "'{s}' terminated {s} exitcode: {d}", .{ self.arg0, err_msg, exit_code }); + self.handlers.exit(self.parent.ref(), self.arg0, err_msg, exit_code); } } }; diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index 299ed86..07737e9 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -607,14 +607,11 @@ const cmds = struct { if (!try ctx.args.match(.{ tp.string, tp.more })) return error.InvalidShellArgument; const cmd = ctx.args; - const handlers = struct { - fn out(_: tp.pid_ref, arg0: []const u8, output: []const u8) void { - const logger = log.logger(arg0); - var it = std.mem.splitScalar(u8, output, '\n'); - while (it.next()) |line| logger.print("{s}", .{std.fmt.fmtSliceEscapeLower(line)}); - } - }; - try shell.execute(self.allocator, cmd, .{ .out = handlers.out }); + try shell.execute(self.allocator, cmd, .{ + .out = shell.log_handler, + .err = shell.log_err_handler, + .exit = shell.log_exit_err_handler, + }); } pub const shell_execute_log_meta = .{ .arguments = &.{.string} };