terminal: implement resizing in a basic way

This commit is contained in:
Mitchell Hashimoto
2022-05-16 11:02:33 -07:00
parent cb267f4302
commit 751f97220e
3 changed files with 34 additions and 3 deletions

View File

@ -282,7 +282,8 @@ fn sizeCallback(window: glfw.Window, width: i32, height: i32) void {
}) catch |err| log.err("error updating grid screen size err={}", .{err});
// Update the size of our terminal state
win.terminal.resize(win.grid.size.columns, win.grid.size.rows);
win.terminal.resize(win.alloc, win.grid.size.columns, win.grid.size.rows) catch |err|
log.err("error updating terminal size: {}", .{err});
// Update the size of our pty
win.pty.setSize(.{

View File

@ -100,6 +100,7 @@ pub fn get(self: Tabstops, col: usize) bool {
}
/// Resize this to support up to cols columns.
// TODO: needs interval to set new tabstops
pub fn resize(self: *Tabstops, alloc: Allocator, cols: usize) !void {
// Set our new value
self.cols = cols;

View File

@ -103,10 +103,39 @@ pub fn deinit(self: *Terminal, alloc: Allocator) void {
}
/// Resize the underlying terminal.
pub fn resize(self: *Terminal, cols: usize, rows: usize) void {
// TODO: actually doing anything for this
pub fn resize(self: *Terminal, alloc: Allocator, cols: usize, rows: usize) !void {
// TODO: test, wrapping, etc.
// Resize our tabstops
// TODO: use resize, but it doesn't set new tabstops
if (self.cols != cols) {
self.tabstops.deinit(alloc);
self.tabstops = try Tabstops.init(alloc, cols, 8);
}
// If we're making the screen smaller, dealloc the unused items.
// TODO: we probably want to wrap in the future.
if (rows < self.rows and self.screen.items.len > rows) {
for (self.screen.items[rows..self.screen.items.len]) |*line|
line.deinit(alloc);
self.screen.shrinkRetainingCapacity(rows);
}
if (cols < self.cols) {
for (self.screen.items) |*line| {
if (line.items.len < cols) continue;
line.shrinkRetainingCapacity(cols);
}
}
// Set our size
self.cols = cols;
self.rows = rows;
// Reset the scrolling region
self.scrolling_region = .{
.top = 0,
.bottom = rows - 1,
};
}
/// Return the current string value of the terminal. Newlines are