From 578dd730f9a81580376c1423ae60f2009cc2d267 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 26 Oct 2023 23:01:55 -0700 Subject: [PATCH] terminal: CUB with reverse wrap on first row should not crash --- src/terminal/Terminal.zig | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index 9f45aebdc..05e0f0d59 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -1464,12 +1464,6 @@ pub fn cursorLeft(self: *Terminal, count_req: usize) void { continue; } - // If our previous line is not wrapped then we are done. - if (wrap_mode != .reverse_extended) { - const row = self.screen.getRow(.{ .active = self.screen.cursor.y - 1 }); - if (!row.isWrapped()) break; - } - // UNDEFINED TERMINAL BEHAVIOR. This situation is not handled in xterm // and currently results in a crash in xterm. Given no other known // terminal [to me] implements XTREVWRAP2, I decided to just mimick @@ -1482,6 +1476,12 @@ pub fn cursorLeft(self: *Terminal, count_req: usize) void { break; } + // If our previous line is not wrapped then we are done. + if (wrap_mode != .reverse_extended) { + const row = self.screen.getRow(.{ .active = self.screen.cursor.y - 1 }); + if (!row.isWrapped()) break; + } + self.screen.cursor.y -= 1; self.screen.cursor.x = right_margin; count -= 1; @@ -6154,6 +6154,22 @@ test "Terminal: cursorLeft extended reverse wrap above top scroll region" { try testing.expectEqual(@as(usize, 0), t.screen.cursor.y); } +test "Terminal: cursorLeft reverse wrap on first row" { + const alloc = testing.allocator; + var t = try init(alloc, 5, 5); + defer t.deinit(alloc); + + t.modes.set(.wraparound, true); + t.modes.set(.reverse_wrap, true); + + t.setTopAndBottomMargin(3, 0); + t.setCursorPos(1, 2); + t.cursorLeft(1000); + + try testing.expectEqual(@as(usize, 0), t.screen.cursor.x); + try testing.expectEqual(@as(usize, 0), t.screen.cursor.y); +} + test "Terminal: cursorDown basic" { const alloc = testing.allocator; var t = try init(alloc, 5, 5);