terminal: don't modify pen state when erasing eraseDisplay

Erasing the display should be done with only the background attribute of
the current pen. This is the current behavior but is done by altering
the current pen state. The pen state should remain after erasure. Use a
new pen variable to erase the display to enable retaining the pen state.
Add a test condition.
This commit is contained in:
Tim Culverhouse
2023-09-25 12:25:45 -05:00
parent b592b069c8
commit 3264e70701

View File

@ -214,11 +214,6 @@ pub fn alternateScreen(
if (options.clear_on_enter) { if (options.clear_on_enter) {
self.eraseDisplay(alloc, .complete); self.eraseDisplay(alloc, .complete);
// HACK: eraseDisplay needs to work properly here, but alters the state
// of our pen in doing so. Either way, we need to set pen state before
// erasing, use the semantics of erasing, and end up with the pen state
// where it was
self.screen.cursor = old.cursor;
} }
} }
@ -1020,7 +1015,7 @@ pub fn eraseDisplay(
defer tracy.end(); defer tracy.end();
// Erasing clears all attributes / colors _except_ the background // Erasing clears all attributes / colors _except_ the background
self.screen.cursor.pen = if (!self.screen.cursor.pen.attrs.has_bg) .{} else .{ const pen: Screen.Cell = if (!self.screen.cursor.pen.attrs.has_bg) .{} else .{
.bg = self.screen.cursor.pen.bg, .bg = self.screen.cursor.pen.bg,
.attrs = .{ .has_bg = true }, .attrs = .{ .has_bg = true },
}; };
@ -1031,7 +1026,7 @@ pub fn eraseDisplay(
while (it.next()) |row| { while (it.next()) |row| {
row.setWrapped(false); row.setWrapped(false);
row.setDirty(true); row.setDirty(true);
row.clear(self.screen.cursor.pen); row.clear(pen);
} }
// Unsets pending wrap state // Unsets pending wrap state
@ -1050,7 +1045,7 @@ pub fn eraseDisplay(
for (self.screen.cursor.x..self.cols) |x| { for (self.screen.cursor.x..self.cols) |x| {
if (row.header().flags.grapheme) row.clearGraphemes(x); if (row.header().flags.grapheme) row.clearGraphemes(x);
const cell = row.getCellPtr(x); const cell = row.getCellPtr(x);
cell.* = self.screen.cursor.pen; cell.* = pen;
cell.char = 0; cell.char = 0;
} }
} }
@ -1063,7 +1058,7 @@ pub fn eraseDisplay(
for (0..self.cols) |x| { for (0..self.cols) |x| {
if (row.header().flags.grapheme) row.clearGraphemes(x); if (row.header().flags.grapheme) row.clearGraphemes(x);
const cell = row.getCellPtr(x); const cell = row.getCellPtr(x);
cell.* = self.screen.cursor.pen; cell.* = pen;
cell.char = 0; cell.char = 0;
} }
} }
@ -1077,7 +1072,7 @@ pub fn eraseDisplay(
var x: usize = 0; var x: usize = 0;
while (x <= self.screen.cursor.x) : (x += 1) { while (x <= self.screen.cursor.x) : (x += 1) {
const cell = self.screen.getCellPtr(.active, self.screen.cursor.y, x); const cell = self.screen.getCellPtr(.active, self.screen.cursor.y, x);
cell.* = self.screen.cursor.pen; cell.* = pen;
cell.char = 0; cell.char = 0;
} }
@ -1087,7 +1082,7 @@ pub fn eraseDisplay(
x = 0; x = 0;
while (x < self.cols) : (x += 1) { while (x < self.cols) : (x += 1) {
const cell = self.screen.getCellPtr(.active, y, x); const cell = self.screen.getCellPtr(.active, y, x);
cell.* = self.screen.cursor.pen; cell.* = pen;
cell.char = 0; cell.char = 0;
} }
} }
@ -1135,6 +1130,9 @@ test "Terminal: eraseDisplay above" {
try testing.expect(!cell.attrs.bold); try testing.expect(!cell.attrs.bold);
try testing.expect(cell.attrs.has_bg); try testing.expect(cell.attrs.has_bg);
// Check that our pen hasn't changed
try testing.expect(t.screen.cursor.pen.attrs.bold);
// check that another cell got the correct bg // check that another cell got the correct bg
cell = t.screen.getCell(.active, 0, 1); cell = t.screen.getCell(.active, 0, 1);
try testing.expect(cell.bg.eql(pink)); try testing.expect(cell.bg.eql(pink));