authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-10-17 21:46:41-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-10-17 21:55:49-04:00
log2d5b2bf1c986d037ef965bf8c9b4d8dfd5967478
treee7b0f9f6f509e34edeb2226569b2ae78d34cfb5a
parent299991019dddb2acd076d4b2698a4fd6a7a6ae94
signaturelock-open Commit is signed but in an unrecognized format.

improve progress reporting

* use erase rest of line escape code. * use `stderr.supportsAnsiEscapeCodes` rather than `isTty`. * respect `--color off` * avoid unnecessary recursion * add `Progress.log` * disable the progress std lib test since it's noisy and uses `time.sleep()`. * enable/integrate progress printing with the default test runner

8 files changed, 92 insertions(+), 55 deletions(-)

lib/std/progress.zig+52-36
......@@ -26,10 +26,6 @@ pub const Progress = struct {
2626 /// with each refresh.
2727 output_buffer: [100]u8 = undefined,
2828
29 /// Keeps track of how many columns in the terminal have been output, so that
30 /// we can move the cursor back later.
31 columns_written: usize = undefined,
32
3329 /// How many nanoseconds between writing updates to the terminal.
3430 refresh_rate_ns: u64 = 50 * std.time.millisecond,
3531
......@@ -38,6 +34,10 @@ pub const Progress = struct {
3834
3935 done: bool = true,
4036
37 /// Keeps track of how many columns in the terminal have been output, so that
38 /// we can move the cursor back later.
39 columns_written: usize = undefined,
40
4141 /// Represents one unit of progress. Each node can have children nodes, or
4242 /// one can use integers with `update`.
4343 pub const Node = struct {
......@@ -99,8 +99,7 @@ pub const Progress = struct {
9999 /// API to return Progress rather than accept it as a parameter.
100100 pub fn start(self: *Progress, name: []const u8, estimated_total_items: ?usize) !*Node {
101101 if (std.io.getStdErr()) |stderr| {
102 const is_term = stderr.isTty();
103 self.terminal = if (is_term) stderr else null;
102 self.terminal = if (stderr.supportsAnsiEscapeCodes()) stderr else null;
104103 } else |_| {
105104 self.terminal = null;
106105 }
......@@ -111,8 +110,8 @@ pub const Progress = struct {
111110 .name = name,
112111 .estimated_total_items = estimated_total_items,
113112 };
114 self.prev_refresh_timestamp = 0;
115113 self.columns_written = 0;
114 self.prev_refresh_timestamp = 0;
116115 self.timer = try std.time.Timer.start();
117116 self.done = false;
118117 return &self.root;
......@@ -133,20 +132,42 @@ pub const Progress = struct {
133132 const prev_columns_written = self.columns_written;
134133 var end: usize = 0;
135134 if (self.columns_written > 0) {
135 // restore cursor position
136136 end += (std.fmt.bufPrint(self.output_buffer[end..], "\x1b[{}D", self.columns_written) catch unreachable).len;
137137 self.columns_written = 0;
138 }
139138
140 if (!self.done) {
141 self.bufWriteNode(self.root, &end);
142 self.bufWrite(&end, "...");
139 // clear rest of line
140 end += (std.fmt.bufPrint(self.output_buffer[end..], "\x1b[0K") catch unreachable).len;
143141 }
144142
145 if (prev_columns_written > self.columns_written) {
146 const amt = prev_columns_written - self.columns_written;
147 std.mem.set(u8, self.output_buffer[end .. end + amt], ' ');
148 end += amt;
149 end += (std.fmt.bufPrint(self.output_buffer[end..], "\x1b[{}D", amt) catch unreachable).len;
143 if (!self.done) {
144 var need_ellipse = false;
145 var maybe_node: ?*Node = &self.root;
146 while (maybe_node) |node| {
147 if (need_ellipse) {
148 self.bufWrite(&end, "...");
149 }
150 need_ellipse = false;
151 if (node.name.len != 0 or node.estimated_total_items != null) {
152 if (node.name.len != 0) {
153 self.bufWrite(&end, "{}", node.name);
154 need_ellipse = true;
155 }
156 if (node.estimated_total_items) |total| {
157 if (need_ellipse) self.bufWrite(&end, " ");
158 self.bufWrite(&end, "[{}/{}] ", node.completed_items, total);
159 need_ellipse = false;
160 } else if (node.completed_items != 0) {
161 if (need_ellipse) self.bufWrite(&end, " ");
162 self.bufWrite(&end, "[{}] ", node.completed_items);
163 need_ellipse = false;
164 }
165 }
166 maybe_node = node.recently_updated_child;
167 }
168 if (need_ellipse) {
169 self.bufWrite(&end, "...");
170 }
150171 }
151172
152173 _ = file.write(self.output_buffer[0..end]) catch |e| {
......@@ -156,25 +177,14 @@ pub const Progress = struct {
156177 self.prev_refresh_timestamp = self.timer.read();
157178 }
158179
159 fn bufWriteNode(self: *Progress, node: Node, end: *usize) void {
160 if (node.name.len != 0 or node.estimated_total_items != null) {
161 if (node.name.len != 0) {
162 self.bufWrite(end, "{}", node.name);
163 if (node.recently_updated_child != null or node.estimated_total_items != null or
164 node.completed_items != 0)
165 {
166 self.bufWrite(end, "...");
167 }
168 }
169 if (node.estimated_total_items) |total| {
170 self.bufWrite(end, "[{}/{}] ", node.completed_items, total);
171 } else if (node.completed_items != 0) {
172 self.bufWrite(end, "[{}] ", node.completed_items);
173 }
174 }
175 if (node.recently_updated_child) |child| {
176 self.bufWriteNode(child.*, end);
177 }
180 pub fn log(self: *Progress, comptime format: []const u8, args: ...) void {
181 const file = self.terminal orelse return;
182 self.refresh();
183 file.outStream().stream.print(format, args) catch {
184 self.terminal = null;
185 return;
186 };
187 self.columns_written = 0;
178188 }
179189
180190 fn bufWrite(self: *Progress, end: *usize, comptime format: []const u8, args: ...) void {
......@@ -200,6 +210,12 @@ pub const Progress = struct {
200210};
201211
202212test "basic functionality" {
213 var disable = true;
214 if (disable) {
215 // This test is disabled because it uses time.sleep() and is therefore slow. It also
216 // prints bogus progress data to stderr.
217 return error.SkipZigTest;
218 }
203219 var progress = Progress{};
204220 const root_node = try progress.start("", 100);
205221 defer root_node.end();
......@@ -235,7 +251,7 @@ test "basic functionality" {
235251 var node = root_node.start("this is a really long name designed to activate the truncation code. let's find out if it works", null);
236252 node.activate();
237253 std.time.sleep(10 * std.time.millisecond);
238 progress.maybeRefresh();
254 progress.refresh();
239255 std.time.sleep(10 * std.time.millisecond);
240256 node.end();
241257 }
lib/std/special/test_runner.zig+15-10
......@@ -2,28 +2,33 @@ const std = @import("std");
22const io = std.io;
33const builtin = @import("builtin");
44const test_fn_list = builtin.test_functions;
5const warn = std.debug.warn;
65
7pub fn main() !void {
6pub fn main() anyerror!void {
87 var ok_count: usize = 0;
98 var skip_count: usize = 0;
10 for (test_fn_list) |test_fn, i| {
11 warn("{}/{} {}...", i + 1, test_fn_list.len, test_fn.name);
9 var progress = std.Progress{};
10 const root_node = progress.start("Test", test_fn_list.len) catch |err| switch (err) {
11 // TODO still run tests in this case
12 error.TimerUnsupported => @panic("timer unsupported"),
13 };
1214
15 for (test_fn_list) |test_fn, i| {
16 var test_node = root_node.start(test_fn.name, null);
17 test_node.activate();
1318 if (test_fn.func()) |_| {
1419 ok_count += 1;
15 warn("OK\n");
20 test_node.end();
1621 } else |err| switch (err) {
1722 error.SkipZigTest => {
1823 skip_count += 1;
19 warn("SKIP\n");
24 test_node.end();
25 progress.log("{}...SKIP\n", test_fn.name);
2026 },
2127 else => return err,
2228 }
2329 }
24 if (ok_count == test_fn_list.len) {
25 warn("All tests passed.\n");
26 } else {
27 warn("{} passed; {} skipped.\n", ok_count, skip_count);
30 root_node.end();
31 if (ok_count != test_fn_list.len) {
32 progress.log("{} passed; {} skipped.\n", ok_count, skip_count);
2833 }
2934}
src-self-hosted/stage1.zig+11-1
......@@ -470,13 +470,23 @@ export fn stage2_progress_destroy(progress: *std.Progress) void {
470470}
471471
472472// ABI warning
473export fn stage2_progress_start_root(progress: *std.Progress, name_ptr: [*]const u8, name_len: usize, estimated_total_items: usize) *std.Progress.Node {
473export fn stage2_progress_start_root(
474 progress: *std.Progress,
475 name_ptr: [*]const u8,
476 name_len: usize,
477 estimated_total_items: usize,
478) *std.Progress.Node {
474479 return progress.start(
475480 name_ptr[0..name_len],
476481 if (estimated_total_items == 0) null else estimated_total_items,
477482 ) catch @panic("timer unsupported");
478483}
479484
485// ABI warning
486export fn stage2_progress_disable_tty(progress: *std.Progress) void {
487 progress.terminal = null;
488}
489
480490// ABI warning
481491export fn stage2_progress_start(
482492 node: *std.Progress.Node,
src/codegen.cpp+4-4
......@@ -10479,11 +10479,11 @@ ZigPackage *codegen_create_package(CodeGen *g, const char *root_src_dir, const c
1047910479}
1048010480
1048110481CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, OutType out_type,
10482 ZigLibCInstallation *libc, const char *name, Stage2ProgressNode *child_progress_node)
10482 ZigLibCInstallation *libc, const char *name, Stage2ProgressNode *parent_progress_node)
1048310483{
10484 if (!child_progress_node) {
10485 child_progress_node = stage2_progress_start(parent_gen->progress_node, name, strlen(name), 0);
10486 }
10484 Stage2ProgressNode *child_progress_node = stage2_progress_start(
10485 parent_progress_node ? parent_progress_node : parent_gen->progress_node,
10486 name, strlen(name), 0);
1048710487
1048810488 CodeGen *child_gen = codegen_create(nullptr, root_src_path, parent_gen->zig_target, out_type,
1048910489 parent_gen->build_mode, parent_gen->zig_lib_dir, libc, get_stage1_cache_path(), false, child_progress_node);
src/main.cpp+5-2
......@@ -506,8 +506,6 @@ int main(int argc, char **argv) {
506506 ZigList<const char *> llvm_argv = {0};
507507 llvm_argv.append("zig (LLVM option parsing)");
508508
509 Stage2ProgressNode *root_progress_node = stage2_progress_start_root(stage2_progress_create(), "", 0, 0);
510
511509 if (argc >= 2 && strcmp(argv[1], "build") == 0) {
512510 Buf zig_exe_path_buf = BUF_INIT;
513511 if ((err = os_self_exe_path(&zig_exe_path_buf))) {
......@@ -589,6 +587,7 @@ int main(int argc, char **argv) {
589587 Buf *cache_dir_buf = buf_create_from_str(cache_dir);
590588 full_cache_dir = os_path_resolve(&cache_dir_buf, 1);
591589 }
590 Stage2ProgressNode *root_progress_node = stage2_progress_start_root(stage2_progress_create(), "", 0, 0);
592591
593592 CodeGen *g = codegen_create(main_pkg_path, build_runner_path, &target, OutTypeExe,
594593 BuildModeDebug, override_lib_dir, nullptr, &full_cache_dir, false, root_progress_node);
......@@ -965,6 +964,10 @@ int main(int argc, char **argv) {
965964 return EXIT_FAILURE;
966965 }
967966
967 Stage2Progress *progress = stage2_progress_create();
968 Stage2ProgressNode *root_progress_node = stage2_progress_start_root(progress, "", 0, 0);
969 if (color == ErrColorOff) stage2_progress_disable_tty(progress);
970
968971 init_all_targets();
969972
970973 ZigTarget target;
src/userland.cpp+1
......@@ -87,3 +87,4 @@ Stage2ProgressNode *stage2_progress_start(Stage2ProgressNode *node,
8787}
8888void stage2_progress_end(Stage2ProgressNode *node) {}
8989void stage2_progress_complete_one(Stage2ProgressNode *node) {}
90void stage2_progress_disable_tty(Stage2Progress *progress) {}
src/userland.h+2
......@@ -163,6 +163,8 @@ struct Stage2ProgressNode;
163163// ABI warning
164164ZIG_EXTERN_C Stage2Progress *stage2_progress_create(void);
165165// ABI warning
166ZIG_EXTERN_C void stage2_progress_disable_tty(Stage2Progress *progress);
167// ABI warning
166168ZIG_EXTERN_C void stage2_progress_destroy(Stage2Progress *progress);
167169// ABI warning
168170ZIG_EXTERN_C Stage2ProgressNode *stage2_progress_start_root(Stage2Progress *progress,
test/cli.zig+2-2
......@@ -87,7 +87,7 @@ fn exec(cwd: []const u8, argv: []const []const u8) !ChildProcess.ExecResult {
8787fn testZigInitLib(zig_exe: []const u8, dir_path: []const u8) !void {
8888 _ = try exec(dir_path, [_][]const u8{ zig_exe, "init-lib" });
8989 const test_result = try exec(dir_path, [_][]const u8{ zig_exe, "build", "test" });
90 testing.expect(std.mem.endsWith(u8, test_result.stderr, "All tests passed.\n"));
90 testing.expect(std.mem.eql(u8, test_result.stderr, ""));
9191}
9292
9393fn testZigInitExe(zig_exe: []const u8, dir_path: []const u8) !void {
......@@ -136,6 +136,6 @@ fn testMissingOutputPath(zig_exe: []const u8, dir_path: []const u8) !void {
136136 const output_path = try fs.path.join(a, [_][]const u8{ "does", "not", "exist" });
137137 const source_path = try fs.path.join(a, [_][]const u8{ "src", "main.zig" });
138138 _ = try exec(dir_path, [_][]const u8{
139 zig_exe, "build-exe", source_path, "--output-dir", output_path
139 zig_exe, "build-exe", source_path, "--output-dir", output_path,
140140 });
141141}