diff options
Diffstat (limited to 'src/cmd/dir.zig')
-rw-r--r-- | src/cmd/dir.zig | 78 |
1 files changed, 52 insertions, 26 deletions
diff --git a/src/cmd/dir.zig b/src/cmd/dir.zig index c19fe3c..27d0e30 100644 --- a/src/cmd/dir.zig +++ b/src/cmd/dir.zig @@ -112,6 +112,9 @@ fn getFreeDiskSpace(path: []const u8) GetFreeDiskSpaceError!u64 { pub const Dir = struct { path: []const u8, + wide_format: bool = false, // /w flag + bare_format: bool = false, // /b flag + subdirs: bool = false, // /s flag pub fn eval(dir: Dir, ctx: CommandContext) !CommandStatus { var output_buffer = ArrayList(u8).init(ctx.allocator); @@ -121,14 +124,17 @@ pub const Dir = struct { 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] == ':') - formatted_path[0] - else - 'C'; - try output_buffer.writer().print(" Volume in drive {c} has no label\n", .{drive_letter}); - try output_buffer.writer().print(" Volume Serial Number is 1234-5678\n", .{}); - try output_buffer.writer().print("\n Directory of {s}\n\n", .{formatted_path}); + // Only show header for non-bare format + if (!dir.bare_format) { + // Get volume label (simplified - just show drive) + const drive_letter = if (formatted_path.len >= 2 and formatted_path[1] == ':') + formatted_path[0] + else + 'C'; + try output_buffer.writer().print(" Volume in drive {c} has no label\n", .{drive_letter}); + try output_buffer.writer().print(" Volume Serial Number is 1234-5678\n", .{}); + try output_buffer.writer().print("\n Directory of {s}\n\n", .{formatted_path}); + } var dir_iterator = std.fs.cwd().openDir(dir.path, .{ .iterate = true }) catch { const error_msg = "File not found\n"; @@ -170,13 +176,25 @@ pub const Dir = struct { switch (entry.kind) { .directory => { - try output_buffer.writer().print("{s:<8} {s:<3} <DIR> {s}\n", .{ lower_name, lower_ext, date_time }); + if (dir.bare_format) { + try output_buffer.writer().print("{s}\n", .{entry.name}); + } else if (dir.wide_format) { + try output_buffer.writer().print("{s:<12} ", .{entry.name}); + } else { + try output_buffer.writer().print("{s:<8} {s:<3} <DIR> {s}\n", .{ lower_name, lower_ext, date_time }); + } dir_count += 1; }, .file => { - const formatted_size = try formatWithCommas(ctx.allocator, stat.size); - defer ctx.allocator.free(formatted_size); - try output_buffer.writer().print("{s:<8} {s:<3} {s:>14} {s}\n", .{ lower_name, lower_ext, formatted_size, date_time }); + if (dir.bare_format) { + try output_buffer.writer().print("{s}\n", .{entry.name}); + } else if (dir.wide_format) { + try output_buffer.writer().print("{s:<12} ", .{entry.name}); + } else { + const formatted_size = try formatWithCommas(ctx.allocator, stat.size); + defer ctx.allocator.free(formatted_size); + try output_buffer.writer().print("{s:<8} {s:<3} {s:>14} {s}\n", .{ lower_name, lower_ext, formatted_size, date_time }); + } file_count += 1; total_file_bytes += stat.size; }, @@ -184,21 +202,29 @@ pub const Dir = struct { } } - // Get free disk space using statvfs - 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, - }; - - const formatted_total_bytes = try formatWithCommas(ctx.allocator, total_file_bytes); - defer ctx.allocator.free(formatted_total_bytes); - const formatted_free_bytes = try formatWithCommas(ctx.allocator, bytes_free); - defer ctx.allocator.free(formatted_free_bytes); + // Add newline after wide format listing + if (dir.wide_format and !dir.bare_format) { + try output_buffer.writer().print("\n", .{}); + } - try output_buffer.writer().print(" {d} File(s) {s:>14} bytes\n", .{ file_count, formatted_total_bytes }); - try output_buffer.writer().print(" {d} Dir(s) {s:>14} bytes free\n", .{ dir_count, formatted_free_bytes }); + // Only show footer for non-bare format + if (!dir.bare_format) { + // Get free disk space using statvfs + 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, + }; + + const formatted_total_bytes = try formatWithCommas(ctx.allocator, total_file_bytes); + defer ctx.allocator.free(formatted_total_bytes); + const formatted_free_bytes = try formatWithCommas(ctx.allocator, bytes_free); + defer ctx.allocator.free(formatted_free_bytes); + + try output_buffer.writer().print(" {d} File(s) {s:>14} bytes\n", .{ file_count, formatted_total_bytes }); + try output_buffer.writer().print(" {d} Dir(s) {s:>14} bytes free\n", .{ dir_count, formatted_free_bytes }); + } if (ctx.output_capture) |capture| { try capture.write(output_buffer.items); |