diff options
author | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2025-08-16 12:50:23 +0200 |
---|---|---|
committer | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2025-08-16 12:50:23 +0200 |
commit | 9edc1338b92d1ead4c5f2fad8c0516037963f7b6 (patch) | |
tree | 64f3ed617771f9327bd49e3732adb9271e3559cc /src/cmd/lib | |
parent | 754a74da6052c9f5e8dc4a536c58ccc11cb66369 (diff) |
Unify output handling.
Now every command always uses an OutputWriter instead of conditionally
writing directly to stdout using std.debug.print.
Diffstat (limited to 'src/cmd/lib')
-rw-r--r-- | src/cmd/lib/types.zig | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/src/cmd/lib/types.zig b/src/cmd/lib/types.zig index c1ce3c7..f663a64 100644 --- a/src/cmd/lib/types.zig +++ b/src/cmd/lib/types.zig @@ -32,6 +32,13 @@ pub const OutputCapture = struct { } }; +pub const StdoutOutputCapture = struct { + pub fn write(self: *StdoutOutputCapture, data: []const u8) !void { + _ = self; + try std.io.getStdOut().writeAll(data); + } +}; + pub const InputSource = struct { data: []const u8, position: usize, @@ -65,18 +72,30 @@ pub const InputSource = struct { } }; +pub const OutputWriter = union(enum) { + capture: *OutputCapture, + stdout: *StdoutOutputCapture, + + pub fn write(self: *OutputWriter, data: []const u8) !void { + switch (self.*) { + .capture => |capture| try capture.write(data), + .stdout => |stdout| try stdout.write(data), + } + } +}; + pub const ExecuteCommandFn = *const fn (Command, Allocator, ?*OutputCapture, ?*InputSource) anyerror!CommandStatus; pub const CommandContext = struct { allocator: Allocator, - output_capture: ?*OutputCapture, + output_writer: OutputWriter, input_source: ?*InputSource, execute_command: ExecuteCommandFn, - pub fn init(allocator: Allocator, output_capture: ?*OutputCapture, input_source: ?*InputSource, execute_command: ExecuteCommandFn) CommandContext { + pub fn init(allocator: Allocator, output_writer: OutputWriter, input_source: ?*InputSource, execute_command: ExecuteCommandFn) CommandContext { return CommandContext{ .allocator = allocator, - .output_capture = output_capture, + .output_writer = output_writer, .input_source = input_source, .execute_command = execute_command, }; |