Skip to content

Commit aae2d50

Browse files
gcgbarbosameszmate
andauthored
fix redrawing & vanishing last column (#145)
* fix(frame): Clear the rows the previous frame used and the current one does not. * test(frame): test the correct bytes when redrawing writer.writeAll(ansi.cursor_home) prints the byte \x1b[H (go to row 1, column 1 but ommit params) I changed it to: ansi.cursorTo0(writer, @intcast(line_count), 0) which prints the byte \x1b[1;1H (go to row 1, column 1, exclicit params) (it actually prints \x1b[{row+1};1H, but you get the idea) * fix(frame): bound row addressing, and erase when the width is unknown Addressing rows absolutely removes the scroll that #143 reported, and skipping `EL` on a line that fills the row removes the erased border that #144 reported. Both need bounds the original change did not apply. A `CUP` row is a `u16`, and the terminal clamps it to the screen: * A view of 65536 lines or more overflowed the row counter and panicked. `writeFull` is the path that absorbs oversized frames, so the crash sat on exactly the input it exists to handle, and `.full` mode never bounds the line count at all. * Rows past the bottom clamped onto the last row, so each overflow line overwrote the one before it: six lines on a three-row screen left `a`, `b`, `f` rather than the top three. Count those lines, so `last_line_count` still describes the frame, but do not draw them. `writeDiff` bounds its trailing clear the same way. It cannot reach a `last_line_count` past the screen today — an oversized frame marks the renderer dirty, which routes the next frame through `writeFull` — but two paths clearing to different limits is a trap, not an invariant. The erase predicate needs two corrections of its own, both in `fillsRow`: * `width < size.width` is never true when the width is zero, which suppressed every erase and left stale text on screen. `TIOCGWINSZ` reports 0x0 for a terminal whose window size was never set, and the 80x24 fallback in `getSize` only covers the non-TTY case. An unknown width says nothing about which lines fill a row, so erase. * A line that wraps returns to the edge at every multiple of the row width, not only when it fills one row exactly. Ten columns of text on a five-column screen parks the cursor on the edge a second time, where `EL` eats the last character just the same. Test the width modulo the row rather than against it. * test(frame): model autowrap, and assert repaints by content Two gaps let the renderer's own regressions through. `VirtualScreen` counted bytes, not columns, and knew nothing of the screen it was modelling. It could not represent the bug behind #144 at all: a character written to the last column parks the cursor there with its wrap deferred, and `EL` erases from the column the cursor occupies — taking that character with it. A frame drawn to the full width of the screen modelled perfectly and still lost its right-hand border on a real terminal. Model columns, autowrap, wrap-pending and `CUP` clamping, so the harness can show what a terminal would. `\x1b[H` used to be proof of a full repaint, because only `writeFull` emitted it. Absolute addressing made it `\x1b[1;1H`, which `writeDiff` also emits whenever row 0 is the row that changed. Every rewritten assertion still discriminated by luck — each left row 0 untouched — but they no longer tested what their names claimed. Assert instead that a row which did *not* change was rewritten, which is the actual difference between the two paths. Covers what neither path had: a frame that fills the width, a wrapped line that ends on the edge, truncation past the bottom row, a view too tall for a `u16` row, and an unknown terminal width. --------- Co-authored-by: Matthew Meszaros <meszmatew@gmail.com>
1 parent 99dd623 commit aae2d50

2 files changed

Lines changed: 336 additions & 60 deletions

File tree

‎src/terminal/frame.zig‎

Lines changed: 83 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub const Renderer = struct {
8484
const line_count = if (use_diff)
8585
try self.writeDiff(writer, view, size)
8686
else
87-
try self.writeFull(writer, view);
87+
try self.writeFull(writer, view, size);
8888
try writer.writeAll(ansi.sync_end);
8989

9090
self.remember(view, line_count, hash);
@@ -111,28 +111,35 @@ pub const Renderer = struct {
111111
self.dirty = false;
112112
}
113113

114-
/// Rewrite the whole frame from the top-left corner.
115-
fn writeFull(self: *Renderer, writer: *Writer, view: []const u8) !usize {
116-
try writer.writeAll(ansi.cursor_home);
114+
/// Rewrite the whole frame, addressing every row absolutely.
115+
///
116+
/// Absolute addressing rather than `\r\n` between lines: a newline on the
117+
/// bottom row scrolls the screen, which silently shifts every row of the
118+
/// frame up by one and leaves the diff path writing to the wrong rows from
119+
/// then on.
120+
fn writeFull(self: *Renderer, writer: *Writer, view: []const u8, size: Size) !usize {
121+
const max_row = addressableRows(size);
117122

118123
var lines = std.mem.splitScalar(u8, view, '\n');
119-
var first = true;
120124
var line_count: usize = 0;
121125
while (lines.next()) |line| {
122-
if (!first) try writer.writeAll("\r\n");
123-
first = false;
126+
defer line_count += 1;
127+
// A row past the bottom cannot be addressed — the terminal clamps
128+
// the cursor to the last row, so every line beyond it would land
129+
// on top of the one before. Count them so `last_line_count` still
130+
// describes the frame, but do not draw them.
131+
if (line_count >= max_row) continue;
132+
133+
try ansi.cursorTo0(writer, @intCast(line_count), 0);
124134
try writer.writeAll(line);
125-
try writer.writeAll(ansi.line_clear_right);
126-
line_count += 1;
135+
if (!fillsRow(line, size)) try writer.writeAll(ansi.line_clear_right);
127136
}
128137

129138
// Clear the rows the previous frame used and this one does not.
130-
if (self.last_line_count > line_count) {
131-
var remaining = self.last_line_count - line_count;
132-
while (remaining > 0) : (remaining -= 1) {
133-
try writer.writeAll("\r\n");
134-
try writer.writeAll(ansi.line_clear);
135-
}
139+
var row = line_count;
140+
while (row < self.last_line_count and row < max_row) : (row += 1) {
141+
try ansi.cursorTo0(writer, @intCast(row), 0);
142+
try writer.writeAll(ansi.line_clear);
136143
}
137144

138145
return line_count;
@@ -158,12 +165,12 @@ pub const Renderer = struct {
158165

159166
try ansi.cursorTo0(writer, row, 0);
160167
try writer.writeAll(line);
161-
try writer.writeAll(ansi.line_clear_right);
168+
if (!fillsRow(line, size)) try writer.writeAll(ansi.line_clear_right);
162169
}
163170

164171
// Clear the rows the previous frame used and this one does not.
165172
var row = line_count;
166-
while (row < self.last_line_count) : (row += 1) {
173+
while (row < self.last_line_count and row < addressableRows(size)) : (row += 1) {
167174
try ansi.cursorTo0(writer, @intCast(row), 0);
168175
try writer.writeAll(ansi.line_clear);
169176
}
@@ -182,6 +189,36 @@ pub const Renderer = struct {
182189
}
183190
};
184191

192+
/// How many rows can be addressed with `CUP`, which carries a `u16` row.
193+
///
194+
/// A height of zero means the size is not known — `TIOCGWINSZ` reports it that
195+
/// way on a terminal whose window size has never been set. Fall back to the
196+
/// widest row the sequence can express so an oversized view cannot overflow
197+
/// the row counter.
198+
fn addressableRows(size: Size) usize {
199+
return if (size.height > 0) size.height else std.math.maxInt(u16);
200+
}
201+
202+
/// Whether writing `line` leaves the cursor parked at the far edge of a row.
203+
///
204+
/// A terminal that has just filled the last column keeps the cursor on it with
205+
/// the wrap deferred until the next character arrives. `EL` erases from the
206+
/// column the cursor sits on, so issuing it in that state destroys the
207+
/// character that was just written — the right-hand border of a frame drawn to
208+
/// the full width of the screen. Nothing follows such a line on its row, so
209+
/// the erase is skipped rather than fixed up.
210+
///
211+
/// A line long enough to wrap lands on the edge whenever its width is an exact
212+
/// multiple of the row width, not only when it fills a single row.
213+
fn fillsRow(line: []const u8, size: Size) bool {
214+
// Size unknown: erase, because leaving stale text on screen is the worse
215+
// of the two failures.
216+
if (size.width == 0) return false;
217+
218+
const w = measure.width(line);
219+
return w > 0 and w % size.width == 0;
220+
}
221+
185222
/// What a frame looks like on screen, as far as the diff path cares.
186223
const Shape = struct {
187224
/// Every line fits on one row and the frame fits on screen, so row `n` of
@@ -384,6 +421,35 @@ test "leavesStyleOpen: hyperlinks" {
384421
try std.testing.expect(!leavesStyleOpen("\x1b]8;;https://example.com\x1b\\link\x1b]8;;\x1b\\"));
385422
}
386423

424+
test "fillsRow: a line that ends on the row edge" {
425+
const size = Size{ .width = 5, .height = 4 };
426+
427+
try std.testing.expect(!fillsRow("", size));
428+
try std.testing.expect(!fillsRow("abc", size));
429+
try std.testing.expect(fillsRow("abcde", size));
430+
431+
// A line long enough to wrap comes back to the edge at every multiple of
432+
// the row width, not only on the first row it fills.
433+
try std.testing.expect(!fillsRow("abcdefgh", size));
434+
try std.testing.expect(fillsRow("abcdefghij", size));
435+
436+
// Escape sequences occupy no columns; double-width characters occupy two.
437+
try std.testing.expect(fillsRow("\x1b[31mabcde\x1b[0m", size));
438+
try std.testing.expect(!fillsRow("日本", size));
439+
try std.testing.expect(fillsRow("日本語だよ", size));
440+
441+
// An unknown width cannot rule the erase out.
442+
try std.testing.expect(!fillsRow("abcde", .{ .width = 0, .height = 0 }));
443+
}
444+
445+
test "addressableRows: an unknown height falls back to the sequence limit" {
446+
try std.testing.expectEqual(@as(usize, 24), addressableRows(.{ .width = 80, .height = 24 }));
447+
try std.testing.expectEqual(
448+
@as(usize, std.math.maxInt(u16)),
449+
addressableRows(.{ .width = 80, .height = 0 }),
450+
);
451+
}
452+
387453
test "scan: frame geometry" {
388454
const size = Size{ .width = 10, .height = 3 };
389455

0 commit comments

Comments
 (0)