Skip to content

build_runner: add option to clear console when watching #24122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/compiler/build_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub fn main() !void {
var steps_menu = false;
var output_tmp_nonce: ?[16]u8 = null;
var watch = false;
var watch_clear = false;
var fuzz = false;
var debounce_interval_ms: u16 = 50;
var listen_port: u16 = 0;
Expand Down Expand Up @@ -266,6 +267,8 @@ pub fn main() !void {
prominent_compile_errors = true;
} else if (mem.eql(u8, arg, "--watch")) {
watch = true;
} else if (mem.eql(u8, arg, "--watch-clear")) {
watch_clear = true;
} else if (mem.eql(u8, arg, "--fuzz")) {
fuzz = true;
} else if (mem.eql(u8, arg, "-fincremental")) {
Expand Down Expand Up @@ -484,6 +487,10 @@ pub fn main() !void {
var debounce_timeout: Watch.Timeout = .none;
while (true) switch (try w.wait(gpa, debounce_timeout)) {
.timeout => {
if (watch_clear) {
try std.io.getStdOut().writeAll("\x1B[2J\x1B[H");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, "\x1B[2J\x1B[H" should be moved into std.io.tty.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also note that std.Progress already has code like this so that could be refactored.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've looked at std.io.tty and it seems to handle only colors not the entire suite of ANSI escape codes. std.Progress on the other hand uses some ANSI codes but it's not the main use of the API and I would rather not import from it.

To me it feels like std.io.tty should hold the ANSI escapes for the screen clearing as well. Does this mean that NO_COLOR/--color off should also disable screen clearing? My assumption is that the main reason you would disable colors is to get rid of the ANSI escapes from the output. Maybe someone from the core team can shed some light.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that NO_COLOR/--color off should also disable screen clearing?

https://no-color.org seems to me to strongly indicate that it's about a preference of colored vs non-colored output, not terminal sequences in general. So I would say no.

}

debouncing_node.end();
markFailedStepsDirty(gpa, run.step_stack.keys());
continue :rebuild;
Expand Down Expand Up @@ -1305,6 +1312,7 @@ fn usage(b: *std.Build, out_stream: anytype) !void {
\\ needed (Default) Lazy dependencies are fetched as needed
\\ all Lazy dependencies are always fetched
\\ --watch Continuously rebuild when source files are modified
\\ --watch-clear Clear screen before each rebuild
\\ --fuzz Continuously search for unit test failures
\\ --debounce <ms> Delay before rebuilding after changed file detected
\\ -fincremental Enable incremental compilation
Expand Down