summaryrefslogtreecommitdiff
path: root/src/list.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/list.zig')
-rw-r--r--src/list.zig103
1 files changed, 103 insertions, 0 deletions
diff --git a/src/list.zig b/src/list.zig
new file mode 100644
index 0000000..d97ba87
--- /dev/null
+++ b/src/list.zig
@@ -0,0 +1,103 @@
1const std = @import("std");
2const zup = @import("zup");
3
4const Allocator = std.mem.Allocator;
5const ArrayList = std.ArrayList;
6const Installation = zup.Installation;
7const Installations = zup.Installations;
8
9pub const params =
10 \\--active List the active version
11 \\-a, --all List the active, available, and installed versions
12 \\-A, --available List available versions
13 \\-i, --installed List installed versions
14;
15pub const description = "Lists Zig versions. Default is `--active -i`.";
16pub const min_args = 0;
17pub const max_args = 0;
18
19pub fn main(comptime Result: type, allocator: Allocator, res: Result) !void {
20 var list_active = res.args.active;
21 var list_available = res.args.available;
22 var list_installed = res.args.installed;
23
24 if (res.args.all) {
25 list_active = true;
26 list_available = true;
27 list_installed = true;
28 } else if (!list_active and !list_available and !list_installed) {
29 list_active = true;
30 list_installed = true;
31 }
32
33 if (list_active) {
34 var active = try Installation.getActiveName(allocator);
35 defer if (active) |s| allocator.free(s);
36 try printActive(active);
37 }
38
39 if (list_installed) {
40 var installed = try Installation.getInstalledList(allocator);
41 defer Installation.deinitMap(allocator, &installed);
42 try printInstalledList(allocator, installed);
43 }
44
45 if (list_available) {
46 var available = try Installation.getAvailableList(allocator);
47 defer Installation.deinitMap(allocator, &available);
48 try printAvailableList(allocator, available);
49 }
50}
51
52// TODO: zig-bug active should be ?[]const u8
53fn printActive(active: ?[]u8) !void {
54 const writer = std.io.getStdOut().writer();
55 try writer.writeAll("Active installation: ");
56 if (active) |act| {
57 try writer.writeAll(act);
58 } else {
59 try writer.writeAll("NONE");
60 }
61
62 try writer.writeByte('\n');
63}
64
65fn printAvailableList(allocator: Allocator, avail: Installations) !void {
66 const writer = std.io.getStdOut().writer();
67 try writer.writeAll("Available versions:\n");
68 try printList(allocator, avail);
69}
70
71fn printInstalledList(allocator: Allocator, installed: Installations) !void {
72 const writer = std.io.getStdOut().writer();
73 try writer.writeAll("Installed versions:\n");
74 try printList(allocator, installed);
75}
76
77fn printList(allocator: Allocator, installations: Installations) !void {
78 const InstallationAndName = struct {
79 name: []const u8,
80 installation: Installation,
81
82 const Self = @This();
83
84 pub fn lessThan(_: void, lhs: Self, rhs: Self) bool {
85 return lhs.installation.version.order(rhs.installation.version) == .lt;
86 }
87 };
88
89 var list = try ArrayList(InstallationAndName).initCapacity(allocator, installations.unmanaged.size);
90 defer list.deinit();
91
92 var it = installations.iterator();
93 while (it.next()) |kv| {
94 list.appendAssumeCapacity(.{ .name = kv.key_ptr.*, .installation = kv.value_ptr.* });
95 }
96
97 std.sort.sort(InstallationAndName, list.items, {}, InstallationAndName.lessThan);
98
99 const writer = std.io.getStdOut().writer();
100 for (list.items) |item| {
101 try writer.print(" {s}: {}\n", .{ item.name, item.installation.version });
102 }
103}