Added macOS file open handler

This commit is contained in:
Qwerasd
2024-01-16 16:26:21 -05:00
parent 771cbea0b0
commit 51f53aa9bf
5 changed files with 50 additions and 14 deletions

View File

@ -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 *);

View File

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

View File

@ -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() // Initialize the surface config which will be used to create the tab or window for the opened file.
alert.messageText = "Dropped File is Not a Directory" var config = Ghostty.SurfaceConfiguration()
alert.informativeText = "Ghostty can currently only open directory paths."
alert.addButton(withTitle: "OK") if (isDirectory.boolValue) {
alert.alertStyle = .warning // When opening a directory, create a new tab in the main window with that as the working directory.
_ = alert.runModal() // If no windows exist, a new one will be created.
return false 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)
} }
// Build our config
var config = Ghostty.SurfaceConfiguration()
config.workingDirectory = filename
// Add a new tab or create a new window
terminalManager.newTab(withBaseConfig: config)
return true return true
} }

View File

@ -207,11 +207,15 @@ 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
} }

View File

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