diff options
| author | 2022-04-25 05:09:55 +0300 | |
|---|---|---|
| committer | 2022-04-25 23:34:05 +0300 | |
| commit | d303b53f2ced75703bf022a5d337ee3ba530b288 (patch) | |
| tree | f2e64057120d01ee8a821596ea01b8fc37c53c2c /src/list.zig | |
| download | zup-0.1.0.tar.gz zup-0.1.0.tar.xz zup-0.1.0.zip | |
Initial commit0.1.0
Diffstat (limited to 'src/list.zig')
| -rw-r--r-- | src/list.zig | 103 |
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 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const zup = @import("zup"); | ||
| 3 | |||
| 4 | const Allocator = std.mem.Allocator; | ||
| 5 | const ArrayList = std.ArrayList; | ||
| 6 | const Installation = zup.Installation; | ||
| 7 | const Installations = zup.Installations; | ||
| 8 | |||
| 9 | pub 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 | ; | ||
| 15 | pub const description = "Lists Zig versions. Default is `--active -i`."; | ||
| 16 | pub const min_args = 0; | ||
| 17 | pub const max_args = 0; | ||
| 18 | |||
| 19 | pub 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 | ||
| 53 | fn 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 | |||
| 65 | fn 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 | |||
| 71 | fn 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 | |||
| 77 | fn 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 | } | ||