From e6a23be99abd1683a0756577899486823de148bc Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 24 Oct 2023 09:24:02 -0700 Subject: [PATCH] terminal: cub with reverse mode on left margin --- src/terminal/Terminal.zig | 43 +++++++++++++++++++++++++++++++++++-- website/app/vt/cub/page.mdx | 22 ++++++++++++++++--- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index 16e12deae..f7f50de7f 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -1413,6 +1413,25 @@ pub fn cursorLeft(self: *Terminal, count_req: usize) void { else self.scrolling_region.left; + // Handle some edge cases when our cursor is already on the left margin. + if (self.screen.cursor.x == left_margin) { + switch (wrap_mode) { + // In reverse mode, if we're already before the top margin + // then we just set our cursor to the top-left and we're done. + .reverse => if (self.screen.cursor.y <= top) { + self.screen.cursor.x = left_margin; + self.screen.cursor.y = top; + return; + }, + + // Handled in while loop + .reverse_extended => {}, + + // Handled above + .none => unreachable, + } + } + while (true) { // We can move at most to the left margin. const max = self.screen.cursor.x - left_margin; @@ -1437,8 +1456,10 @@ pub fn cursorLeft(self: *Terminal, count_req: usize) void { } // If our previous line is not wrapped then we are done. - const row = self.screen.getRow(.{ .active = self.screen.cursor.y - 1 }); - if (wrap_mode != .reverse_extended and !row.isWrapped()) break; + 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; @@ -5934,6 +5955,24 @@ test "Terminal: cursorLeft reverse wrap with no soft wrap" { } } +test "Terminal: cursorLeft reverse wrap before left margin" { + 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.cursorLeft(1); + try t.print('X'); + + { + var str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("\n\nX", str); + } +} + test "Terminal: cursorLeft extended reverse wrap" { const alloc = testing.allocator; var t = try init(alloc, 5, 5); diff --git a/website/app/vt/cub/page.mdx b/website/app/vt/cub/page.mdx index 4b252bdec..1a77af84f 100644 --- a/website/app/vt/cub/page.mdx +++ b/website/app/vt/cub/page.mdx @@ -12,8 +12,7 @@ or equal to 0, adjust `n` to be 1. If `n` is omitted, `n` defaults to 1. This sequence always unsets the pending wrap state. The leftmost boundary the cursor can move to is determined by the current -cursor column and the [left margin](#TODO). If the cursor begins to the left -of the left margin, modify the left margin to be the leftmost column +cursor column and the [left margin](#TODO). If the cursor begins to the left of the left margin, modify the left margin to be the leftmost column for the duration of the sequence. The leftmost column the cursor can be on is the left margin. @@ -142,5 +141,22 @@ printf "X" |A_________| |B_________| |_________Xc -``g ``` + +### CUB V-6: Reverse Wrap Outside of Margins + +```bash +printf "\033[1;1H" +printf "\033[0J" +printf "\033[?45h" +printf "\033[3r" +printf "\b" +printf "X" +``` + +``` +|__________| +|__________| +|Xc________| +``` +