Clear terminal screen on rebuild when using zig build test --watch

I want the terminal screen to clear each time a change is detected by zig build test --watch.
I made a hacky prototype, but it only works for the file src/main.zig.
The main issue is that the step gets cached and doesn’t execute on subsequent rebuilds.

    const clear_screen_step: *std.Build.Step = b.allocator.create(std.Build.Step) catch unreachable;
    clear_screen_step.* = std.Build.Step.init(.{
        .id = std.Build.Step.Id.custom,
        .name = "clear terminal screen",
        .owner = main_exe.step.owner,
        .makeFn = (struct {
            pub fn call(step: *std.Build.Step, options: std.Build.Step.MakeOptions) anyerror!void {
                _ = step;
                _ = options;
                std.debug.print("\x1B[2J\x1B[H", .{});
            }
        }).call,
    });
    clear_screen_step.addWatchInput(.{ .cwd_relative = "src/main.zig" }) catch unreachable;

--watch doesnt have that feature, but it probably will be in the future

2 Likes