From 7600c761ef5f8415f0c8097eda3abde678c65368 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 18 Dec 2023 08:00:40 -0800 Subject: [PATCH] fix callback struct ordering, use internal_os.open --- include/ghostty.h | 4 +-- macos/Sources/Ghostty/AppState.swift | 2 +- src/apprt/embedded.zig | 2 +- src/config.zig | 1 + src/config/CAPI.zig | 3 ++- src/config/Config.zig | 37 ---------------------------- src/config/edit.zig | 29 ++++++++++++++++++++++ 7 files changed, 36 insertions(+), 42 deletions(-) create mode 100644 src/config/edit.zig diff --git a/include/ghostty.h b/include/ghostty.h index 8c19150b1..0b70e2549 100644 --- a/include/ghostty.h +++ b/include/ghostty.h @@ -353,8 +353,8 @@ typedef struct { } ghostty_surface_config_s; typedef void (*ghostty_runtime_wakeup_cb)(void *); -typedef void (*ghostty_runtime_open_config_cb)(void *); typedef const ghostty_config_t (*ghostty_runtime_reload_config_cb)(void *); +typedef void (*ghostty_runtime_open_config_cb)(void *); typedef void (*ghostty_runtime_set_title_cb)(void *, const char *); typedef void (*ghostty_runtime_set_mouse_shape_cb)(void *, ghostty_mouse_shape_e); typedef void (*ghostty_runtime_set_mouse_visibility_cb)(void *, bool); @@ -381,8 +381,8 @@ typedef struct { void *userdata; bool supports_selection_clipboard; ghostty_runtime_wakeup_cb wakeup_cb; - ghostty_runtime_open_config_cb open_config_cb; ghostty_runtime_reload_config_cb reload_config_cb; + ghostty_runtime_open_config_cb open_config_cb; ghostty_runtime_set_title_cb set_title_cb; ghostty_runtime_set_mouse_shape_cb set_mouse_shape_cb; ghostty_runtime_set_mouse_visibility_cb set_mouse_visibility_cb; diff --git a/macos/Sources/Ghostty/AppState.swift b/macos/Sources/Ghostty/AppState.swift index df1bc56bd..ca457f211 100644 --- a/macos/Sources/Ghostty/AppState.swift +++ b/macos/Sources/Ghostty/AppState.swift @@ -146,8 +146,8 @@ extension Ghostty { userdata: Unmanaged.passUnretained(self).toOpaque(), supports_selection_clipboard: false, wakeup_cb: { userdata in AppState.wakeup(userdata) }, - open_config_cb: { userdata in AppState.openConfig(userdata) }, reload_config_cb: { userdata in AppState.reloadConfig(userdata) }, + open_config_cb: { userdata in AppState.openConfig(userdata) }, set_title_cb: { userdata, title in AppState.setTitle(userdata, title: title) }, set_mouse_shape_cb: { userdata, shape in AppState.setMouseShape(userdata, shape: shape) }, set_mouse_visibility_cb: { userdata, visible in AppState.setMouseVisibility(userdata, visible: visible) }, diff --git a/src/apprt/embedded.zig b/src/apprt/embedded.zig index 0a4a59678..724eb395f 100644 --- a/src/apprt/embedded.zig +++ b/src/apprt/embedded.zig @@ -164,7 +164,7 @@ pub const App = struct { } pub fn openConfig(self: *App) !void { - try Config.edit(self.core_app.alloc); + try configpkg.edit.open(self.core_app.alloc); } pub fn reloadConfig(self: *App) !?*const Config { diff --git a/src/config.zig b/src/config.zig index cd449fb38..57c4bcd88 100644 --- a/src/config.zig +++ b/src/config.zig @@ -3,6 +3,7 @@ const builtin = @import("builtin"); pub usingnamespace @import("config/key.zig"); pub const Config = @import("config/Config.zig"); pub const string = @import("config/string.zig"); +pub const edit = @import("config/edit.zig"); pub const url = @import("config/url.zig"); // Field types diff --git a/src/config/CAPI.zig b/src/config/CAPI.zig index 9213f7cf8..cff6a4ea3 100644 --- a/src/config/CAPI.zig +++ b/src/config/CAPI.zig @@ -5,6 +5,7 @@ const global = &@import("../main.zig").state; const Config = @import("Config.zig"); const c_get = @import("c_get.zig"); +const edit = @import("edit.zig"); const Key = @import("key.zig").Key; const log = std.log.scoped(.config); @@ -121,7 +122,7 @@ export fn ghostty_config_get_error(self: *Config, idx: u32) Error { } export fn ghostty_config_open() void { - Config.edit(global.alloc) catch |err| { + edit.open(global.alloc) catch |err| { log.err("error opening config in editor err={}", .{err}); }; } diff --git a/src/config/Config.zig b/src/config/Config.zig index d52546bed..f99cd989a 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -19,7 +19,6 @@ const Key = @import("key.zig").Key; const KeyValue = @import("key.zig").Value; const ErrorList = @import("ErrorList.zig"); const MetricModifier = fontpkg.face.Metrics.Modifier; -const Command = @import("../Command.zig"); const log = std.log.scoped(.config); @@ -820,42 +819,6 @@ pub fn deinit(self: *Config) void { self.* = undefined; } -/// Open the configuration in the OS default editor according to the default paths the main config file could be in: -/// -/// 1. XDG Config File -/// -pub fn edit(alloc_gpa: Allocator) !void { - // default location - const config_path = try internal_os.xdg.config(alloc_gpa, .{ .subdir = "ghostty/config" }); - defer alloc_gpa.free(config_path); - - // Try to create file and go on if it already exists - _ = std.fs.createFileAbsolute(config_path, .{ .exclusive = true }) catch |err| { - switch (err) { - error.PathAlreadyExists => log.info("config file found at {s}", .{config_path}), - else => return err, - } - }; - - // TODO: maybe add editor config property to allow users to set the editor they want to use when opening a file. - const editor = try Command.expandPath(alloc_gpa, "open") orelse "/usr/bin/open"; // should always be found, but worse case we use a hardcoded absolute path. - defer alloc_gpa.free(editor); - - // the command to run - const argv = [_][]const u8{ editor, config_path }; - - var proc = std.ChildProcess.init(&argv, alloc_gpa); - proc.stdin_behavior = .Ignore; - proc.stdout_behavior = .Ignore; - proc.stderr_behavior = .Ignore; - - try proc.spawn(); - log.info("started subcommand path={s} pid={?}", .{ editor, proc.id }); - - // the process only ends after this call returns. - if (try proc.wait() != std.ChildProcess.Term.Exited) return error.ExitError; -} - /// Load the configuration according to the default rules: /// /// 1. Defaults diff --git a/src/config/edit.zig b/src/config/edit.zig new file mode 100644 index 000000000..8f223afcc --- /dev/null +++ b/src/config/edit.zig @@ -0,0 +1,29 @@ +const std = @import("std"); +const builtin = @import("builtin"); +const assert = std.debug.assert; +const Allocator = std.mem.Allocator; +const internal_os = @import("../os/main.zig"); +const Command = @import("../Command.zig"); + +const log = std.log.scoped(.config); + +/// Open the configuration in the OS default editor according to the default +/// paths the main config file could be in. +pub fn open(alloc_gpa: Allocator) !void { + // default location + const config_path = try internal_os.xdg.config(alloc_gpa, .{ .subdir = "ghostty/config" }); + defer alloc_gpa.free(config_path); + + // Try to create file and go on if it already exists + _ = std.fs.createFileAbsolute( + config_path, + .{ .exclusive = true }, + ) catch |err| { + switch (err) { + error.PathAlreadyExists => {}, + else => return err, + } + }; + + try internal_os.open(alloc_gpa, config_path); +}