From 5051a19c7f9fcb402335ae0f3a6d003e167c17a2 Mon Sep 17 00:00:00 2001 From: pnodet Date: Sat, 28 Dec 2024 21:06:34 +0100 Subject: [PATCH] feat: add basic state keep --- src/apprt/glfw.zig | 24 +++++++++++++++++ src/terminal/closedtabs.zig | 53 +++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/terminal/closedtabs.zig diff --git a/src/apprt/glfw.zig b/src/apprt/glfw.zig index cb2189339..e73cabbf8 100644 --- a/src/apprt/glfw.zig +++ b/src/apprt/glfw.zig @@ -23,6 +23,8 @@ const CoreSurface = @import("../Surface.zig"); const configpkg = @import("../config.zig"); const Config = @import("../config.zig").Config; +const LastClosedTabs = @import("../terminal/closedtabs.zig").LastClosedTabs; + // Get native API access on certain platforms so we can do more customization. const glfwNative = glfw.Native(.{ .cocoa = builtin.target.isDarwin(), @@ -38,6 +40,9 @@ pub const App = struct { /// Mac-specific state. darwin: if (Darwin.enabled) Darwin else void, + /// Store information about the last closed tabs + last_closed_tabs: LastClosedTabs = .{}, + pub const Options = struct {}; pub fn init(core_app: *CoreApp, _: Options) !App { @@ -105,6 +110,7 @@ pub const App = struct { } pub fn terminate(self: *App) void { + self.last_closed_tabs.deinit(self.app.alloc); self.config.deinit(); glfw.terminate(); } @@ -604,6 +610,24 @@ pub const Surface = struct { } pub fn deinit(self: *Surface) void { + // Save the closing tab information + const title = if (self.title_text) |t| + self.core_surface.alloc.dupe(u8, t) catch null + else + null; + errdefer if (title) |t| self.core_surface.alloc.free(t); + + const cwd = self.core_surface.alloc.dupe(u8, "~") catch null; + errdefer if (cwd) |c| self.core_surface.alloc.free(c); + + self.app.last_closed_tabs.push(.{ + .title = title, + .cwd = cwd, + }, self.core_surface.alloc); + + log.debug("all last closed tab: {?}", .{self.app.last_closed_tabs.this}); + log.debug("last closed tab: {?}", .{self.app.last_closed_tabs.getLast()}); + if (self.title_text) |t| self.core_surface.alloc.free(t); // Remove ourselves from the list of known surfaces in the app. diff --git a/src/terminal/closedtabs.zig b/src/terminal/closedtabs.zig new file mode 100644 index 000000000..6e0b0205b --- /dev/null +++ b/src/terminal/closedtabs.zig @@ -0,0 +1,53 @@ +const std = @import("std"); +const Allocator = std.mem.Allocator; + +const max_closed_tabs = 10; + +pub const LastClosedTab = struct { + title: ?[]const u8, + cwd: ?[]const u8, + + pub fn deinit(self: *LastClosedTab, alloc: Allocator) void { + if (self.title) |t| alloc.free(t); + if (self.cwd) |c| alloc.free(c); + } +}; + +pub const LastClosedTabs = struct { + this: std.BoundedArray(LastClosedTab, max_closed_tabs) = std.BoundedArray(LastClosedTab, max_closed_tabs).init(0) catch unreachable, + + pub fn push(self: *LastClosedTabs, tab: LastClosedTab, alloc: Allocator) void { + if (self.this.len == max_closed_tabs) { + // Remove oldest tab and free its memory + self.this.buffer[0].deinit(alloc); + // Shift all elements left + for (0..self.this.len - 1) |i| { + self.this.buffer[i] = self.this.buffer[i + 1]; + } + // Add new tab at the end + self.this.buffer[self.this.len - 1] = tab; + } else { + self.this.append(tab) catch unreachable; + } + } + + pub fn deinit(self: *LastClosedTabs, alloc: Allocator) void { + for (0..self.this.len) |i| { + self.this.buffer[i].deinit(alloc); + } + self.this.len = 0; + } + + pub fn get(self: *LastClosedTabs, index: usize) ?*LastClosedTab { + return self.this.get(index); + } + + pub fn len(self: *LastClosedTabs) usize { + return self.this.len; + } + + pub fn getLast(self: *LastClosedTabs) ?*LastClosedTab { + if (self.this.len == 0) return null; + return &self.this.buffer[self.this.len - 1]; + } +};