From 069e16cb46206d62f93b91dc1e89178e9ac13f86 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 27 Oct 2023 09:42:39 -0700 Subject: [PATCH] terminal: restore cursor should clamp x/y --- src/terminal/Terminal.zig | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index a932c33c2..1bd5f2cf3 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -443,8 +443,8 @@ pub fn restoreCursor(self: *Terminal) void { self.screen.cursor.pen = saved.pen; self.screen.charset = saved.charset; self.modes.set(.origin, saved.origin); - self.screen.cursor.x = saved.x; - self.screen.cursor.y = saved.y; + self.screen.cursor.x = @min(saved.x, self.cols - 1); + self.screen.cursor.y = @min(saved.y, self.rows - 1); self.screen.cursor.pending_wrap = saved.pending_wrap; } @@ -5065,6 +5065,24 @@ test "Terminal: saveCursor origin mode" { } } +test "Terminal: saveCursor resize" { + const alloc = testing.allocator; + var t = try init(alloc, 10, 5); + defer t.deinit(alloc); + + t.setCursorPos(1, 10); + t.saveCursor(); + try t.resize(alloc, 5, 5); + t.restoreCursor(); + try t.print('X'); + + { + var str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings(" X", str); + } +} + test "Terminal: setProtectedMode" { const alloc = testing.allocator; var t = try init(alloc, 3, 3);