diff --git a/src/Surface.zig b/src/Surface.zig index 4283a5d54..12adde808 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -731,10 +731,14 @@ pub fn close(self: *Surface) void { const title = self.rt_surface.getTitle(); const title_copy = if (title) |t| self.alloc.dupe(u8, t) catch null else null; + // Save terminal contents including scrollback buffer + const contents = self.io.terminal.screen.dumpStringAlloc(self.alloc, .{ .screen = .{} }) catch null; + // Save to last closed tabs self.app.last_closed_tabs.push(.{ .title = title_copy, .cwd = cwd_copy, + .contents = contents, }, self.alloc); log.debug("closing tab - pwd: {s}, title: {s}", .{ diff --git a/src/apprt/glfw.zig b/src/apprt/glfw.zig index ccdd0f313..94d6d8720 100644 --- a/src/apprt/glfw.zig +++ b/src/apprt/glfw.zig @@ -23,6 +23,7 @@ const CoreSurface = @import("../Surface.zig"); const configpkg = @import("../config.zig"); const Config = @import("../config.zig").Config; const LastClosedTab = @import("../terminal/closedtabs.zig").LastClosedTab; +const sgr = @import("../terminal/sgr.zig"); // Get native API access on certain platforms so we can do more customization. const glfwNative = glfw.Native(.{ @@ -382,6 +383,11 @@ pub const App = struct { try window.core_surface.rt_surface.setTitle(title_z); } + // Restore terminal contents if available + if (last_tab.contents) |contents| { + try window.core_surface.io.terminal.printString(contents); + } + log.debug("Reopening last tab - pwd: {s}, title: {s}", .{ last_tab.cwd orelse "(null)", last_tab.title orelse "(null)", diff --git a/src/terminal/closedtabs.zig b/src/terminal/closedtabs.zig index 6e0b0205b..06b9b4063 100644 --- a/src/terminal/closedtabs.zig +++ b/src/terminal/closedtabs.zig @@ -6,10 +6,12 @@ const max_closed_tabs = 10; pub const LastClosedTab = struct { title: ?[]const u8, cwd: ?[]const u8, + contents: ?[]const u8, pub fn deinit(self: *LastClosedTab, alloc: Allocator) void { if (self.title) |t| alloc.free(t); if (self.cwd) |c| alloc.free(c); + if (self.contents) |c| alloc.free(c); } };