switch to new screen, bugs!

This commit is contained in:
Mitchell Hashimoto
2022-05-21 16:42:55 -07:00
parent da1e42de1a
commit c749371bae
2 changed files with 29 additions and 49 deletions

View File

@ -265,16 +265,21 @@ pub fn updateCells(self: *Grid, term: Terminal) !void {
// we have plus a full width. This is very likely too much but its // we have plus a full width. This is very likely too much but its
// the probably close enough while guaranteeing no more allocations. // the probably close enough while guaranteeing no more allocations.
self.cells.clearRetainingCapacity(); self.cells.clearRetainingCapacity();
if (term.screen.items.len == 0) return;
try self.cells.ensureTotalCapacity( try self.cells.ensureTotalCapacity(
self.alloc, self.alloc,
(term.screen.items.len * term.cols * 2) + 1, // * 2 for background modes and cursor
// * 2 for background modes and cursor
// + 1 for cursor // + 1 for cursor
(term.screen.rows * term.screen.cols * 2) + 1,
); );
// Build each cell // Build each cell
for (term.screen.items) |line, y| { var rowIter = term.screen.rowIterator();
for (line.items) |cell, x| { var y: usize = 0;
while (rowIter.next()) |line| {
defer y += 1;
for (line) |cell, x| {
// If the cell has a background, we always draw it. // If the cell has a background, we always draw it.
if (cell.bg) |rgb| { if (cell.bg) |rgb| {
self.cells.appendAssumeCapacity(.{ self.cells.appendAssumeCapacity(.{

View File

@ -91,17 +91,18 @@ pub fn resize(self: *Terminal, alloc: Allocator, cols: usize, rows: usize) !void
// If we're making the screen smaller, dealloc the unused items. // If we're making the screen smaller, dealloc the unused items.
// TODO: we probably want to wrap in the future. // TODO: we probably want to wrap in the future.
if (rows < self.rows and self.screen.items.len > rows) { // TODO: new screen
for (self.screen.items[rows..self.screen.items.len]) |*line| // if (rows < self.rows and self.screen.items.len > rows) {
line.deinit(alloc); // for (self.screen.items[rows..self.screen.items.len]) |*line|
self.screen.shrinkRetainingCapacity(rows); // line.deinit(alloc);
} // self.screen.shrinkRetainingCapacity(rows);
if (cols < self.cols) { // }
for (self.screen.items) |*line| { // if (cols < self.cols) {
if (line.items.len < cols) continue; // for (self.screen.items) |*line| {
line.shrinkRetainingCapacity(cols); // if (line.items.len < cols) continue;
} // line.shrinkRetainingCapacity(cols);
} // }
// }
// Set our size // Set our size
self.cols = cols; self.cols = cols;
@ -291,24 +292,18 @@ pub fn eraseLine(
/// ///
/// TODO: test /// TODO: test
pub fn deleteChars(self: *Terminal, count: usize) !void { pub fn deleteChars(self: *Terminal, count: usize) !void {
var line = &self.screen.items[self.cursor.y]; const line = self.screen.getRow(self.cursor.y);
// Our last index is at most the end of the number of chars we have // Our last index is at most the end of the number of chars we have
// in the current line. // in the current line.
const end = @minimum(line.items.len, self.cols - count); const end = self.cols - count;
// Do nothing if we have no values.
if (self.cursor.x >= line.items.len) return;
// Shift // Shift
var i: usize = self.cursor.x; var i: usize = self.cursor.x;
while (i < end) : (i += 1) { while (i < end) : (i += 1) {
const j = i + count; const j = i + count;
if (j < line.items.len) { line[i] = line[j];
line.items[i] = line.items[j]; line[j].char = 0;
} else {
line.items[i].char = 0;
}
} }
} }
@ -562,30 +557,10 @@ pub fn scrollDown(self: *Terminal, alloc: Allocator) !void {
const tracy = trace(@src()); const tracy = trace(@src());
defer tracy.end(); defer tracy.end();
// TODO: this is horribly expensive. we need to optimize the screen repr _ = alloc;
self.screen.scroll(1);
// We need space for one more row if we aren't at the max. const top = self.screen.getRow(0);
if (self.screen.capacity < self.rows) { for (top) |*cell| cell.char = 0;
try self.screen.ensureTotalCapacity(alloc, self.screen.items.len + 1);
}
// Add one more item if we aren't at the max
if (self.screen.items.len < self.rows) {
self.screen.items.len += 1;
} else {
// We have the max, we need to deinitialize the last row because
// we're going to overwrite it.
self.screen.items[self.screen.items.len - 1].deinit(alloc);
}
// Shift everything down
var i: usize = self.screen.items.len - 1;
while (i > 0) : (i -= 1) {
self.screen.items[i] = self.screen.items[i - 1];
}
// Clear this row
self.screen.items[0] = .{};
} }
/// Set Top and Bottom Margins If bottom is not specified, 0 or bigger than /// Set Top and Bottom Margins If bottom is not specified, 0 or bigger than