summaryrefslogtreecommitdiff
path: root/src/cmd/dir.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/dir.zig')
-rw-r--r--src/cmd/dir.zig27
1 files changed, 13 insertions, 14 deletions
diff --git a/src/cmd/dir.zig b/src/cmd/dir.zig
index a3b8279..5aff48b 100644
--- a/src/cmd/dir.zig
+++ b/src/cmd/dir.zig
@@ -17,6 +17,7 @@ const convertTo83 = paths.convertTo83;
const types = @import("./lib/types.zig");
const CommandStatus = types.CommandStatus;
+const CommandContext = types.CommandContext;
const OutputCapture = types.OutputCapture;
const InputSource = types.InputSource;
@@ -75,15 +76,13 @@ fn getFreeDiskSpace(path: []const u8) GetFreeDiskSpaceError!u64 {
pub const Dir = struct {
path: []const u8,
- pub fn eval(dir: Dir, allocator: Allocator, output_capture: ?*OutputCapture, input_source: ?*InputSource) !CommandStatus {
- _ = input_source;
-
- var output_buffer = ArrayList(u8).init(allocator);
+ pub fn eval(dir: Dir, ctx: CommandContext) !CommandStatus {
+ var output_buffer = ArrayList(u8).init(ctx.allocator);
defer output_buffer.deinit();
// Format path in DOS style with backslashes and uppercase drive letter
- const formatted_path = try formatDosPath(allocator, dir.path);
- defer allocator.free(formatted_path);
+ const formatted_path = try formatDosPath(ctx.allocator, dir.path);
+ defer ctx.allocator.free(formatted_path);
// Get volume label (simplified - just show drive)
const drive_letter = if (formatted_path.len >= 2 and formatted_path[1] == ':')
@@ -96,7 +95,7 @@ pub const Dir = struct {
var dir_iterator = std.fs.cwd().openDir(dir.path, .{ .iterate = true }) catch {
const error_msg = "File not found\n";
- if (output_capture) |capture| {
+ if (ctx.output_capture) |capture| {
try capture.write(error_msg);
} else {
print("{s}", .{error_msg});
@@ -115,12 +114,12 @@ pub const Dir = struct {
// Convert timestamp to DOS date/time format
const mtime_secs = @divFloor(stat.mtime, std.time.ns_per_s);
- const date_time = try formatDosDateTime(allocator, @intCast(mtime_secs));
- defer allocator.free(date_time);
+ const date_time = try formatDosDateTime(ctx.allocator, @intCast(mtime_secs));
+ defer ctx.allocator.free(date_time);
// Convert filename to 8.3 format
- const short_name = try convertTo83(allocator, entry.name);
- defer allocator.free(short_name);
+ const short_name = try convertTo83(ctx.allocator, entry.name);
+ defer ctx.allocator.free(short_name);
switch (entry.kind) {
.directory => {
@@ -137,8 +136,8 @@ pub const Dir = struct {
}
// Get free disk space using statvfs
- const path = try std.fs.cwd().realpathAlloc(allocator, dir.path);
- defer allocator.free(path);
+ const path = try std.fs.cwd().realpathAlloc(ctx.allocator, dir.path);
+ defer ctx.allocator.free(path);
const bytes_free = getFreeDiskSpace(path) catch |err| switch (err) {
error.AccessDenied => 0,
error.NotImplemented => 0,
@@ -147,7 +146,7 @@ pub const Dir = struct {
try output_buffer.writer().print(" {d} File(s) {d:>14} bytes\n", .{ file_count, total_file_bytes });
try output_buffer.writer().print(" {d} Dir(s) {d:>14} bytes free\n", .{ dir_count, bytes_free });
- if (output_capture) |capture| {
+ if (ctx.output_capture) |capture| {
try capture.write(output_buffer.items);
} else {
print("{s}", .{output_buffer.items});