summaryrefslogtreecommitdiff
path: root/src/list.zig
diff options
context:
space:
mode:
authorGravatar Uko Kokņevičs2025-07-22 06:29:25 +0300
committerGravatar Uko Kokņevičs2025-07-22 06:29:25 +0300
commit73fd32eb28753584edb160fc11b0c16078a2ed6d (patch)
tree6ced78d1293bea471d6fb3dd29a57e860960385e /src/list.zig
parentDocument that zup 0.6.0 compiles with zig 0.14.1 as well (diff)
downloadzup-73fd32eb28753584edb160fc11b0c16078a2ed6d.tar.gz
zup-73fd32eb28753584edb160fc11b0c16078a2ed6d.tar.xz
zup-73fd32eb28753584edb160fc11b0c16078a2ed6d.zip
Update ZigHEADmain
Diffstat (limited to 'src/list.zig')
-rw-r--r--src/list.zig33
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");
4const Allocator = std.mem.Allocator; 4const Allocator = std.mem.Allocator;
5const ArrayList = std.ArrayList; 5const ArrayList = std.ArrayList;
6const Config = zup.Config; 6const Config = zup.Config;
7const File = std.fs.File;
7const Installation = zup.Installation; 8const Installation = zup.Installation;
8const Installations = zup.Installations; 9const Installations = zup.Installations;
10const Writer = std.io.Writer;
9 11
10pub const params = 12pub 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
55fn printActive(active: ?[]const u8) !void { 64fn 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
67fn printAvailableList(allocator: Allocator, avail: Installations) !void { 75fn 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
73fn printInstalledList(allocator: Allocator, installed: Installations) !void { 80fn 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
79fn printList(allocator: Allocator, installations: Installations) !void { 85fn 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}