From 28f637945395e8575399eda0f9d58dab51c20330 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 7 Oct 2023 09:17:00 -0700 Subject: [PATCH] terminal: CUP respects left/right scroll region --- src/terminal/Terminal.zig | 99 +++++++++++++++++++++++++++++++++++-- website/app/vt/cup/page.mdx | 14 ++++++ 2 files changed, 109 insertions(+), 4 deletions(-) diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index 2fc0ee95c..6f6c69d05 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -975,9 +975,9 @@ pub fn setCursorPos(self: *Terminal, row_req: usize, col_req: usize) void { x_max: usize, y_max: usize, } = if (self.modes.get(.origin)) .{ - .x_offset = 0, // TODO: left/right margins + .x_offset = self.scrolling_region.left, .y_offset = self.scrolling_region.top, - .x_max = self.cols, // TODO: left/right margins + .x_max = self.scrolling_region.right + 1, // We need this 1-indexed .y_max = self.scrolling_region.bottom + 1, // We need this 1-indexed } else .{ .x_max = self.cols, @@ -986,7 +986,7 @@ pub fn setCursorPos(self: *Terminal, row_req: usize, col_req: usize) void { const row = if (row_req == 0) 1 else row_req; const col = if (col_req == 0) 1 else col_req; - self.screen.cursor.x = @min(params.x_max, col) -| 1; + self.screen.cursor.x = @min(params.x_max, col + params.x_offset) -| 1; self.screen.cursor.y = @min(params.y_max, row + params.y_offset) -| 1; // log.info("set cursor position: col={} row={}", .{ self.screen.cursor.x, self.screen.cursor.y }); @@ -2422,7 +2422,98 @@ test "Terminal: horizontal tabs with left margin in origin mode" { } } -test "Terminal: setCursorPosition" { +test "Terminal: cursorPos resets wrap" { + const alloc = testing.allocator; + var t = try init(alloc, 5, 5); + defer t.deinit(alloc); + + for ("ABCDE") |c| try t.print(c); + try testing.expect(t.screen.cursor.pending_wrap); + t.setCursorPos(1, 1); + try testing.expect(!t.screen.cursor.pending_wrap); + try t.print('X'); + + { + var str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("XBCDE", str); + } +} + +test "Terminal: cursorPos off the screen" { + const alloc = testing.allocator; + var t = try init(alloc, 5, 5); + defer t.deinit(alloc); + + t.setCursorPos(500, 500); + try t.print('X'); + + { + var str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("\n\n\n\n X", str); + } +} + +test "Terminal: cursorPos relative to origin" { + const alloc = testing.allocator; + var t = try init(alloc, 5, 5); + defer t.deinit(alloc); + + t.scrolling_region.top = 2; + t.scrolling_region.bottom = 3; + t.modes.set(.origin, true); + t.setCursorPos(1, 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: cursorPos relative to origin with left/right" { + const alloc = testing.allocator; + var t = try init(alloc, 5, 5); + defer t.deinit(alloc); + + t.scrolling_region.top = 2; + t.scrolling_region.bottom = 3; + t.scrolling_region.left = 2; + t.scrolling_region.right = 4; + t.modes.set(.origin, true); + t.setCursorPos(1, 1); + try t.print('X'); + + { + var str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("\n\n X", str); + } +} + +test "Terminal: cursorPos limits with full scroll region" { + const alloc = testing.allocator; + var t = try init(alloc, 5, 5); + defer t.deinit(alloc); + + t.scrolling_region.top = 2; + t.scrolling_region.bottom = 3; + t.scrolling_region.left = 2; + t.scrolling_region.right = 4; + t.modes.set(.origin, true); + t.setCursorPos(500, 500); + try t.print('X'); + + { + var str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("\n\n\n X", str); + } +} + +test "Terminal: setCursorPos (original test)" { var t = try init(testing.allocator, 80, 80); defer t.deinit(testing.allocator); diff --git a/website/app/vt/cup/page.mdx b/website/app/vt/cup/page.mdx index 5e93362a4..75e504d8f 100644 --- a/website/app/vt/cup/page.mdx +++ b/website/app/vt/cup/page.mdx @@ -111,3 +111,17 @@ printf "X" |__________| |____X_____| ``` + +### CUP V-6: Pending Wrap is Unset + +```bash +cols=$(tput cols) +printf "\033[${cols}G" # move to last column +printf "A" # set pending wrap state +printf "\033[1;1H" +printf "X" +``` + +``` +|Xc_______X| +```