commit 8d8daec6a078eb273bb2e561b30fb59dc20f4c74
parent b44211565149aec2a06f7bbfc96b651fd7f51c66
Author: brookjeynes <me@brookjeynes.dev>
Date: Wed, 22 Apr 2026 11:09:04 +1000
refactor(bar): refactor colour and bg_colour usage
This moves colour and bg_colour into the config and renames setColour
and setBgColour to updateColour and updateBackgroundColour
Diffstat:
2 files changed, 18 insertions(+), 28 deletions(-)
diff --git a/examples/bar/complex.zig b/examples/bar/complex.zig
@@ -13,8 +13,8 @@ pub fn main(init: std.process.Init) !void {
.show_iterations = true,
.show_percentage = true,
.width = 60,
+ .colour = .Blue,
});
- pb1.setColour(.Blue);
var pb2 = ProgressBar.init(15, &stdout_writer, .{
.bar_prefix = '|',
@@ -25,10 +25,9 @@ pub fn main(init: std.process.Init) !void {
.show_percentage = true,
.width = 100,
.show_background = true,
+ .colour = .{ .RGB = .{ .r = 255, .g = 0, .b = 127 } },
+ .bg_colour = .{ .RGB = .{ .r = 120, .g = 0, .b = 127 } },
});
- pb2.setColour(.{ .RGB = .{ .r = 255, .g = 0, .b = 127 } });
- pb2.setBgColour(.{ .RGB = .{ .r = 120, .g = 0, .b = 127 } });
-
const progress_bars: [2]*ProgressBar = .{ &pb1, &pb2 };
for (progress_bars, 0..) |pb, i| {
diff --git a/src/bar.zig b/src/bar.zig
@@ -33,6 +33,10 @@ const Config = struct {
///If the width is greater than the terminal width, the terminal width will be used.
///It is up to you to ensure you provide enough space to render the complete bar. If the bar is too small, calls to `render()` will error.
width: ?usize = null,
+ ///The progress bar foreground colour.
+ colour: ansi_term.Colour = .Default,
+ ///The progress bar background colour.
+ bg_colour: ansi_term.Colour = .Default,
};
const Bar = @This();
@@ -45,10 +49,6 @@ config: Config,
mutex: std.Io.Mutex = std.Io.Mutex.init,
///Direct access is not thread safe. Use `isFinished()` if you need thread safety.
finished: bool = false,
-///The progress bar foreground colour.
-colour: ansi_term.Colour = .Default,
-///The progress bar background colour.
-bg_colour: ansi_term.Colour = .Default,
pub fn init(max_progress: usize, writer: *std.Io.File.Writer, config: Config) Bar {
return Bar{
@@ -138,7 +138,7 @@ pub fn render(self: *Bar) !void {
for (0..max_percentage_pos) |write_pos| {
if (write_pos > current_percentage_pos) {
if (self.config.show_background) {
- try ansi_term.writeColour(&self.writer.interface, self.bg_colour);
+ try ansi_term.writeColour(&self.writer.interface, self.config.bg_colour);
const fill_char_bytes = try std.unicode.utf8Encode(self.config.bar_fill_char, &unicode_conversion_buf);
try self.writer.interface.writeAll(unicode_conversion_buf[0..fill_char_bytes]);
try ansi_term.resetColour(&self.writer.interface);
@@ -146,7 +146,7 @@ pub fn render(self: *Bar) !void {
continue;
}
- try ansi_term.writeColour(&self.writer.interface, self.colour);
+ try ansi_term.writeColour(&self.writer.interface, self.config.colour);
const fill_char_bytes = try std.unicode.utf8Encode(self.config.bar_fill_char, &unicode_conversion_buf);
try self.writer.interface.writeAll(unicode_conversion_buf[0..fill_char_bytes]);
try ansi_term.resetColour(&self.writer.interface);
@@ -172,36 +172,27 @@ pub fn render(self: *Bar) !void {
try self.writer.interface.flush();
}
-///Set the progress bar colour.
-pub fn setColour(self: *Bar, colour: ansi_term.Colour) void {
+///Update the progress bar colour.
+pub fn updateColour(self: *Bar, colour: ansi_term.Colour) void {
self.mutex.lockUncancelable(self.writer.io);
defer self.mutex.unlock(self.writer.io);
- self.colour = colour;
+ self.config.colour = colour;
}
-///Set the progress bar background colour.
-pub fn setBgColour(self: *Bar, colour: ansi_term.Colour) void {
+///Update the progress bar background colour.
+pub fn updateBgColour(self: *Bar, colour: ansi_term.Colour) void {
self.mutex.lockUncancelable(self.writer.io);
defer self.mutex.unlock(self.writer.io);
- self.bg_colour = colour;
+ self.config.bg_colour = colour;
}
-///Show the progress bar background.
-pub fn showBg(self: *Bar) void {
+///Toggle the progress bar background.
+pub fn setShowBg(self: *Bar, visible: bool) void {
self.mutex.lockUncancelable(self.writer.io);
defer self.mutex.unlock(self.writer.io);
-
- self.config.show_background = true;
-}
-
-///Hide the progress bar background.
-pub fn hideBg(self: *Bar) void {
- self.mutex.lockUncancelable(self.writer.io);
- defer self.mutex.unlock(self.writer.io);
-
- self.config.show_background = false;
+ self.config.show_background = visible;
}
///Returns `true` if the progress bar is finished and `false` otherwise.