macos: store pwd with save/restore state

This commit is contained in:
Mitchell Hashimoto
2023-12-23 17:06:34 -08:00
parent 243379c50f
commit a8568306c9
2 changed files with 22 additions and 3 deletions

View File

@ -141,6 +141,10 @@ extension Ghostty {
// MARK: - Codable
enum CodingKeys: String, CodingKey {
case pwd
}
required convenience init(from decoder: Decoder) throws {
// Decoding uses the global Ghostty app
guard let del = NSApplication.shared.delegate,
@ -149,12 +153,16 @@ extension Ghostty {
throw TerminalRestoreError.delegateInvalid
}
self.init(app, nil)
let container = try decoder.container(keyedBy: CodingKeys.self)
var config = SurfaceConfiguration()
config.workingDirectory = try container.decode(String?.self, forKey: .pwd)
self.init(app, config)
}
func encode(to encoder: Encoder) throws {
// We don't currently encode anything, but in the future we will
// want to encode pwd, etc...
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(surface.pwd, forKey: .pwd)
}
}

View File

@ -283,6 +283,17 @@ extension Ghostty {
return ghostty_surface_needs_confirm_quit(surface)
}
/// Returns the pwd of the surface if it has one.
var pwd: String? {
guard let surface = self.surface else { return nil }
let v = String(unsafeUninitializedCapacity: 1024) {
Int(ghostty_surface_pwd(surface, $0.baseAddress, UInt($0.count)))
}
if (v.count == 0) { return nil }
return v
}
// Returns the inspector instance for this surface, or nil if the
// surface has been closed.
var inspector: ghostty_inspector_t? {