diff --git a/zigup.zig b/zigup.zig index 6f7e861..f7f53f9 100644 --- a/zigup.zig +++ b/zigup.zig @@ -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, @@ -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}'", @@ -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, @@ -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];