commit ba858080ae85aa2b33381e6c1e3893aac324586a
parent 464f9d67c21fb0a5a3e45fe0c1f602efbdff60e0
Author: BrookJeynes <jeynesbrook@gmail.com>
Date: Tue, 13 Aug 2024 09:43:57 +1000
Removed the need for try/catching certain methods
- Setting `max_progress` to 0 will no longer throw an error.
- `add()` no longer explicitly errors.
- `set()` no longer explicitly errors. Setting the bar to a larger
number than `max_progress` will set `current_progress` to
`max_progress`
- Updated doc strings.
- Updated examples.
Diffstat:
4 files changed, 14 insertions(+), 16 deletions(-)
diff --git a/examples/complex.zig b/examples/complex.zig
@@ -15,7 +15,7 @@ pub fn main() !void {
defer pb.clear() catch {};
while (!pb.isFinished()) {
- try pb.add(1);
+ pb.add(1);
try pb.render();
std.time.sleep(std.time.ns_per_ms * 150);
diff --git a/examples/simple.zig b/examples/simple.zig
@@ -6,7 +6,7 @@ pub fn main() !void {
var pb = ProgressBar.init(10, stdout.any(), .{});
while (!pb.isFinished()) {
- try pb.add(1);
+ pb.add(1);
try pb.render();
std.time.sleep(std.time.ns_per_ms * 150);
diff --git a/examples/thread.zig b/examples/thread.zig
@@ -6,7 +6,7 @@ pub fn threadWorker(bar: *ProgressBar, seed: usize) !void {
const num = @mod(rand_impl.random().int(u64), 1000);
for (0..5) |_| {
- try bar.add(1);
+ bar.add(1);
try bar.render();
std.time.sleep(std.time.ns_per_ms * num);
diff --git a/src/progress.zig b/src/progress.zig
@@ -3,9 +3,7 @@ const termsize = @import("termsize.zig");
const escape_codes = @import("escape_codes.zig");
const Error = error{
- MaxIsZero,
FailedToRender,
- NumGreaterThanMax,
};
const Config = struct {
@@ -45,15 +43,12 @@ pub fn init(max_progress: usize, writer: std.io.AnyWriter, config: Config) Progr
};
}
-///Add `num` to the progress bar. If `num` is greater than `max_progress`, `current_progress` will be set to the max.
-pub fn add(self: *ProgressBar, num: usize) Error!void {
+///Add `num` to the progress bar.
+///If `num` is greater than `max_progress`, `current_progress` will be set to the max.
+pub fn add(self: *ProgressBar, num: usize) void {
self.mutex.lock();
defer self.mutex.unlock();
- if (self.max_progress == 0) {
- return Error.MaxIsZero;
- }
-
if (self.current_progress + num < self.max_progress) {
self.current_progress += num;
} else {
@@ -87,7 +82,7 @@ pub fn render(self: *ProgressBar) !void {
count += 2; // "||"
}
- if (self.config.show_percentage) count += 5;
+ if (self.config.show_percentage) count += 4; // "xxx%"
break :brk count;
};
@@ -170,13 +165,16 @@ pub fn reset(self: *ProgressBar) void {
///Set the progress bar to `num`.
///
///If `num` is equal to `max_progress`, `finished` is set true.
-///If `num` is greater than `max_progress`, `Error.NumGreaterThanMax` is returned.
-pub fn set(self: *ProgressBar, num: usize) Error!void {
+///If `num` is greater than `max_progress`, `current_progress` will be set to the max.
+pub fn set(self: *ProgressBar, num: usize) void {
self.mutex.lock();
defer self.mutex.unlock();
- if (num == self.max_progress) self.finished = true;
- if (num > self.max_progress) return Error.NumGreaterThanMax;
+ if (num >= self.max_progress) {
+ self.finished = true;
+ self.current_progress = self.max_progress;
+ return;
+ }
self.current_progress = num;
}