Add settings shortcut on MacOS

- Settings shortcut opens the config file in the default editor.

Signed-off-by: Borja Clemente <borja.clemente@gmail.com>
This commit is contained in:
Borja Clemente
2023-12-17 14:22:38 +01:00
parent 5eea79ad0d
commit 646e3c365c
10 changed files with 104 additions and 2 deletions

View File

@@ -186,6 +186,7 @@ fn drainMailbox(self: *App, rt_app: *apprt.App) !void {
log.debug("mailbox message={s}", .{@tagName(message)});
switch (message) {
.reload_config => try self.reloadConfig(rt_app),
.open_config => try self.openConfig(rt_app),
.new_window => |msg| try self.newWindow(rt_app, msg),
.close => |surface| try self.closeSurface(surface),
.quit => try self.setQuit(),
@@ -196,6 +197,12 @@ fn drainMailbox(self: *App, rt_app: *apprt.App) !void {
}
}
pub fn openConfig(self: *App, rt_app: *apprt.App) !void {
_ = self;
log.debug("opening configuration", .{});
try rt_app.openConfig();
}
pub fn reloadConfig(self: *App, rt_app: *apprt.App) !void {
log.debug("reloading configuration", .{});
if (try rt_app.reloadConfig()) |new| {
@@ -274,6 +281,9 @@ pub const Message = union(enum) {
/// all the active surfaces.
reload_config: void,
// Open the configuration file
open_config: void,
/// Create a new terminal window.
new_window: NewWindow,

View File

@@ -2550,6 +2550,8 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !bool
.unbind => unreachable,
.ignore => {},
.open_config => try self.app.openConfig(self.rt_app),
.reload_config => try self.app.reloadConfig(self.rt_app),
.csi, .esc => |data| {

View File

@@ -47,6 +47,9 @@ pub const App = struct {
/// called.
reload_config: *const fn (AppUD) callconv(.C) ?*const Config,
/// Open the configuration file.
open_config: *const fn (AppUD) callconv(.C) void,
/// Called to set the title of the window.
set_title: *const fn (SurfaceUD, [*]const u8) callconv(.C) void,
@@ -160,6 +163,10 @@ pub const App = struct {
}
}
pub fn openConfig(self: *App) !void {
try Config.edit(self.core_app.alloc);
}
pub fn reloadConfig(self: *App) !?*const Config {
// Reload
if (self.opts.reload_config(self.opts.userdata)) |new| {
@@ -1285,6 +1292,14 @@ pub const CAPI = struct {
};
}
/// Open the configuration.
export fn ghostty_app_open_config(v: *App) void {
_ = v.core_app.openConfig(v) catch |err| {
log.err("error reloading config err={}", .{err});
return;
};
}
/// Reload the configuration.
export fn ghostty_app_reload_config(v: *App) void {
_ = v.core_app.reloadConfig(v) catch |err| {

View File

@@ -120,6 +120,12 @@ export fn ghostty_config_get_error(self: *Config, idx: u32) Error {
return .{ .message = err.message.ptr };
}
export fn ghostty_config_open() void {
Config.edit(global.alloc) catch |err| {
log.err("error opening config in editor err={}", .{err});
};
}
/// Sync with ghostty_error_s
const Error = extern struct {
message: [*:0]const u8 = "",

View File

@@ -19,6 +19,7 @@ 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);
@@ -819,6 +820,42 @@ 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
@@ -1121,7 +1158,11 @@ pub fn default(alloc_gpa: Allocator) Allocator.Error!Config {
.{ .key = .comma, .mods = .{ .super = true, .shift = true } },
.{ .reload_config = {} },
);
try result.keybind.set.put(
alloc,
.{ .key = .comma, .mods = .{ .super = true } },
.{ .open_config = {} },
);
try result.keybind.set.put(
alloc,
.{ .key = .k, .mods = .{ .super = true } },

View File

@@ -214,6 +214,9 @@ pub const Action = union(enum) {
/// focused terminal.
inspector: InspectorMode,
/// Open the configuration file in the default OS editor.
open_config: void,
/// Reload the configuration. The exact meaning depends on the app runtime
/// in use but this usually involves re-reading the configuration file
/// and applying any changes. Note that not all changes can be applied at