From a082e4c1a2067a95189f7eefa6c2366e0fcccdfa Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 22 Aug 2023 16:43:21 -0700 Subject: [PATCH] terminal: delete lines (CSI M) should clamp count to remaining lines --- src/terminal/Terminal.zig | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index 67fa95889..7d37eec24 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -1407,7 +1407,7 @@ pub fn deleteLines(self: *Terminal, count: usize) !void { self.screen.scrollRegionUp( .{ .active = self.screen.cursor.y }, .{ .active = self.scrolling_region.bottom }, - count, + @min(count, self.scrolling_region.bottom - self.screen.cursor.y), ); } @@ -2055,6 +2055,42 @@ test "Terminal: deleteLines with scroll region" { } } +test "Terminal: deleteLines with scroll region, large count" { + const alloc = testing.allocator; + var t = try init(alloc, 80, 80); + defer t.deinit(alloc); + + // Initial value + 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.setScrollingRegion(1, 3); + t.setCursorPos(1, 1); + try t.deleteLines(5); + + try t.print('E'); + t.carriageReturn(); + try t.linefeed(); + + // We should be + // try testing.expectEqual(@as(usize, 0), t.screen.cursor.x); + // try testing.expectEqual(@as(usize, 2), t.screen.cursor.y); + + { + var str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("E\n\n\nD", str); + } +} + test "Terminal: insertLines" { const alloc = testing.allocator; var t = try init(alloc, 2, 5);