diff options
Diffstat (limited to 'src/cmd/chdir.zig')
-rw-r--r-- | src/cmd/chdir.zig | 31 |
1 files changed, 15 insertions, 16 deletions
diff --git a/src/cmd/chdir.zig b/src/cmd/chdir.zig index 5c2c618..c9413ff 100644 --- a/src/cmd/chdir.zig +++ b/src/cmd/chdir.zig @@ -7,35 +7,34 @@ const formatDosPath = paths.formatDosPath; const types = @import("./lib/types.zig"); const CommandStatus = types.CommandStatus; -const OutputCapture = types.OutputCapture; -const InputSource = types.InputSource; +const CommandContext = types.CommandContext; pub const Chdir = struct { path: []const u8, - pub fn eval(chdir: Chdir, allocator: Allocator, output_capture: ?*OutputCapture, input_source: ?*InputSource) !CommandStatus { - _ = input_source; + pub fn eval(chdir: Chdir, ctx: CommandContext) !CommandStatus { + _ = ctx.input_source; if (chdir.path.len == 0) { // No arguments - display current directory - const cwd = std.fs.cwd().realpathAlloc(allocator, ".") catch { + const cwd = std.fs.cwd().realpathAlloc(ctx.allocator, ".") catch { const error_msg = "Unable to determine current directory\n"; - if (output_capture) |capture| { + if (ctx.output_capture) |capture| { try capture.write(error_msg); } else { print("{s}", .{error_msg}); } return CommandStatus{ .Code = 1 }; }; - defer allocator.free(cwd); + defer ctx.allocator.free(cwd); - const formatted_path = try formatDosPath(allocator, cwd); - defer allocator.free(formatted_path); + const formatted_path = try formatDosPath(ctx.allocator, cwd); + defer ctx.allocator.free(formatted_path); - const output = try std.fmt.allocPrint(allocator, "{s}\n", .{formatted_path}); - defer allocator.free(output); + const output = try std.fmt.allocPrint(ctx.allocator, "{s}\n", .{formatted_path}); + defer ctx.allocator.free(output); - if (output_capture) |capture| { + if (ctx.output_capture) |capture| { try capture.write(output); } else { print("{s}", .{output}); @@ -50,7 +49,7 @@ pub const Chdir = struct { // Go to parent directory std.process.changeCurDir("..") catch { const error_msg = "The system cannot find the path specified.\n"; - if (output_capture) |capture| { + if (ctx.output_capture) |capture| { try capture.write(error_msg); } else { print("{s}", .{error_msg}); @@ -61,7 +60,7 @@ pub const Chdir = struct { // Go to root directory - simplified to just go to "/" std.process.changeCurDir("/") catch { const error_msg = "The system cannot find the path specified.\n"; - if (output_capture) |capture| { + if (ctx.output_capture) |capture| { try capture.write(error_msg); } else { print("{s}", .{error_msg}); @@ -74,7 +73,7 @@ pub const Chdir = struct { for (target_path) |ch| { if (ch == 0) { const error_msg = "Invalid path: contains null character\n"; - if (output_capture) |capture| { + if (ctx.output_capture) |capture| { try capture.write(error_msg); } else { print("{s}", .{error_msg}); @@ -85,7 +84,7 @@ pub const Chdir = struct { std.process.changeCurDir(target_path) catch { const error_msg = "The system cannot find the path specified.\n"; - if (output_capture) |capture| { + if (ctx.output_capture) |capture| { try capture.write(error_msg); } else { print("{s}", .{error_msg}); |