diff options
Diffstat (limited to 'src/list.zig')
| -rw-r--r-- | src/list.zig | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/src/list.zig b/src/list.zig index 9ff26c2..72f3d91 100644 --- a/src/list.zig +++ b/src/list.zig | |||
| @@ -4,8 +4,10 @@ const zup = @import("root"); | |||
| 4 | const Allocator = std.mem.Allocator; | 4 | const Allocator = std.mem.Allocator; |
| 5 | const ArrayList = std.ArrayList; | 5 | const ArrayList = std.ArrayList; |
| 6 | const Config = zup.Config; | 6 | const Config = zup.Config; |
| 7 | const File = std.fs.File; | ||
| 7 | const Installation = zup.Installation; | 8 | const Installation = zup.Installation; |
| 8 | const Installations = zup.Installations; | 9 | const Installations = zup.Installations; |
| 10 | const Writer = std.io.Writer; | ||
| 9 | 11 | ||
| 10 | pub const params = | 12 | pub const params = |
| 11 | \\--active List the active version | 13 | \\--active List the active version |
| @@ -33,27 +35,33 @@ pub fn main(comptime Result: type, config: Config, res: Result) !void { | |||
| 33 | list_installed = true; | 35 | list_installed = true; |
| 34 | } | 36 | } |
| 35 | 37 | ||
| 38 | const buf = try allocator.alloc(u8, 4096); | ||
| 39 | defer allocator.free(buf); | ||
| 40 | |||
| 41 | var stdout = File.stdout().writer(buf); | ||
| 42 | |||
| 36 | if (list_active) { | 43 | if (list_active) { |
| 37 | const active = try Installation.getActiveName(allocator); | 44 | const active = try Installation.getActiveName(allocator); |
| 38 | defer if (active) |s| allocator.free(s); | 45 | defer if (active) |s| allocator.free(s); |
| 39 | try printActive(active); | 46 | try printActive(&stdout.interface, active); |
| 40 | } | 47 | } |
| 41 | 48 | ||
| 42 | if (list_installed) { | 49 | if (list_installed) { |
| 43 | var installed = try Installation.getInstalledList(allocator); | 50 | var installed = try Installation.getInstalledList(allocator); |
| 44 | defer Installation.deinitMap(allocator, &installed); | 51 | defer Installation.deinitMap(allocator, &installed); |
| 45 | try printInstalledList(allocator, installed); | 52 | try printInstalledList(allocator, &stdout.interface, installed); |
| 46 | } | 53 | } |
| 47 | 54 | ||
| 48 | if (list_available) { | 55 | if (list_available) { |
| 49 | var available = try Installation.getAvailableList(config); | 56 | var available = try Installation.getAvailableList(config); |
| 50 | defer Installation.deinitMap(allocator, &available); | 57 | defer Installation.deinitMap(allocator, &available); |
| 51 | try printAvailableList(allocator, available); | 58 | try printAvailableList(allocator, &stdout.interface, available); |
| 52 | } | 59 | } |
| 60 | |||
| 61 | try stdout.interface.flush(); | ||
| 53 | } | 62 | } |
| 54 | 63 | ||
| 55 | fn printActive(active: ?[]const u8) !void { | 64 | fn printActive(writer: *Writer, active: ?[]const u8) !void { |
| 56 | const writer = std.io.getStdOut().writer(); | ||
| 57 | try writer.writeAll("Active installation: "); | 65 | try writer.writeAll("Active installation: "); |
| 58 | if (active) |act| { | 66 | if (active) |act| { |
| 59 | try writer.writeAll(act); | 67 | try writer.writeAll(act); |
| @@ -64,19 +72,17 @@ fn printActive(active: ?[]const u8) !void { | |||
| 64 | try writer.writeByte('\n'); | 72 | try writer.writeByte('\n'); |
| 65 | } | 73 | } |
| 66 | 74 | ||
| 67 | fn printAvailableList(allocator: Allocator, avail: Installations) !void { | 75 | fn printAvailableList(allocator: Allocator, writer: *Writer, avail: Installations) !void { |
| 68 | const writer = std.io.getStdOut().writer(); | ||
| 69 | try writer.writeAll("Available versions:\n"); | 76 | try writer.writeAll("Available versions:\n"); |
| 70 | try printList(allocator, avail); | 77 | try printList(allocator, writer, avail); |
| 71 | } | 78 | } |
| 72 | 79 | ||
| 73 | fn printInstalledList(allocator: Allocator, installed: Installations) !void { | 80 | fn printInstalledList(allocator: Allocator, writer: *Writer, installed: Installations) !void { |
| 74 | const writer = std.io.getStdOut().writer(); | ||
| 75 | try writer.writeAll("Installed versions:\n"); | 81 | try writer.writeAll("Installed versions:\n"); |
| 76 | try printList(allocator, installed); | 82 | try printList(allocator, writer, installed); |
| 77 | } | 83 | } |
| 78 | 84 | ||
| 79 | fn printList(allocator: Allocator, installations: Installations) !void { | 85 | fn printList(allocator: Allocator, writer: *Writer, installations: Installations) !void { |
| 80 | const InstallationAndName = struct { | 86 | const InstallationAndName = struct { |
| 81 | name: []const u8, | 87 | name: []const u8, |
| 82 | installation: Installation, | 88 | installation: Installation, |
| @@ -98,8 +104,7 @@ fn printList(allocator: Allocator, installations: Installations) !void { | |||
| 98 | 104 | ||
| 99 | std.mem.sort(InstallationAndName, list.items, {}, InstallationAndName.lessThan); | 105 | std.mem.sort(InstallationAndName, list.items, {}, InstallationAndName.lessThan); |
| 100 | 106 | ||
| 101 | const writer = std.io.getStdOut().writer(); | ||
| 102 | for (list.items) |item| { | 107 | for (list.items) |item| { |
| 103 | try writer.print(" {s}: {}\n", .{ item.name, item.installation.version }); | 108 | try writer.print(" {s}: {f}\n", .{ item.name, item.installation.version }); |
| 104 | } | 109 | } |
| 105 | } | 110 | } |