commit f633931728718c22ee2c1bbcaef7de3cf023bfd0
parent 1c40be0c4f0008229c9968d970ecca794816e6b6
Author: brookjeynes <me@brookjeynes.dev>
Date: Wed, 22 Apr 2026 10:48:18 +1000
fix: render race and redundant width calculation
Diffstat:
2 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/src/ansi-term.zig b/src/ansi-term.zig
@@ -71,7 +71,7 @@ pub fn cursorForward(writer: anytype, columns: usize) !void {
pub fn writeColour(writer: anytype, colour: Colour) !void {
try writer.writeAll(csi);
- _ = switch (colour) {
+ switch (colour) {
.Default => try writer.writeAll("39"),
.Black => try writer.writeAll("30"),
.Red => try writer.writeAll("31"),
@@ -84,7 +84,7 @@ pub fn writeColour(writer: anytype, colour: Colour) !void {
.Fixed => |fixed| try writer.print("48;5;{}", .{fixed}),
.Grey => |grey| try writer.print("48;2;{};{};{}", .{ grey, grey, grey }),
.RGB => |rgb| try writer.print("38;2;{};{};{}", .{ rgb.r, rgb.g, rgb.b }),
- };
+ }
try writer.writeAll("m");
}
diff --git a/src/bar.zig b/src/bar.zig
@@ -73,13 +73,13 @@ pub fn add(self: *Bar, num: usize) void {
///Render the progress bar.
pub fn render(self: *Bar) !void {
+ try self.mutex.lock(self.writer.io);
+ defer self.mutex.unlock(self.writer.io);
+
const winsize = try termsize.termSize(std.Io.File.stdout(), self.writer.io) orelse termsize.TermSize{ .width = default_bar_width, .height = 0 };
const width = if (self.config.width) |w| @min(w, winsize.width) else winsize.width;
var unicode_conversion_buf: [8]u8 = undefined;
- try self.mutex.lock(self.writer.io);
- defer self.mutex.unlock(self.writer.io);
-
if (self.finished) return;
try self.clear();
@@ -134,7 +134,7 @@ pub fn render(self: *Bar) !void {
try self.writer.interface.writeAll(unicode_conversion_buf[0..prefix_bytes]);
const max_percentage_pos: usize = std.math.sub(usize, width, extra_front_chars + extra_back_chars) catch 0;
- const current_percentage_pos: usize = @intFromFloat(percentage * @as(f32, @floatFromInt(std.math.sub(usize, width, extra_front_chars + extra_back_chars) catch 0)));
+ const current_percentage_pos: usize = @intFromFloat(percentage * @as(f32, @floatFromInt(max_percentage_pos)));
for (0..max_percentage_pos) |write_pos| {
if (write_pos > current_percentage_pos) {
if (self.config.show_background) {