From 8435d45bc946c9b05eba1d09a40fe05a51ccae99 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 18 Aug 2023 08:32:13 -0700 Subject: [PATCH] terminal: proper reverseIndex behavior with scroll region set Fixes #298 This fix is quite simple and the code should be more or less obvious: we weren't handling scroll regions properly for the reverse index (RI) escape sequence. --- src/terminal/Terminal.zig | 41 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index c071b12db..67fa95889 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -909,9 +909,11 @@ pub fn reverseIndex(self: *Terminal) !void { const tracy = trace(@src()); defer tracy.end(); - // TODO: scrolling region - - if (self.screen.cursor.y == 0) { + // If the cursor is on the top-most line of the scroll region or + // its on the top of the screen we scroll down. + if (self.screen.cursor.y == self.scrolling_region.top or + self.screen.cursor.y == 0) + { try self.scrollDown(1); } else { self.screen.cursor.y -|= 1; @@ -2222,6 +2224,39 @@ test "Terminal: reverseIndex from the top" { } } +test "Terminal: reverseIndex top of scrolling region" { + const alloc = testing.allocator; + var t = try init(alloc, 2, 10); + defer t.deinit(alloc); + + // Initial value + t.setCursorPos(2, 1); + try t.print('A'); + t.carriageReturn(); + try t.linefeed(); + try t.print('B'); + t.carriageReturn(); + try t.linefeed(); + try t.print('C'); + t.carriageReturn(); + try t.linefeed(); + try t.print('D'); + t.carriageReturn(); + try t.linefeed(); + + // Set our scroll region + t.setScrollingRegion(2, 5); + t.setCursorPos(2, 1); + try t.reverseIndex(); + try t.print('X'); + + { + var str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("\nX\nA\nB\nC", str); + } +} + test "Terminal: index" { const alloc = testing.allocator; var t = try init(alloc, 2, 5);