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": {
"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": {

View file

@ -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);
}
}
};

View file

@ -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} };