summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/cmd/dir.zig50
1 files changed, 47 insertions, 3 deletions
diff --git a/src/cmd/dir.zig b/src/cmd/dir.zig
index dde0707..d7e8d8e 100644
--- a/src/cmd/dir.zig
+++ b/src/cmd/dir.zig
@@ -53,6 +53,43 @@ fn formatDosDateTime(allocator: Allocator, timestamp_secs: i64) ![]const u8 {
return try std.fmt.allocPrint(allocator, "{d:0>2}-{d:0>2}-{d:0>2} {d:>2}:{d:0>2}{s}", .{ @as(u32, @intCast(month)), @as(u32, @intCast(day)), @as(u32, @intCast(year % 100)), @as(u32, @intCast(display_hour)), @as(u32, @intCast(minutes)), am_pm });
}
+fn formatWithCommas(allocator: Allocator, number: u64) ![]const u8 {
+ // Convert number to string first
+ const num_str = try std.fmt.allocPrint(allocator, "{d}", .{number});
+ defer allocator.free(num_str);
+
+ // Calculate how many commas we need
+ const digits = num_str.len;
+ const comma_count = if (digits <= 3) 0 else @divFloor(digits - 1, 3);
+
+ if (comma_count == 0) {
+ return try allocator.dupe(u8, num_str);
+ }
+
+ // Allocate space for number + commas
+ const result = try allocator.alloc(u8, digits + comma_count);
+
+ var result_idx: usize = result.len;
+ var num_idx: usize = num_str.len;
+ var digit_count: usize = 0;
+
+ // Work backwards, adding commas every 3 digits
+ while (num_idx > 0) {
+ num_idx -= 1;
+ result_idx -= 1;
+ result[result_idx] = num_str[num_idx];
+ digit_count += 1;
+
+ // Add comma every 3 digits (but not at the beginning)
+ if (digit_count % 3 == 0 and num_idx > 0) {
+ result_idx -= 1;
+ result[result_idx] = ',';
+ }
+ }
+
+ return result;
+}
+
fn isLeapYear(year: u32) bool {
return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0);
}
@@ -128,7 +165,9 @@ pub const Dir = struct {
dir_count += 1;
},
.file => {
- try output_buffer.writer().print("{s:<8} {s:<3} {d:>14} {s}\n", .{ short_name.name, short_name.ext, stat.size, date_time });
+ 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", .{ short_name.name, short_name.ext, formatted_size, date_time });
file_count += 1;
total_file_bytes += stat.size;
},
@@ -144,8 +183,13 @@ pub const Dir = struct {
error.NotImplemented => 0,
};
- 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 });
+ 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);