Commit 11f894702b

Andrew Kelley <andrew@ziglang.org>
2024-05-27 17:38:02
std.Progress: avoid scrolling the PS1 off the terminal
1 parent 52ed54d
Changed files (1)
lib
lib/std/Progress.zig
@@ -908,13 +908,13 @@ fn computeNode(
         global_progress.newline_count += 1;
     }
 
-    if (global_progress.newline_count < global_progress.rows) {
+    if (global_progress.withinRowLimit()) {
         if (children[@intFromEnum(node_index)].child.unwrap()) |child| {
             i = computeNode(buf, i, serialized, children, child);
         }
     }
 
-    if (global_progress.newline_count < global_progress.rows) {
+    if (global_progress.withinRowLimit()) {
         if (children[@intFromEnum(node_index)].sibling.unwrap()) |sibling| {
             i = computeNode(buf, i, serialized, children, sibling);
         }
@@ -923,6 +923,11 @@ fn computeNode(
     return i;
 }
 
+fn withinRowLimit(p: *Progress) bool {
+    // The +1 here is so that the PS1 is not scrolled off the top of the terminal.
+    return p.newline_count + 1 < p.rows;
+}
+
 fn write(buf: []const u8) void {
     const tty = global_progress.terminal orelse return;
     tty.writeAll(buf) catch {