authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-08 17:26:55-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-08 17:26:55-07:00
log5a00e249632716b86edac088f69d19d82e307a28
tree2daa825ca0033bd0fb4735c43134c3f9ef783f6c
parentfd6c351263e99af4dca73af953711e6d1b57f4e4

std.Progress: make the API infallible

by handling `error.TimerUnsupported`. In this case, only explicit calls to refresh() will cause the progress line to be printed.

4 files changed, 21 insertions(+), 23 deletions(-)

lib/std/Progress.zig+18-14
......@@ -35,7 +35,7 @@ root: Node = undefined,
3535
3636/// Keeps track of how much time has passed since the beginning.
3737/// Used to compare with `initial_delay_ms` and `refresh_rate_ms`.
38timer: std.time.Timer = undefined,
38timer: ?std.time.Timer = null,
3939
4040/// When the previous refresh was written to the terminal.
4141/// Used to compare with `refresh_rate_ms`.
......@@ -139,7 +139,7 @@ pub const Node = struct {
139139/// TODO solve https://github.com/ziglang/zig/issues/2765 and then change this
140140/// API to return Progress rather than accept it as a parameter.
141141/// `estimated_total_items` value of 0 means unknown.
142pub fn start(self: *Progress, name: []const u8, estimated_total_items: usize) !*Node {
142pub fn start(self: *Progress, name: []const u8, estimated_total_items: usize) *Node {
143143 const stderr = std.io.getStdErr();
144144 self.terminal = null;
145145 if (stderr.supportsAnsiEscapeCodes()) {
......@@ -161,22 +161,24 @@ pub fn start(self: *Progress, name: []const u8, estimated_total_items: usize) !*
161161 };
162162 self.columns_written = 0;
163163 self.prev_refresh_timestamp = 0;
164 self.timer = try std.time.Timer.start();
164 self.timer = std.time.Timer.start() catch null;
165165 self.done = false;
166166 return &self.root;
167167}
168168
169169/// Updates the terminal if enough time has passed since last update. Thread-safe.
170170pub fn maybeRefresh(self: *Progress) void {
171 const now = self.timer.read();
172 if (now < self.initial_delay_ns) return;
173 if (!self.update_mutex.tryLock()) return;
174 defer self.update_mutex.unlock();
175 // TODO I have observed this to happen sometimes. I think we need to follow Rust's
176 // lead and guarantee monotonically increasing times in the std lib itself.
177 if (now < self.prev_refresh_timestamp) return;
178 if (now - self.prev_refresh_timestamp < self.refresh_rate_ns) return;
179 return self.refreshWithHeldLock();
171 if (self.timer) |*timer| {
172 const now = timer.read();
173 if (now < self.initial_delay_ns) return;
174 if (!self.update_mutex.tryLock()) return;
175 defer self.update_mutex.unlock();
176 // TODO I have observed this to happen sometimes. I think we need to follow Rust's
177 // lead and guarantee monotonically increasing times in the std lib itself.
178 if (now < self.prev_refresh_timestamp) return;
179 if (now - self.prev_refresh_timestamp < self.refresh_rate_ns) return;
180 return self.refreshWithHeldLock();
181 }
180182}
181183
182184/// Updates the terminal and resets `self.next_refresh_timestamp`. Thread-safe.
......@@ -285,7 +287,9 @@ fn refreshWithHeldLock(self: *Progress) void {
285287 // Stop trying to write to this file once it errors.
286288 self.terminal = null;
287289 };
288 self.prev_refresh_timestamp = self.timer.read();
290 if (self.timer) |*timer| {
291 self.prev_refresh_timestamp = timer.read();
292 }
289293}
290294
291295pub fn log(self: *Progress, comptime format: []const u8, args: anytype) void {
......@@ -327,7 +331,7 @@ test "basic functionality" {
327331 return error.SkipZigTest;
328332 }
329333 var progress = Progress{};
330 const root_node = try progress.start("", 100);
334 const root_node = progress.start("", 100);
331335 defer root_node.end();
332336
333337 const sub_task_names = [_][]const u8{
lib/std/special/test_runner.zig+1-4
......@@ -34,10 +34,7 @@ pub fn main() void {
3434 var progress = std.Progress{
3535 .dont_print_on_dumb = true,
3636 };
37 const root_node = progress.start("Test", test_fn_list.len) catch |err| switch (err) {
38 // TODO still run tests in this case
39 error.TimerUnsupported => @panic("timer unsupported"),
40 };
37 const root_node = progress.start("Test", test_fn_list.len);
4138 const have_tty = progress.terminal != null and progress.supports_ansi_escape_codes;
4239
4340 var async_frame_buffer: []align(std.Target.stack_align) u8 = undefined;
src/Compilation.zig+1-1
......@@ -2586,7 +2586,7 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
25862586 // If the terminal is dumb, we dont want to show the user all the
25872587 // output.
25882588 var progress: std.Progress = .{ .dont_print_on_dumb = true };
2589 var main_progress_node = try progress.start("", 0);
2589 var main_progress_node = progress.start("", 0);
25902590 defer main_progress_node.end();
25912591 if (self.color == .off) progress.terminal = null;
25922592
src/stage1.zig+1-4
......@@ -305,10 +305,7 @@ export fn stage2_progress_start_root(
305305 name_len: usize,
306306 estimated_total_items: usize,
307307) *std.Progress.Node {
308 return progress.start(
309 name_ptr[0..name_len],
310 estimated_total_items,
311 ) catch @panic("timer unsupported");
308 return progress.start(name_ptr[0..name_len], estimated_total_items);
312309}
313310
314311// ABI warning