feat: add sample binding of shell_execute_log

This commit is contained in:
CJ van den Berg 2025-01-07 23:08:24 +01:00
parent cfc99b61dc
commit e1f0a4d074
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 18 additions and 12 deletions

View file

@ -2,7 +2,8 @@
"project": { "project": {
"press": [ "press": [
["f5", ["create_scratch_buffer", "*test*"], ["shell_execute_insert", "zig", "build", "test"]], ["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": { "normal": {

View file

@ -79,7 +79,7 @@ pub fn log_handler(parent: tp.pid_ref, arg0: []const u8, output: []const u8) voi
_ = arg0; _ = arg0;
const logger = log.logger(@typeName(Self)); const logger = log.logger(@typeName(Self));
var it = std.mem.splitScalar(u8, output, '\n'); 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 { 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 { const Process = struct {
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
arg0: [:0]const u8, arg0: [:0]const u8,
@ -187,11 +195,11 @@ const Process = struct {
var err_msg: []const u8 = undefined; var err_msg: []const u8 = undefined;
var exit_code: i64 = undefined; var exit_code: i64 = undefined;
if (try m.match(.{ tp.any, tp.any, "exited", 0 })) { 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 })) { } else if (try m.match(.{ tp.any, tp.any, "error.FileNotFound", 1 })) {
self.logger.print_err(self.arg0, "'{s}' executable not found", .{self.arg0}); 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) })) { } 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);
} }
} }
}; };

View file

@ -607,14 +607,11 @@ const cmds = struct {
if (!try ctx.args.match(.{ tp.string, tp.more })) if (!try ctx.args.match(.{ tp.string, tp.more }))
return error.InvalidShellArgument; return error.InvalidShellArgument;
const cmd = ctx.args; const cmd = ctx.args;
const handlers = struct { try shell.execute(self.allocator, cmd, .{
fn out(_: tp.pid_ref, arg0: []const u8, output: []const u8) void { .out = shell.log_handler,
const logger = log.logger(arg0); .err = shell.log_err_handler,
var it = std.mem.splitScalar(u8, output, '\n'); .exit = shell.log_exit_err_handler,
while (it.next()) |line| logger.print("{s}", .{std.fmt.fmtSliceEscapeLower(line)}); });
}
};
try shell.execute(self.allocator, cmd, .{ .out = handlers.out });
} }
pub const shell_execute_log_meta = .{ .arguments = &.{.string} }; pub const shell_execute_log_meta = .{ .arguments = &.{.string} };