feat: add basic state keep

This commit is contained in:
pnodet
2024-12-28 21:06:34 +01:00
parent e26edaab96
commit 5051a19c7f
2 changed files with 77 additions and 0 deletions

View File

@ -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.

View File

@ -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];
}
};