diff --git a/src/terminal/Screen.zig b/src/terminal/Screen.zig index 564439e45..7bdfaa46b 100644 --- a/src/terminal/Screen.zig +++ b/src/terminal/Screen.zig @@ -1838,9 +1838,6 @@ pub fn resize(self: *Screen, rows: usize, cols: usize) !void { return; } - // We grow rows first so we can make space for more reflow - if (rows > self.rows) try self.resizeWithoutReflow(rows, cols); - // If our columns increased, we alloc space for the new column width // and go through each row and reflow if necessary. if (cols > self.cols) { @@ -2014,6 +2011,10 @@ pub fn resize(self: *Screen, rows: usize, cols: usize) !void { } } + // We grow rows after cols so that we can do our unwrapping/reflow + // before we do a no-reflow grow. + if (rows > self.rows) try self.resizeWithoutReflow(rows, cols); + // If our rows got smaller, we trim the scrollback. We do this after // handling cols growing so that we can save as many lines as we can. // We do it before cols shrinking so we can save compute on that operation. @@ -4058,6 +4059,39 @@ test "Screen: resize more rows with populated scrollback" { } } +test "Screen: resize more rows and cols with wrapping" { + const testing = std.testing; + const alloc = testing.allocator; + + var s = try init(alloc, 4, 2, 0); + defer s.deinit(); + const str = "1A2B\n3C4D"; + try s.testWriteString(str); + { + var contents = try s.testString(alloc, .viewport); + defer alloc.free(contents); + const expected = "1A\n2B\n3C\n4D"; + try testing.expectEqualStrings(expected, contents); + } + + try s.resize(10, 5); + + // Cursor should move due to wrapping + try testing.expectEqual(@as(usize, 3), s.cursor.x); + try testing.expectEqual(@as(usize, 1), s.cursor.y); + + { + var contents = try s.testString(alloc, .viewport); + defer alloc.free(contents); + try testing.expectEqualStrings(str, contents); + } + { + var contents = try s.testString(alloc, .screen); + defer alloc.free(contents); + try testing.expectEqualStrings(str, contents); + } +} + test "Screen: resize more cols no reflow" { const testing = std.testing; const alloc = testing.allocator;