Skip to content

Commit

Permalink
Merge pull request #639 from mitchellh/il
Browse files Browse the repository at this point in the history
xterm audit: insert line (IL), delete line (DL)
  • Loading branch information
mitchellh authored Oct 9, 2023
2 parents 710a2df + 1176b65 commit 33ce2c2
Show file tree
Hide file tree
Showing 4 changed files with 506 additions and 14 deletions.
21 changes: 21 additions & 0 deletions src/terminal/Screen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,27 @@ pub const Row = struct {
}
}

/// Copy a single cell from column x in src to column x in this row.
pub fn copyCell(self: Row, src: Row, x: usize) !void {
const dst_cell = self.getCellPtr(x);
const src_cell = src.getCellPtr(x);

// If our destination has graphemes, we have to clear them.
if (dst_cell.attrs.grapheme) self.clearGraphemes(x);
dst_cell.* = src_cell.*;

// If the source doesn't have any graphemes, then we can just copy.
if (!src_cell.attrs.grapheme) return;

// Source cell has graphemes. Copy them.
const src_key = src.getId() + x + 1;
const src_data = src.screen.graphemes.get(src_key) orelse return;
const dst_key = self.getId() + x + 1;
const dst_gop = try self.screen.graphemes.getOrPut(self.screen.alloc, dst_key);
dst_gop.value_ptr.* = try src_data.copy(self.screen.alloc);
self.storage[0].header.flags.grapheme = true;
}

/// Copy the row src into this row. The row can be from another screen.
pub fn copyRow(self: Row, src: Row) !void {
// If we have graphemes, clear first to unset them.
Expand Down
267 changes: 253 additions & 14 deletions src/terminal/Terminal.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1564,8 +1564,14 @@ pub fn insertLines(self: *Terminal, count: usize) !void {
// Rare, but happens
if (count == 0) return;

// If the cursor is outside the scroll region we do nothing.
if (self.screen.cursor.y < self.scrolling_region.top or
self.screen.cursor.y > self.scrolling_region.bottom or
self.screen.cursor.x < self.scrolling_region.left or
self.screen.cursor.x > self.scrolling_region.right) return;

// Move the cursor to the left margin
self.screen.cursor.x = 0;
self.screen.cursor.x = self.scrolling_region.left;
self.screen.cursor.pending_wrap = false;

// Remaining rows from our cursor
Expand All @@ -1582,14 +1588,21 @@ pub fn insertLines(self: *Terminal, count: usize) !void {

// Ensure we have the lines populated to the end
while (y > top) : (y -= 1) {
try self.screen.copyRow(.{ .active = y }, .{ .active = y - adjusted_count });
const src = self.screen.getRow(.{ .active = y - adjusted_count });
const dst = self.screen.getRow(.{ .active = y });
for (self.scrolling_region.left..self.scrolling_region.right + 1) |x| {
try dst.copyCell(src, x);
}
}

// Insert count blank lines
y = self.screen.cursor.y;
while (y < self.screen.cursor.y + adjusted_count) : (y += 1) {
const row = self.screen.getRow(.{ .active = y });
row.clear(self.screen.cursor.pen);
row.fillSlice(.{
.bg = self.screen.cursor.pen.bg,
.attrs = .{ .has_bg = self.screen.cursor.pen.attrs.has_bg },
}, self.scrolling_region.left, self.scrolling_region.right + 1);
}
}

Expand All @@ -1613,23 +1626,58 @@ pub fn deleteLines(self: *Terminal, count: usize) !void {
const tracy = trace(@src());
defer tracy.end();

// If our cursor is outside of the scroll region, do nothing.
// If the cursor is outside the scroll region we do nothing.
if (self.screen.cursor.y < self.scrolling_region.top or
self.screen.cursor.y > self.scrolling_region.bottom)
self.screen.cursor.y > self.scrolling_region.bottom or
self.screen.cursor.x < self.scrolling_region.left or
self.screen.cursor.x > self.scrolling_region.right) return;

// Move the cursor to the left margin
self.screen.cursor.x = self.scrolling_region.left;
self.screen.cursor.pending_wrap = false;

// If this is a full line margin then we can do a faster scroll.
if (self.scrolling_region.left == 0 and
self.scrolling_region.right == self.cols - 1)
{
self.screen.scrollRegionUp(
.{ .active = self.screen.cursor.y },
.{ .active = self.scrolling_region.bottom },
@min(count, self.scrolling_region.bottom - self.screen.cursor.y),
);
return;
}

// Move the cursor to the left margin
self.screen.cursor.x = 0;
self.screen.cursor.pending_wrap = false;
// Left/right margin is set, we need to do a slower scroll.
// Remaining rows from our cursor in the region, 1-indexed.
const rem = self.scrolling_region.bottom - self.screen.cursor.y + 1;

// Perform the scroll
self.screen.scrollRegionUp(
.{ .active = self.screen.cursor.y },
.{ .active = self.scrolling_region.bottom },
@min(count, self.scrolling_region.bottom - self.screen.cursor.y),
);
// If our count is greater than the remaining amount, we can just
// clear the region using insertLines.
if (count >= rem) {
try self.insertLines(count);
return;
}

// The amount of lines we need to scroll up.
const scroll_amount = rem - count;
const scroll_top = self.scrolling_region.bottom - scroll_amount;
for (self.screen.cursor.y..scroll_top + 1) |y| {
const src = self.screen.getRow(.{ .active = y + count });
const dst = self.screen.getRow(.{ .active = y });
for (self.scrolling_region.left..self.scrolling_region.right + 1) |x| {
try dst.copyCell(src, x);
}
}

// Insert blank lines
for (scroll_top + 1..self.scrolling_region.bottom + 1) |y| {
const row = self.screen.getRow(.{ .active = y });
row.fillSlice(.{
.bg = self.screen.cursor.pen.bg,
.attrs = .{ .has_bg = self.screen.cursor.pen.attrs.has_bg },
}, self.scrolling_region.left, self.scrolling_region.right + 1);
}
}

/// Scroll the text down by one row.
Expand Down Expand Up @@ -2648,6 +2696,171 @@ test "Terminal: deleteLines resets wrap" {
}
}

