From 28732b950c78f91cb0bdce9d3d9ebe354df59639 Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Sun, 21 Jan 2024 06:41:43 -0600 Subject: [PATCH 1/2] reflow: respect wraparound mode when reflowing text When calling resize, Ghostty should be respecting the wraparound state. This behavior matches xterm. Note that this same bug was also found in kitty. Fixes: #1343 --- src/terminal/Terminal.zig | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index 2f87ae0d5..97b5d9c63 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -330,11 +330,19 @@ pub fn resize(self: *Terminal, alloc: Allocator, cols: usize, rows: usize) !void // If we're making the screen smaller, dealloc the unused items. if (self.active_screen == .primary) { self.clearPromptForResize(); - try self.screen.resize(rows, cols); + if (self.modes.get(.wraparound)) { + try self.screen.resize(rows, cols); + } else { + try self.screen.resizeWithoutReflow(rows, cols); + } try self.secondary_screen.resizeWithoutReflow(rows, cols); } else { try self.screen.resizeWithoutReflow(rows, cols); - try self.secondary_screen.resize(rows, cols); + if (self.modes.get(.wraparound)) { + try self.secondary_screen.resize(rows, cols); + } else { + try self.secondary_screen.resizeWithoutReflow(rows, cols); + } } // Set our size From 6dcec75e3243810c0269e8273b8d9a45dbdf1bb7 Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Sun, 21 Jan 2024 07:02:06 -0600 Subject: [PATCH 2/2] reflow: add unit tests for wraparound mode Add unit tests for resizing with wraparound on and off. --- src/terminal/Terminal.zig | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index 97b5d9c63..c93a061b4 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -5099,6 +5099,47 @@ test "Terminal: resize with left and right margin set" { try t.resize(alloc, cols, rows); } +// https://github.com/mitchellh/ghostty/issues/1343 +test "Terminal: resize with wraparound off" { + const alloc = testing.allocator; + const cols = 4; + const rows = 2; + var t = try init(alloc, cols, rows); + defer t.deinit(alloc); + + t.modes.set(.wraparound, false); + try t.print('0'); + try t.print('1'); + try t.print('2'); + try t.print('3'); + const new_cols = 2; + try t.resize(alloc, new_cols, rows); + + const str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("01", str); +} + +test "Terminal: resize with wraparound on" { + const alloc = testing.allocator; + const cols = 4; + const rows = 2; + var t = try init(alloc, cols, rows); + defer t.deinit(alloc); + + t.modes.set(.wraparound, true); + try t.print('0'); + try t.print('1'); + try t.print('2'); + try t.print('3'); + const new_cols = 2; + try t.resize(alloc, new_cols, rows); + + const str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("01\n23", str); +} + test "Terminal: saveCursor" { const alloc = testing.allocator; var t = try init(alloc, 3, 3);