From 705bd210552fc89d3d22514624c3eb720236f70b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 25 Mar 2024 10:18:31 -0700 Subject: [PATCH] terminal: PageList trim blanks erases empty pages Fixes #1605 --- src/terminal/PageList.zig | 40 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/terminal/PageList.zig b/src/terminal/PageList.zig index 6ed34328d..ea6188b95 100644 --- a/src/terminal/PageList.zig +++ b/src/terminal/PageList.zig @@ -1554,7 +1554,12 @@ fn trimTrailingBlankRows( // no text we can also be sure it has no styling // so we don't need to worry about memory. row_pin.page.data.size.rows -= 1; - row_pin.page.data.assertIntegrity(); + if (row_pin.page.data.size.rows == 0) { + self.erasePage(row_pin.page); + } else { + row_pin.page.data.assertIntegrity(); + } + trimmed += 1; if (trimmed >= max) return trimmed; } @@ -4687,6 +4692,39 @@ test "PageList resize (no reflow) less rows trims blank lines cursor in blank li } }, s.pointFromPin(.active, p.*).?); } +test "PageList resize (no reflow) less rows trims blank lines erases pages" { + const testing = std.testing; + const alloc = testing.allocator; + + var s = try init(alloc, 100, 5, 0); + defer s.deinit(); + try testing.expect(s.pages.first == s.pages.last); + const page = &s.pages.first.?.data; + + // Resize to take up two pages + { + const rows = page.capacity.rows + 10; + try s.resize(.{ .rows = rows, .reflow = false }); + try testing.expectEqual(@as(usize, 2), s.totalPages()); + } + + // Write codepoint into first line + { + const rac = page.getRowAndCell(0, 0); + rac.cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = 'A' }, + }; + } + + // Resize down. Every row except the first is blank so we + // should erase the second page. + try s.resize(.{ .rows = 5, .reflow = false }); + try testing.expectEqual(@as(usize, 5), s.rows); + try testing.expectEqual(@as(usize, 5), s.totalRows()); + try testing.expectEqual(@as(usize, 1), s.totalPages()); +} + test "PageList resize (no reflow) more rows extends blank lines" { const testing = std.testing; const alloc = testing.allocator;