From 54d6654d8bee8eefbc196a81a7898274865baa9e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 17 Mar 2023 11:07:43 -0700 Subject: [PATCH] terminal: selectLine can select last line with scrollback We had incorrect logic around when the line you want to select is the last line and it crashed. A new test case covers this. --- src/terminal/Screen.zig | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/terminal/Screen.zig b/src/terminal/Screen.zig index dd1e7b223..f50d4dfa6 100644 --- a/src/terminal/Screen.zig +++ b/src/terminal/Screen.zig @@ -1175,7 +1175,7 @@ pub fn selectLine(self: *Screen, pt: point.ScreenPoint) ?Selection { // The real end of the row is the final row in the soft-wrap. const end_row: usize = end_row: { var y: usize = pt.y; - while (y < y_max) : (y += 1) { + while (y <= y_max) : (y += 1) { const current = self.getRow(.{ .screen = y }); if (y == y_max or !current.header().flags.wrap) break :end_row y; } @@ -3060,6 +3060,33 @@ test "Screen: selectLine across soft-wrap ignores blank lines" { } } +test "Screen: selectLine with scrollback" { + const testing = std.testing; + const alloc = testing.allocator; + + var s = try init(alloc, 3, 2, 5); + defer s.deinit(); + try s.testWriteString("1A\n2B\n3C\n4D\n5E"); + + // Selecting first line + { + const sel = s.selectLine(.{ .x = 0, .y = 0 }).?; + try testing.expectEqual(@as(usize, 0), sel.start.x); + try testing.expectEqual(@as(usize, 0), sel.start.y); + try testing.expectEqual(@as(usize, 1), sel.end.x); + try testing.expectEqual(@as(usize, 0), sel.end.y); + } + + // Selecting last line + { + const sel = s.selectLine(.{ .x = 0, .y = 4 }).?; + try testing.expectEqual(@as(usize, 0), sel.start.x); + try testing.expectEqual(@as(usize, 4), sel.start.y); + try testing.expectEqual(@as(usize, 1), sel.end.x); + try testing.expectEqual(@as(usize, 4), sel.end.y); + } +} + test "Screen: selectWord" { const testing = std.testing; const alloc = testing.allocator;