Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

fix(tui): expand tabs before painting prompt rows

yxlyxd521bea45809
diff --git a/TUI/paint.zig b/TUI/paint.zig
index 48e02859..34f9640b 100644
--- a/TUI/paint.zig
+++ b/TUI/paint.zig
@@ -98,9 +98,10 @@ pub fn paintRow(w: *Io.Writer, ln: []const u8, cols: usize, bg: []const u8) !voi
try w.writeAll(bg);
// Measure and write the same cells: a smuggled CR/BEL/BS is not a column
// and must not reach the terminal (CR rewinds the row, BEL rings, BS
- // walks the cursor back). Tab stays — it is legal prose. ESC stays —
- // that is SGR. The frame builders already strip this; this is the last
- // door so a leak cannot desync the row↔line map.
+ // walks the cursor back). Tab expands to the next stop of 8 — counting
+ // it as one cell leaves pad() short and can preserve the previous frame.
+ // ESC stays — that is SGR. The frame builders already strip this; this is
+ // the last door so a leak cannot desync the row↔line map.
const vis = cellLen(ln);
if (vis < cols) {
try writeCells(w, ln);
@@ -128,12 +129,17 @@ fn pad(w: *Io.Writer, cells: usize) !void {
while (n > 0) : (n -= 1) try w.writeByte(' ');
}
-/// C0/DEL that must never be a cell. Tab is kept (legal prose); ESC is kept
-/// (SGR) and handled by the callers before they reach this.
+/// C0/DEL that must never be a cell. Tab is expanded to spaces by the callers;
+/// ESC is kept (SGR) and handled before it reaches this predicate.
fn isRowControl(b: u8) bool {
return (b < 0x20 and b != '\t') or b == 0x7f;
}
+/// Columns a tab occupies at `col`, matching terminal stops of 8.
+fn tabPad(col: usize) usize {
+ return 8 - (col % 8);
+}
+
fn cellLen(ln: []const u8) usize {
var n: usize = 0;
var i: usize = 0;
@@ -142,6 +148,11 @@ fn cellLen(ln: []const u8) usize {
i = theme_mod.skipEsc(ln, i);
continue;
}
+ if (ln[i] == '\t') {
+ n += tabPad(n);
+ i += 1;
+ continue;
+ }
if (isRowControl(ln[i])) {
i += 1;
continue;
@@ -161,6 +172,13 @@ fn takeCells(ln: []const u8, max: usize) []const u8 {
i = theme_mod.skipEsc(ln, i);
continue;
}
+ if (ln[i] == '\t') {
+ const width = tabPad(n);
+ if (n + width > max) break;
+ n += width;
+ i += 1;
+ continue;
+ }
if (isRowControl(ln[i])) {
i += 1;
continue;
@@ -175,6 +193,7 @@ fn takeCells(ln: []const u8, max: usize) []const u8 {
fn writeCells(w: *Io.Writer, ln: []const u8) !void {
var i: usize = 0;
+ var col: usize = 0;
while (i < ln.len) {
if (ln[i] == 0x1b) {
const e = theme_mod.skipEsc(ln, i);
@@ -182,14 +201,22 @@ fn writeCells(w: *Io.Writer, ln: []const u8) !void {
i = e;
continue;
}
+ if (ln[i] == '\t') {
+ const width = tabPad(col);
+ try pad(w, width);
+ col += width;
+ i += 1;
+ continue;
+ }
if (isRowControl(ln[i])) {
i += 1;
continue;
}
const start = i;
i += 1;
- while (i < ln.len and ln[i] != 0x1b and !isRowControl(ln[i])) : (i += 1) {}
+ while (i < ln.len and ln[i] != 0x1b and ln[i] != '\t' and !isRowControl(ln[i])) : (i += 1) {}
try w.writeAll(ln[start..i]);
+ col += theme_mod.visibleLen(ln[start..i]);
}
}
diff --git a/TUI/paint_tests.zig b/TUI/paint_tests.zig
index 23d0fdd8..dc46d83b 100644
--- a/TUI/paint_tests.zig
+++ b/TUI/paint_tests.zig
@@ -480,13 +480,29 @@ test "a smuggled CR or BEL in a frame row never reaches the terminal" {
try screen.expectMatches(&want);
}
-test "SGR and tab survive the cell filter; only C0 is dropped" {
+test "SGR survives the cell filter and tabs expand to terminal stops" {
const a = std.testing.allocator;
+ // "red" is 3 cells; tab -> 5 spaces (stop 8); "a" then tab -> 7 spaces.
const frame = "\x1b[31mred\x1b[0m\ta\tb";
const out = try paintToBuf(a, frame, 1, 20, "");
defer a.free(out);
- try std.testing.expect(std.mem.indexOf(u8, out, "\x1b[31mred\x1b[0m") != null);
- try std.testing.expect(std.mem.indexOf(u8, out, "\ta\tb") != null);
+ try std.testing.expect(std.mem.indexOf(u8, out, "\x1b[31mred\x1b[0m a b") != null);
+ try std.testing.expect(std.mem.indexOfScalar(u8, out, '\t') == null);
+}
+
+test "a tabbed code row shrinks without leaving the previous prompt behind (#798)" {
+ const a = std.testing.allocator;
+ var arena = std.heap.ArenaAllocator.init(a);
+ defer arena.deinit();
+ const ar = arena.allocator();
+ const cols: usize = 20;
+ const recalled = "XXXXXXXXXXXXXXXXXXXX";
+ const fresh = "ab\tcd";
+ var screen = try Screen.init(ar, 1, cols);
+ screen.feed(try paintToBuf(ar, recalled, 1, cols, ""));
+ screen.feed(try paintToBuf(ar, fresh, 1, cols, recalled));
+ var want = try Screen.expect(ar, "ab cd", 1, cols, true);
+ try screen.expectMatches(&want);
}
test "history C0 never reaches the composed frame or the paint stream" {