mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-08-02 14:57:31 +03:00
chore: cleanup and improve mem handling
This commit is contained in:
@ -727,27 +727,25 @@ pub fn close(self: *Surface) void {
|
|||||||
// Save tab data before closing
|
// Save tab data before closing
|
||||||
const cwd = self.io.terminal.getPwd();
|
const cwd = self.io.terminal.getPwd();
|
||||||
const cwd_copy = if (cwd) |c| self.alloc.dupe(u8, c) catch null else null;
|
const cwd_copy = if (cwd) |c| self.alloc.dupe(u8, c) catch null else null;
|
||||||
|
errdefer if (cwd_copy) |c| self.alloc.free(c);
|
||||||
|
|
||||||
const title = self.rt_surface.getTitle();
|
const title = self.rt_surface.getTitle();
|
||||||
const title_copy = if (title) |t| self.alloc.dupe(u8, t) catch null else null;
|
const title_copy = if (title) |t| self.alloc.dupe(u8, t) catch null else null;
|
||||||
|
errdefer if (title_copy) |t| self.alloc.free(t);
|
||||||
|
|
||||||
// Save terminal contents including scrollback buffer
|
// Save terminal contents including scrollback buffer
|
||||||
const contents = self.io.terminal.screen.dumpStringAlloc(self.alloc, .{ .screen = .{} }) catch null;
|
const contents = self.io.terminal.screen.dumpStringAlloc(self.alloc, .{ .screen = .{} }) catch null;
|
||||||
|
errdefer if (contents) |c| self.alloc.free(c);
|
||||||
|
|
||||||
// Save to last closed tabs
|
// Save to last closed tabs
|
||||||
self.app.last_closed_tabs.push(.{
|
self.app.last_closed_tabs.push(.{
|
||||||
.title = title_copy,
|
.title = title_copy,
|
||||||
.cwd = cwd_copy,
|
.cwd = cwd_copy,
|
||||||
.contents = contents,
|
.contents = contents,
|
||||||
}, self.alloc);
|
|
||||||
|
|
||||||
log.debug("closing tab - pwd: {s}, title: {s}", .{
|
|
||||||
cwd_copy orelse "(null)",
|
|
||||||
title_copy orelse "(null)",
|
|
||||||
});
|
});
|
||||||
|
|
||||||
log.debug("close from surface ptr={X}", .{@intFromPtr(self)});
|
// Close the surface
|
||||||
self.rt_surface.close(self.needsConfirmQuit());
|
self.rt_surface.close(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Forces the surface to render. This is useful for when the surface
|
/// Forces the surface to render. This is useful for when the surface
|
||||||
|
@ -363,27 +363,27 @@ pub const App = struct {
|
|||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get the last closed tab from the app-level storage
|
const last_tab_opt = parent.app.last_closed_tabs.pop() orelse {
|
||||||
const last_tab: LastClosedTab = parent.app.last_closed_tabs.pop() orelse {
|
|
||||||
log.warn("No last closed tab found", .{});
|
log.warn("No last closed tab found", .{});
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create a new tab
|
// Make a mutable copy
|
||||||
|
var last_tab = last_tab_opt;
|
||||||
|
defer last_tab.deinit(parent.app.alloc);
|
||||||
|
|
||||||
const window = try self.newSurface(parent);
|
const window = try self.newSurface(parent);
|
||||||
|
|
||||||
// Set the working directory and title if available
|
|
||||||
if (last_tab.cwd) |cwd| {
|
if (last_tab.cwd) |cwd| {
|
||||||
try window.core_surface.io.terminal.setPwd(cwd);
|
try window.core_surface.io.terminal.setPwd(cwd);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (last_tab.title) |title| {
|
if (last_tab.title) |title| {
|
||||||
// Ensure we have a null-terminated string for the title
|
|
||||||
const title_z = try window.core_surface.alloc.dupeZ(u8, title);
|
const title_z = try window.core_surface.alloc.dupeZ(u8, title);
|
||||||
errdefer window.core_surface.alloc.free(title_z);
|
defer window.core_surface.alloc.free(title_z);
|
||||||
try window.core_surface.rt_surface.setTitle(title_z);
|
try window.core_surface.rt_surface.setTitle(title_z);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restore terminal contents if available
|
|
||||||
if (last_tab.contents) |contents| {
|
if (last_tab.contents) |contents| {
|
||||||
try window.core_surface.io.terminal.printString(contents);
|
try window.core_surface.io.terminal.printString(contents);
|
||||||
}
|
}
|
||||||
@ -656,6 +656,9 @@ pub const Surface = struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Surface) void {
|
pub fn deinit(self: *Surface) void {
|
||||||
|
// Free the title text if it exists
|
||||||
|
if (self.title_text) |t| self.core_surface.alloc.free(t);
|
||||||
|
|
||||||
// Remove ourselves from the list of known surfaces in the app.
|
// Remove ourselves from the list of known surfaces in the app.
|
||||||
self.app.app.deleteSurface(self);
|
self.app.app.deleteSurface(self);
|
||||||
|
|
||||||
|
@ -18,16 +18,14 @@ pub const LastClosedTab = struct {
|
|||||||
pub const LastClosedTabs = struct {
|
pub const LastClosedTabs = struct {
|
||||||
this: std.BoundedArray(LastClosedTab, max_closed_tabs) = std.BoundedArray(LastClosedTab, max_closed_tabs).init(0) catch unreachable,
|
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 {
|
pub fn push(self: *LastClosedTabs, tab: LastClosedTab) void {
|
||||||
if (self.this.len == max_closed_tabs) {
|
if (self.this.len == max_closed_tabs) {
|
||||||
// Remove oldest tab and free its memory
|
// Remove oldest tab and free its memory
|
||||||
self.this.buffer[0].deinit(alloc);
|
self.this.buffer[0] = tab;
|
||||||
// Shift all elements left
|
// Shift all elements left
|
||||||
for (0..self.this.len - 1) |i| {
|
for (0..self.this.len - 1) |i| {
|
||||||
self.this.buffer[i] = self.this.buffer[i + 1];
|
self.this.buffer[i] = self.this.buffer[i + 1];
|
||||||
}
|
}
|
||||||
// Add new tab at the end
|
|
||||||
self.this.buffer[self.this.len - 1] = tab;
|
|
||||||
} else {
|
} else {
|
||||||
self.this.append(tab) catch unreachable;
|
self.this.append(tab) catch unreachable;
|
||||||
}
|
}
|
||||||
@ -41,17 +39,13 @@ pub const LastClosedTabs = struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(self: *LastClosedTabs, index: usize) ?*LastClosedTab {
|
pub fn get(self: *LastClosedTabs, index: usize) ?*LastClosedTab {
|
||||||
return self.this.get(index);
|
if (index >= self.this.len) return null;
|
||||||
}
|
return &self.this.buffer[index];
|
||||||
|
|
||||||
pub fn len(self: *LastClosedTabs) usize {
|
|
||||||
return self.this.len;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pop(self: *LastClosedTabs) ?LastClosedTab {
|
pub fn pop(self: *LastClosedTabs) ?LastClosedTab {
|
||||||
if (self.this.len == 0) return null;
|
if (self.this.len == 0) return null;
|
||||||
const last = self.this.buffer[self.this.len - 1];
|
|
||||||
self.this.len -= 1;
|
self.this.len -= 1;
|
||||||
return last;
|
return self.this.buffer[self.this.len];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user