Files
ghostty-rs/crates/ghostty-config/src/edit.rs
T
ddidderr 33d15a2dc2 feat(config): port the configuration system and CLI argument parser
Ports ZIG/src/config (minus C/WASM APIs) and cli/{args,CommaSplitter,
diagnostics}.zig into ghostty-config:
- all 202 options with exact names, types, defaults and verbatim docs,
  declared through one config_struct! macro; Zig's comptime reflection is
  replaced by the ConfigField/CliTarget traits
- loading: default XDG files, -e, config-file includes (optional '?',
  cycle detection), themes incl. light:/dark: pairs, conditional config,
  replay, finalize() derived defaults, clone/changed diffing for reload
- Linux/GTK default keybinds built on ghostty_input::binding::Set
- formatter producing +show-config output

Verified against the installed Ghostty 1.3.1: `+show-config --default`
and `--default --docs` match byte for byte (except gtk-opengl-debug,
which differs between debug/release builds), as do two complex loaded
configs.

Open items (docs/rewrite/notes/ghostty-config.md): url regex needs an
Oniguruma-compatible engine (onig) in the renderer; some font/terminal
types are local copies awaiting integration; KeyRemapSet equality is
order sensitive unlike Zig.

Test Plan:
- cargo test -p ghostty-config (255 tests)
- compare `ghostty +show-config --default --docs` with our formatter
2026-10-11 22:00:12 +02:00

89 lines
2.7 KiB
Rust

//! Opening the config for editing (port of `config/edit.zig`).
use crate::file_load;
use std::fs::OpenOptions;
use std::io;
use std::path::Path;
/// The path to the configuration that should be opened for editing.
///
/// On Linux, this will use the file at the XDG config path. This is the
/// only valid path for Linux so we don't need to check for other paths.
///
/// (On macOS both XDG and AppSupport paths are valid; macOS is not
/// supported by this port.)
///
/// The config file (and its directory) is created if it doesn't exist.
pub fn open_path() -> io::Result<String> {
// Get the path we should open
let config_path = config_path()?;
// Create config directory recursively.
if let Some(config_dir) = Path::new(&config_path).parent() {
std::fs::create_dir_all(config_dir)?;
}
// Try to create file and go on if it already exists
match OpenOptions::new()
.write(true)
.create_new(true)
.open(&config_path)
{
Ok(_) => {}
Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {}
Err(e) => return Err(e),
}
Ok(config_path)
}
fn xdg_err(e: ghostty_os::xdg::Error) -> io::Error {
io::Error::new(io::ErrorKind::NotFound, e)
}
/// Returns the config path to use for open for the current OS.
fn config_path() -> io::Result<String> {
let paths = config_path_candidates()?;
// Find the first path that exists and is non-empty. If no paths are
// non-empty but at least one exists, we will return the first path that
// exists.
let mut exists: Option<&String> = None;
for path in &paths {
let meta = match std::fs::File::open(path).and_then(|f| f.metadata()) {
Ok(m) => m,
// File doesn't exist, continue.
Err(e) if e.kind() == io::ErrorKind::NotFound => continue,
// Some other error, assume it exists and return it.
Err(e) => return Err(e),
};
// If the file is non-empty, return it.
if meta.len() > 0 {
return Ok(path.clone());
}
// If the file is empty, remember it exists.
if exists.is_none() {
exists = Some(path);
}
}
// No paths are non-empty, return the first path that exists.
if let Some(v) = exists {
return Ok(v.clone());
}
// No paths are non-empty or exist, return the first path.
Ok(paths[0].clone())
}
/// Returns a list of possible paths the main config file could be
/// in for the current OS.
fn config_path_candidates() -> io::Result<Vec<String>> {
Ok(vec![
file_load::default_xdg_path().map_err(xdg_err)?,
file_load::legacy_default_xdg_path().map_err(xdg_err)?,
])
}