diff options
Diffstat (limited to 'src/cmd/copy.zig')
-rw-r--r-- | src/cmd/copy.zig | 90 |
1 files changed, 24 insertions, 66 deletions
diff --git a/src/cmd/copy.zig b/src/cmd/copy.zig index 3ca8af0..1c3c3ba 100644 --- a/src/cmd/copy.zig +++ b/src/cmd/copy.zig @@ -1,6 +1,5 @@ const std = @import("std"); const Allocator = std.mem.Allocator; -const print = std.debug.print; const syntax = @import("../syntax.zig"); const FileSpec = syntax.FileSpec; @@ -21,20 +20,14 @@ pub const Copy = struct { const dest_path = switch (copy.to) { .Con => { const error_msg = "Cannot copy from CON to CON\n"; - 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 }; }, .Lpt1, .Lpt2, .Lpt3, .Prn => { const error_msg = "Cannot copy to device\n"; - 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 }; }, .Path => |path| path, @@ -47,11 +40,8 @@ pub const Copy = struct { error.PathAlreadyExists => "File already exists - use different name\n", else => "Cannot create file\n", }; - 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 }; }; defer dest_file.close(); @@ -60,11 +50,6 @@ pub const Copy = struct { const stdin = std.io.getStdIn().reader(); var line_count: u32 = 0; - // Skip output redirection since we're doing interactive input - if (ctx.output_capture == null) { - // In interactive mode, show no prompt (DOS behavior) - } - while (true) { if (stdin.readUntilDelimiterOrEofAlloc(ctx.allocator, '\n', 4096)) |maybe_line| { if (maybe_line) |line| { @@ -97,20 +82,14 @@ pub const Copy = struct { const msg = try std.fmt.allocPrint(ctx.allocator, " 1 File(s) copied\n", .{}); defer ctx.allocator.free(msg); - if (ctx.output_capture) |capture| { - try capture.write(msg); - } else { - print("{s}", .{msg}); - } + var writer = ctx.output_writer; + try writer.write(msg); return CommandStatus{ .Code = 0 }; }, .Lpt1, .Lpt2, .Lpt3, .Prn => { const error_msg = "Cannot copy from device\n"; - 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 }; }, .Path => |path| path, @@ -126,11 +105,8 @@ pub const Copy = struct { error.AccessDenied => "Access denied\n", else => "Cannot access source file\n", }; - 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 }; }; defer source_file.close(); @@ -140,39 +116,27 @@ pub const Copy = struct { while (true) { const bytes_read = source_file.readAll(&buffer) catch { const error_msg = "Error reading file\n"; - 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 }; }; if (bytes_read == 0) break; - if (ctx.output_capture) |capture| { - try capture.write(buffer[0..bytes_read]); - } else { - print("{s}", .{buffer[0..bytes_read]}); - } + var writer = ctx.output_writer; + try writer.write(buffer[0..bytes_read]); if (bytes_read < buffer.len) break; } const msg = " 1 File(s) copied\n"; - if (ctx.output_capture) |capture| { - try capture.write(msg); - } else { - print("{s}", .{msg}); - } + var writer = ctx.output_writer; + try writer.write(msg); return CommandStatus{ .Code = 0 }; }, .Lpt1, .Lpt2, .Lpt3, .Prn => { const error_msg = "Cannot copy to device\n"; - 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 }; }, .Path => |path| path, @@ -186,20 +150,14 @@ pub const Copy = struct { error.PathAlreadyExists => "File already exists\n", else => "Cannot copy file\n", }; - 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 }; }; const msg = " 1 File(s) copied\n"; - if (ctx.output_capture) |capture| { - try capture.write(msg); - } else { - print("{s}", .{msg}); - } + var writer = ctx.output_writer; + try writer.write(msg); return CommandStatus{ .Code = 0 }; } }; |