diff options
Diffstat (limited to 'src/eval.zig')
-rw-r--r-- | src/eval.zig | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/src/eval.zig b/src/eval.zig index bd5804d..3381302 100644 --- a/src/eval.zig +++ b/src/eval.zig @@ -25,6 +25,8 @@ const Redirect = syntax.Redirect; const cmdTypes = @import("cmd/lib/types.zig"); pub const CommandStatus = cmdTypes.CommandStatus; const OutputCapture = cmdTypes.OutputCapture; +const StdoutOutputCapture = cmdTypes.StdoutOutputCapture; +const OutputWriter = cmdTypes.OutputWriter; const InputSource = cmdTypes.InputSource; const CommandContext = cmdTypes.CommandContext; @@ -36,7 +38,13 @@ pub fn executeCommand(command: Command, allocator: Allocator) !CommandStatus { } pub fn executeCommandWithOutput(command: Command, allocator: Allocator, output_capture: ?*OutputCapture, input_source: ?*InputSource) !CommandStatus { - const ctx = CommandContext.init(allocator, output_capture, input_source, executeCommandWithOutput); + var stdout_capture = StdoutOutputCapture{}; + const output_writer = if (output_capture) |capture| + OutputWriter{ .capture = capture } + else + OutputWriter{ .stdout = &stdout_capture }; + + const ctx = CommandContext.init(allocator, output_writer, input_source, executeCommandWithOutput); switch (command) { .Empty => return CommandStatus{ .Code = 0 }, @@ -112,11 +120,8 @@ pub fn executeCommandWithOutput(command: Command, allocator: Allocator, output_c else => { const error_msg = try std.fmt.allocPrint(ctx.allocator, "Command not implemented: {any}\n", .{builtin_cmd}); defer ctx.allocator.free(error_msg); - if (ctx.output_capture) |capture| { - try capture.write(error_msg); - } else { - print("{s}", .{error_msg}); - } + var writer = ctx.output_writer; + try writer.write(error_msg); return CommandStatus{ .Code = 1 }; }, } |