Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions zigup.zig
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ fn download(allocator: Allocator, url: []const u8, writer: anytype) DownloadResu
// TODO: we take advantage of request.response.content_length

var buf: [4096]u8 = undefined;
var current: usize = 0;
while (true) {
const len = request.reader().read(&buf) catch |err| return .{ .err = std.fmt.allocPrint(
allocator,
Expand All @@ -111,6 +112,13 @@ fn download(allocator: Allocator, url: []const u8, writer: anytype) DownloadResu
) catch |e| oom(e) };
if (len == 0)
return .ok;
progressBar(current, request.response.content_length.?) catch |err| return .{ .err = std.fmt.allocPrint(
allocator,
"failed to write the progress bar with {s}'",
.{@errorName(err)},
) catch |e| oom(e) };
current += len;

writer.writeAll(buf[0..len]) catch |err| return .{ .err = std.fmt.allocPrint(
allocator,
"failed to write the HTTP response body with {s}'",
Expand All @@ -119,6 +127,17 @@ fn download(allocator: Allocator, url: []const u8, writer: anytype) DownloadResu
}
}

fn progressBar(downloaded: usize, total: u64) !void {
const stdout = std.io.getStdOut().writer();
const width = 20;
const percent: f64 = (@as(f64, @floatFromInt(downloaded)) / @as(f64, @floatFromInt(total))) * 100.0;
const filled: u64 = @intFromFloat(@round((percent / 100.0) * @as(f64, @floatFromInt(width))));
try stdout.print("\r[", .{});
for (0..filled) |_| try stdout.print("#", .{});
for (filled..width) |_| try stdout.print(" ", .{});
try stdout.print("] {d:.0}%", .{percent});
}

const DownloadStringResult = union(enum) {
ok: []u8,
err: []u8,
Expand Down Expand Up @@ -1287,6 +1306,7 @@ fn installCompiler(allocator: Allocator, compiler_dir: []const u8, url: []const
return error.AlreadyReported;
},
}
loginfo("", .{}); // add new line

if (std.mem.endsWith(u8, archive_basename, ".tar.xz")) {
archive_root_dir = archive_basename[0 .. archive_basename.len - ".tar.xz".len];
Expand Down
Loading