test "Terminal: deleteLines simple" {
const alloc = testing.allocator;
var t = try init(alloc, 5, 5);
defer t.deinit(alloc);

try t.printString("ABC");
t.carriageReturn();
try t.linefeed();
try t.printString("DEF");
t.carriageReturn();
try t.linefeed();
try t.printString("GHI");
t.setCursorPos(2, 2);
try t.deleteLines(1);

{
var str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("ABC\nGHI", str);
}
}

test "Terminal: deleteLines left/right scroll region" {
const alloc = testing.allocator;
var t = try init(alloc, 10, 10);
defer t.deinit(alloc);

try t.printString("ABC123");
t.carriageReturn();
try t.linefeed();
try t.printString("DEF456");
t.carriageReturn();
try t.linefeed();
try t.printString("GHI789");
t.scrolling_region.left = 1;
t.scrolling_region.right = 3;
t.setCursorPos(2, 2);
try t.deleteLines(1);

{
var str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("ABC123\nDHI756\nG 89", str);
}
}

test "Terminal: deleteLines left/right scroll region high count" {
const alloc = testing.allocator;
var t = try init(alloc, 10, 10);
defer t.deinit(alloc);

try t.printString("ABC123");
t.carriageReturn();
try t.linefeed();
try t.printString("DEF456");
t.carriageReturn();
try t.linefeed();
try t.printString("GHI789");
t.scrolling_region.left = 1;
t.scrolling_region.right = 3;
t.setCursorPos(2, 2);
try t.deleteLines(100);

{
var str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("ABC123\nD 56\nG 89", str);
}
}

test "Terminal: insertLines simple" {
const alloc = testing.allocator;
var t = try init(alloc, 5, 5);
defer t.deinit(alloc);

try t.printString("ABC");
t.carriageReturn();
try t.linefeed();
try t.printString("DEF");
t.carriageReturn();
try t.linefeed();
try t.printString("GHI");
t.setCursorPos(2, 2);
try t.insertLines(1);

{
var str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("ABC\n\nDEF\nGHI", str);
}
}

test "Terminal: insertLines outside of scroll region" {
const alloc = testing.allocator;
var t = try init(alloc, 5, 5);
defer t.deinit(alloc);

try t.printString("ABC");
t.carriageReturn();
try t.linefeed();
try t.printString("DEF");
t.carriageReturn();
try t.linefeed();
try t.printString("GHI");
t.setScrollingRegion(3, 4);
t.setCursorPos(2, 2);
try t.insertLines(1);

{
var str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("ABC\nDEF\nGHI", str);
}
}

test "Terminal: insertLines top/bottom scroll region" {
const alloc = testing.allocator;
var t = try init(alloc, 5, 5);
defer t.deinit(alloc);

try t.printString("ABC");
t.carriageReturn();
try t.linefeed();
try t.printString("DEF");
t.carriageReturn();
try t.linefeed();
try t.printString("GHI");
t.carriageReturn();
try t.linefeed();
try t.printString("123");
t.setScrollingRegion(1, 3);
t.setCursorPos(2, 2);
try t.insertLines(1);

{
var str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("ABC\n\nDEF\n123", str);
}
}

test "Terminal: insertLines left/right scroll region" {
const alloc = testing.allocator;
var t = try init(alloc, 10, 10);
defer t.deinit(alloc);

try t.printString("ABC123");
t.carriageReturn();
try t.linefeed();
try t.printString("DEF456");
t.carriageReturn();
try t.linefeed();
try t.printString("GHI789");
t.scrolling_region.left = 1;
t.scrolling_region.right = 3;
t.setCursorPos(2, 2);
try t.insertLines(1);

{
var str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("ABC123\nD 56\nGEF489\n HI7", str);
}
}

test "Terminal: insertLines" {
const alloc = testing.allocator;
var t = try init(alloc, 2, 5);
Expand Down Expand Up @@ -2964,6 +3177,32 @@ test "Terminal: index bottom of primary screen" {
}
}

test "Terminal: index bottom of primary screen background sgr" {
const alloc = testing.allocator;
var t = try init(alloc, 5, 5);
defer t.deinit(alloc);

const pen: Screen.Cell = .{
.bg = .{ .r = 0xFF, .g = 0x00, .b = 0x00 },
.attrs = .{ .has_bg = true },
};

t.setCursorPos(5, 1);
try t.print('A');
t.screen.cursor.pen = pen;
try t.index();

{
var str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("\n\n\nA", str);
for (0..5) |x| {
const cell = t.screen.getCell(.active, 4, x);
try testing.expectEqual(pen, cell);
}
}
}

test "Terminal: index inside scroll region" {
const alloc = testing.allocator;
var t = try init(alloc, 5, 5);
Expand Down
Loading

0 comments on commit 33ce2c2

Please sign in to comment.