mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-17 09:16:11 +03:00
Added macOS file open handler
This commit is contained in:
@ -350,6 +350,7 @@ typedef struct {
|
|||||||
double scale_factor;
|
double scale_factor;
|
||||||
uint16_t font_size;
|
uint16_t font_size;
|
||||||
const char *working_directory;
|
const char *working_directory;
|
||||||
|
const char *command;
|
||||||
} ghostty_surface_config_s;
|
} ghostty_surface_config_s;
|
||||||
|
|
||||||
typedef void (*ghostty_runtime_wakeup_cb)(void *);
|
typedef void (*ghostty_runtime_wakeup_cb)(void *);
|
||||||
|
@ -4,6 +4,23 @@
|
|||||||
<dict>
|
<dict>
|
||||||
<key>CFBundleDocumentTypes</key>
|
<key>CFBundleDocumentTypes</key>
|
||||||
<array>
|
<array>
|
||||||
|
<dict>
|
||||||
|
<key>CFBundleTypeExtensions</key>
|
||||||
|
<array>
|
||||||
|
<string>command</string>
|
||||||
|
<string>tool</string>
|
||||||
|
<string>sh</string>
|
||||||
|
<string>zsh</string>
|
||||||
|
<string>csh</string>
|
||||||
|
<string>pl</string>
|
||||||
|
</array>
|
||||||
|
<key>CFBundleTypeIconFile</key>
|
||||||
|
<string>AppIcon.icns</string>
|
||||||
|
<key>CFBundleTypeName</key>
|
||||||
|
<string>Terminal scripts</string>
|
||||||
|
<key>CFBundleTypeRole</key>
|
||||||
|
<string>Editor</string>
|
||||||
|
</dict>
|
||||||
<dict>
|
<dict>
|
||||||
<key>CFBundleTypeName</key>
|
<key>CFBundleTypeName</key>
|
||||||
<string>Folders</string>
|
<string>Folders</string>
|
||||||
|
@ -216,22 +216,23 @@ class AppDelegate: NSObject,
|
|||||||
// this way.
|
// this way.
|
||||||
var isDirectory = ObjCBool(true)
|
var isDirectory = ObjCBool(true)
|
||||||
guard FileManager.default.fileExists(atPath: filename, isDirectory: &isDirectory) else { return false }
|
guard FileManager.default.fileExists(atPath: filename, isDirectory: &isDirectory) else { return false }
|
||||||
guard isDirectory.boolValue else {
|
|
||||||
let alert = NSAlert()
|
|
||||||
alert.messageText = "Dropped File is Not a Directory"
|
|
||||||
alert.informativeText = "Ghostty can currently only open directory paths."
|
|
||||||
alert.addButton(withTitle: "OK")
|
|
||||||
alert.alertStyle = .warning
|
|
||||||
_ = alert.runModal()
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build our config
|
// Initialize the surface config which will be used to create the tab or window for the opened file.
|
||||||
var config = Ghostty.SurfaceConfiguration()
|
var config = Ghostty.SurfaceConfiguration()
|
||||||
config.workingDirectory = filename
|
|
||||||
|
if (isDirectory.boolValue) {
|
||||||
// Add a new tab or create a new window
|
// When opening a directory, create a new tab in the main window with that as the working directory.
|
||||||
terminalManager.newTab(withBaseConfig: config)
|
// If no windows exist, a new one will be created.
|
||||||
|
config.workingDirectory = filename
|
||||||
|
terminalManager.newTab(withBaseConfig: config)
|
||||||
|
} else {
|
||||||
|
// When opening a file, open a new window with that file as the command,
|
||||||
|
// and its parent directory as the working directory.
|
||||||
|
config.command = filename
|
||||||
|
config.workingDirectory = (filename as NSString).deletingLastPathComponent
|
||||||
|
terminalManager.newWindow(withBaseConfig: config)
|
||||||
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -206,12 +206,16 @@ extension Ghostty {
|
|||||||
|
|
||||||
/// Explicit working directory to set
|
/// Explicit working directory to set
|
||||||
var workingDirectory: String? = nil
|
var workingDirectory: String? = nil
|
||||||
|
|
||||||
|
/// Explicit command to set
|
||||||
|
var command: String? = nil
|
||||||
|
|
||||||
init() {}
|
init() {}
|
||||||
|
|
||||||
init(from config: ghostty_surface_config_s) {
|
init(from config: ghostty_surface_config_s) {
|
||||||
self.fontSize = config.font_size
|
self.fontSize = config.font_size
|
||||||
self.workingDirectory = String.init(cString: config.working_directory, encoding: .utf8)
|
self.workingDirectory = String.init(cString: config.working_directory, encoding: .utf8)
|
||||||
|
self.command = String.init(cString: config.command, encoding: .utf8)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the ghostty configuration for this surface configuration struct. The memory
|
/// Returns the ghostty configuration for this surface configuration struct. The memory
|
||||||
@ -226,6 +230,9 @@ extension Ghostty {
|
|||||||
if let workingDirectory = workingDirectory {
|
if let workingDirectory = workingDirectory {
|
||||||
config.working_directory = (workingDirectory as NSString).utf8String
|
config.working_directory = (workingDirectory as NSString).utf8String
|
||||||
}
|
}
|
||||||
|
if let command = command {
|
||||||
|
config.command = (command as NSString).utf8String
|
||||||
|
}
|
||||||
|
|
||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
|
@ -253,6 +253,9 @@ pub const Surface = struct {
|
|||||||
|
|
||||||
/// The working directory to load into.
|
/// The working directory to load into.
|
||||||
working_directory: [*:0]const u8 = "",
|
working_directory: [*:0]const u8 = "",
|
||||||
|
|
||||||
|
/// The command to run in the new surface.
|
||||||
|
command: [*:0]const u8 = "",
|
||||||
};
|
};
|
||||||
|
|
||||||
/// This is the key event sent for ghostty_surface_key.
|
/// This is the key event sent for ghostty_surface_key.
|
||||||
@ -326,6 +329,13 @@ pub const Surface = struct {
|
|||||||
config.@"working-directory" = wd;
|
config.@"working-directory" = wd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If we have a command from the options then we set it.
|
||||||
|
const cm = std.mem.sliceTo(opts.command, 0);
|
||||||
|
if (cm.len > 0) {
|
||||||
|
// TODO: Maybe add some validation to this, like the working directory has?
|
||||||
|
config.command = cm;
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize our surface right away. We're given a view that is
|
// Initialize our surface right away. We're given a view that is
|
||||||
// ready to use.
|
// ready to use.
|
||||||
try self.core_surface.init(
|
try self.core_surface.init(
|
||||||
|
Reference in New Issue
Block a user