diff options
author | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2025-08-13 21:31:52 +0200 |
---|---|---|
committer | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2025-08-13 21:31:52 +0200 |
commit | ceb5eba67fff7cc8f85cbb113a86e0c6f349d9db (patch) | |
tree | 026827926119335ec8644205f6c7246be706b7a8 | |
parent | 7438ec97b41719d743d252d83840d51f4e4d8d04 (diff) |
main: Use convertTo83 during prompt generation.
-rw-r--r-- | src/main.zig | 83 |
1 files changed, 69 insertions, 14 deletions
diff --git a/src/main.zig b/src/main.zig index 1ad379d..c3e83b5 100644 --- a/src/main.zig +++ b/src/main.zig @@ -11,33 +11,88 @@ const Command = parser.Command; const eval = @import("eval.zig"); const CommandStatus = eval.CommandStatus; +const paths = @import("paths.zig"); +const convertTo83 = paths.convertTo83; + const STDOUT_BUFFER_SIZE: usize = 1024; const STDERR_BUFFER_SIZE: usize = 1024; fn formatPath(allocator: Allocator, path: []const u8) ![]const u8 { - var result = ArrayList(u8).init(allocator); - defer result.deinit(); + // Normalize separators and case first + var normalized = ArrayList(u8).init(allocator); + defer normalized.deinit(); - // Simple DOS-style path formatting - // Convert to uppercase and replace / with \ for (path) |ch| { if (ch == '/') { - try result.append('\\'); + try normalized.append('\\'); } else { - try result.append(std.ascii.toUpper(ch)); + try normalized.append(std.ascii.toUpper(ch)); + } + } + + // Extract drive letter if present (e.g., "C:") + var idx: usize = 0; + var has_drive = false; + var drive_letter: u8 = 'C'; + if (normalized.items.len >= 2 and normalized.items[1] == ':') { + has_drive = true; + drive_letter = normalized.items[0]; + idx = 2; + } + + // Remember if the path is absolute after the drive (leading backslash) + var starts_with_backslash = false; + if (idx < normalized.items.len and normalized.items[idx] == '\\') { + starts_with_backslash = true; + // Skip the first leading backslash to avoid creating an empty component + idx += 1; + } + + // Build result with components converted to 8.3 + var result = ArrayList(u8).init(allocator); + defer result.deinit(); + + // If absolute, start with a backslash (after potential drive prefix) + if (starts_with_backslash) { + try result.append('\\'); + } + + var it = std.mem.splitScalar(u8, normalized.items[idx..], '\\'); + var first_component = true; + while (it.next()) |component| { + if (component.len == 0) continue; + + if (!first_component and result.items.len > 0 and result.items[result.items.len - 1] != '\\') { + try result.append('\\'); } + first_component = false; + + const short_name = try convertTo83(allocator, component); + defer allocator.free(short_name); + try result.appendSlice(short_name); } - // Add C: prefix if no drive letter - if (result.items.len == 0 or result.items[1] != ':') { - var prefixed = ArrayList(u8).init(allocator); - defer prefixed.deinit(); - try prefixed.appendSlice("C:"); - try prefixed.appendSlice(result.items); - return allocator.dupe(u8, prefixed.items); + // Prepend drive if present or add default C: + var final_buf = ArrayList(u8).init(allocator); + defer final_buf.deinit(); + + if (has_drive) { + try final_buf.append(drive_letter); + try final_buf.append(':'); + if (result.items.len > 0 and result.items[0] != '\\') { + try final_buf.append('\\'); + } + try final_buf.appendSlice(result.items); + } else { + // No drive: default to C: + try final_buf.appendSlice("C:"); + if (result.items.len > 0 and result.items[0] != '\\') { + try final_buf.append('\\'); + } + try final_buf.appendSlice(result.items); } - return allocator.dupe(u8, result.items); + return allocator.dupe(u8, final_buf.items); } fn parseAndExecute(input: []const u8, allocator: Allocator) !CommandStatus { |