authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-07-25 13:51:22+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-07-25 13:51:22+01:00
log06e50e9aa7b64b0b191f4048c853dfbc02eacbed
treef3cefb6f060d60d6a4714c0776227a146043008d
parentdc24835168d107183671288899954f5af56b0ba1
signaturelock-open Commit is signed but in an unrecognized format.

std.Progress: add optional unit to progress indicator


1 files changed, 18 insertions(+), 2 deletions(-)

lib/std/Progress.zig+18-2
......@@ -68,6 +68,7 @@ pub const Node = struct {
6868 context: *Progress,
6969 parent: ?*Node,
7070 name: []const u8,
71 unit: []const u8 = "",
7172 /// Must be handled atomically to be thread-safe.
7273 recently_updated_child: ?*Node = null,
7374 /// Must be handled atomically to be thread-safe. 0 means null.
......@@ -141,6 +142,21 @@ pub const Node = struct {
141142 }
142143 }
143144
145 /// Thread-safe.
146 pub fn setUnit(self: *Node, unit: []const u8) void {
147 const progress = self.context;
148 progress.update_mutex.lock();
149 defer progress.update_mutex.unlock();
150 self.unit = unit;
151 if (self.parent) |parent| {
152 @atomicStore(?*Node, &parent.recently_updated_child, self, .Release);
153 if (parent.parent) |grand_parent| {
154 @atomicStore(?*Node, &grand_parent.recently_updated_child, parent, .Release);
155 }
156 if (progress.timer) |*timer| progress.maybeRefreshWithHeldLock(timer);
157 }
158 }
159
144160 /// Thread-safe. 0 means unknown.
145161 pub fn setEstimatedTotalItems(self: *Node, count: usize) void {
146162 @atomicStore(usize, &self.unprotected_estimated_total_items, count, .Monotonic);
......@@ -307,11 +323,11 @@ fn refreshWithHeldLock(self: *Progress) void {
307323 }
308324 if (eti > 0) {
309325 if (need_ellipse) self.bufWrite(&end, " ", .{});
310 self.bufWrite(&end, "[{d}/{d}] ", .{ current_item, eti });
326 self.bufWrite(&end, "[{d}/{d}{s}] ", .{ current_item, eti, node.unit });
311327 need_ellipse = false;
312328 } else if (completed_items != 0) {
313329 if (need_ellipse) self.bufWrite(&end, " ", .{});
314 self.bufWrite(&end, "[{d}] ", .{current_item});
330 self.bufWrite(&end, "[{d}{s}] ", .{ current_item, node.unit });
315331 need_ellipse = false;
316332 }
317333 }