const std = @import("std"); const zup = @import("root"); const Allocator = std.mem.Allocator; const ArrayList = std.ArrayList; const Config = zup.Config; const Installation = zup.Installation; const Installations = zup.Installations; pub const params = \\--active List the active version \\-a, --all List the active, available, and installed versions \\-A, --available List available versions \\-i, --installed List installed versions ; pub const description = "Lists Zig versions. Default is `--active -i`."; pub const min_args = 0; pub const max_args = 0; pub fn main(comptime Result: type, config: Config, res: Result) !void { const allocator = config.allocator; var list_active = res.args.active != 0; var list_available = res.args.available != 0; var list_installed = res.args.installed != 0; if (res.args.all != 0) { list_active = true; list_available = true; list_installed = true; } else if (!list_active and !list_available and !list_installed) { list_active = true; list_installed = true; } if (list_active) { const active = try Installation.getActiveName(allocator); defer if (active) |s| allocator.free(s); try printActive(active); } if (list_installed) { var installed = try Installation.getInstalledList(allocator); defer Installation.deinitMap(allocator, &installed); try printInstalledList(allocator, installed); } if (list_available) { var available = try Installation.getAvailableList(config); defer Installation.deinitMap(allocator, &available); try printAvailableList(allocator, available); } } fn printActive(active: ?[]const u8) !void { const writer = std.io.getStdOut().writer(); try writer.writeAll("Active installation: "); if (active) |act| { try writer.writeAll(act); } else { try writer.writeAll("NONE"); } try writer.writeByte('\n'); } fn printAvailableList(allocator: Allocator, avail: Installations) !void { const writer = std.io.getStdOut().writer(); try writer.writeAll("Available versions:\n"); try printList(allocator, avail); } fn printInstalledList(allocator: Allocator, installed: Installations) !void { const writer = std.io.getStdOut().writer(); try writer.writeAll("Installed versions:\n"); try printList(allocator, installed); } fn printList(allocator: Allocator, installations: Installations) !void { const InstallationAndName = struct { name: []const u8, installation: Installation, const Self = @This(); pub fn lessThan(_: void, lhs: Self, rhs: Self) bool { return lhs.installation.version.order(rhs.installation.version) == .lt; } }; var list = try ArrayList(InstallationAndName).initCapacity(allocator, installations.unmanaged.size); defer list.deinit(); var it = installations.iterator(); while (it.next()) |kv| { list.appendAssumeCapacity(.{ .name = kv.key_ptr.*, .installation = kv.value_ptr.* }); } std.mem.sort(InstallationAndName, list.items, {}, InstallationAndName.lessThan); const writer = std.io.getStdOut().writer(); for (list.items) |item| { try writer.print(" {s}: {}\n", .{ item.name, item.installation.version }); } }