diff --git a/src/Window.zig b/src/Window.zig index 4f21ce228..57f5ed997 100644 --- a/src/Window.zig +++ b/src/Window.zig @@ -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(.{ diff --git a/src/terminal/Tabstops.zig b/src/terminal/Tabstops.zig index 63a001247..cbf304bb9 100644 --- a/src/terminal/Tabstops.zig +++ b/src/terminal/Tabstops.zig @@ -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; diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index 1f873efd0..589196ebb 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -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