summaryrefslogtreecommitdiff
path: root/src/cmd/external.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/external.zig')
-rw-r--r--src/cmd/external.zig47
1 files changed, 20 insertions, 27 deletions
diff --git a/src/cmd/external.zig b/src/cmd/external.zig
index 68fc758..0f4e65f 100644
--- a/src/cmd/external.zig
+++ b/src/cmd/external.zig
@@ -1,7 +1,6 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
-const print = std.debug.print;
const types = @import("./lib/types.zig");
const CommandStatus = types.CommandStatus;
@@ -15,8 +14,13 @@ pub const External = struct {
pub fn eval(external: External, ctx: CommandContext) !CommandStatus {
const allocator = ctx.allocator;
- const output_capture = ctx.output_capture;
const input_source = ctx.input_source;
+
+ // Check if we need to capture output (not going to stdout)
+ const needs_capture = switch (ctx.output_writer) {
+ .stdout => false,
+ .capture => true,
+ };
// Try to execute external command
var child_args = ArrayList([]const u8).init(allocator);
defer child_args.deinit();
@@ -30,8 +34,8 @@ pub const External = struct {
// Set up pipes for capturing output
child.stdin_behavior = if (input_source != null) .Pipe else .Inherit;
- child.stdout_behavior = if (output_capture != null) .Pipe else .Inherit;
- child.stderr_behavior = if (output_capture != null) .Pipe else .Inherit;
+ child.stdout_behavior = if (needs_capture) .Pipe else .Inherit;
+ child.stderr_behavior = if (needs_capture) .Pipe else .Inherit;
const spawn_result = child.spawn();
if (spawn_result) |_| {
@@ -41,31 +45,22 @@ pub const External = struct {
const error_msg = try std.fmt.allocPrint(allocator, "'{s}' is not recognized as an internal or external command,\noperable program or batch file.\n", .{external.program});
defer allocator.free(error_msg);
- if (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 };
},
error.AccessDenied => {
const error_msg = "Access is denied.\n";
- if (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 };
},
else => {
const error_msg = try std.fmt.allocPrint(allocator, "Cannot execute '{s}': {}\n", .{ external.program, err });
defer allocator.free(error_msg);
- if (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 };
},
}
@@ -89,14 +84,15 @@ pub const External = struct {
}
// Handle output capture
- if (output_capture) |capture| {
+ if (needs_capture) {
+ var writer = ctx.output_writer;
// Read stdout
if (child.stdout) |stdout| {
var buffer: [4096]u8 = undefined;
while (true) {
const bytes_read = stdout.read(&buffer) catch break;
if (bytes_read == 0) break;
- try capture.write(buffer[0..bytes_read]);
+ try writer.write(buffer[0..bytes_read]);
}
}
@@ -106,7 +102,7 @@ pub const External = struct {
while (true) {
const bytes_read = stderr.read(&buffer) catch break;
if (bytes_read == 0) break;
- try capture.write(buffer[0..bytes_read]);
+ try writer.write(buffer[0..bytes_read]);
}
}
}
@@ -119,11 +115,8 @@ pub const External = struct {
};
defer allocator.free(error_msg);
- if (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 };
};