Escaped config strings (ghostty_os::osbytes) kept reaching OS boundaries undecoded, and decoding was occasionally applied to plain text. The root cause was that both were plain `String`s. Now: - `EncodedOs` is a newtype (no Display/AsRef<Path>) that decodes only via to_os/to_path/to_bytes/to_wide; plain text enters via from_plain_text. OS helpers (env, home, xdg, passwd, resources dir, path expansion), config::Path, WorkingDirectory, theme/file_load/edit paths, terminal pwd, OpenUrl/Pwd actions and termio cwd use it, so the compiler forces decoding at every boundary. os::open takes &OsStr. Config output is unchanged. Fixes all rows of docs/rewrite/notes/review-rv5-codex.md (final-Z7.md): screen-file copy; regex/OSC 8 URLs no longer decoded (regression); lossless Windows WTF-8/WTF-16 for args, env and CreateProcessW; Windows resources dir/TERMINFO; color OSC request lists and X11 color lookup no longer allocate globally (C zero-allocation test extended); macOS Mach handler resumes all threads before KERN_FAILURE (hang regression) and SIGABRT minidumps go through the exception thread; plus open-config, background images, crash dir, ssh cache, exe dir/PATH, fontconfig filenames, file links, kitty TMPDIR, .hushlogin and benchmark paths. The typing also surfaced and fixed gtk-custom-css, bell audio, bash HISTFILE, OSC 52 writes and the C config getters. Test Plan: - cargo test --workspace (3376 passed) - just check-macos (both), just check-windows, just check-wasm - diff <(ghostty-rs +show-config --default --docs) \ <(ghostty +show-config --default --docs)
145 lines
5.1 KiB
Rust
145 lines
5.1 KiB
Rust
//! `+edit-config` (ZIG/src/cli/edit_config.zig).
|
|
|
|
use std::io::Write;
|
|
#[cfg(unix)]
|
|
use std::os::unix::process::CommandExt;
|
|
|
|
use ghostty_config::config::Config;
|
|
use ghostty_os::osbytes::EncodedOs;
|
|
|
|
use crate::error::Error;
|
|
pub use crate::options::NoOptions as Options;
|
|
|
|
/// The `edit-config` command opens the Ghostty configuration file in the
|
|
/// editor specified by the `$VISUAL` or `$EDITOR` environment variables.
|
|
///
|
|
/// IMPORTANT: This command will not reload the configuration after
|
|
/// editing. You will need to manually reload the configuration using the
|
|
/// application menu, configured keybind, or by restarting Ghostty. We
|
|
/// plan to auto-reload in the future, but Ghostty isn't capable of
|
|
/// this yet.
|
|
///
|
|
/// The filepath opened is the default user-specific configuration
|
|
/// file, which is typically located at `$XDG_CONFIG_HOME/ghostty/config.ghostty`.
|
|
/// On macOS, this may also be located at
|
|
/// `~/Library/Application Support/com.mitchellh.ghostty/config.ghostty`.
|
|
/// On macOS, whichever path exists and is non-empty will be prioritized,
|
|
/// prioritizing the Application Support directory if neither are
|
|
/// non-empty.
|
|
///
|
|
/// This command prefers the `$VISUAL` environment variable over `$EDITOR`,
|
|
/// if both are set. If neither are set, it will print an error
|
|
/// and exit.
|
|
pub fn run(args: &[String]) -> Result<u8, Error> {
|
|
let mut stderr = std::io::stderr().lock();
|
|
|
|
let mut opts = Options;
|
|
crate::parse_options(&mut opts, args)?;
|
|
|
|
let result = run_inner(&mut stderr);
|
|
// Flushing *shouldn't* fail but...
|
|
let _ = stderr.flush();
|
|
result
|
|
}
|
|
|
|
/// Pick the editor from `$VISUAL` then `$EDITOR` (empty values are skipped).
|
|
#[must_use]
|
|
pub fn find_editor() -> Option<EncodedOs> {
|
|
// VISUAL vs. EDITOR: https://unix.stackexchange.com/questions/4859/visual-vs-editor-what-s-the-difference
|
|
for var in ["VISUAL", "EDITOR"] {
|
|
if let Some(v) = ghostty_os::getenv(var) {
|
|
if !v.is_empty() {
|
|
return Some(v);
|
|
}
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
fn run_inner(stderr: &mut dyn Write) -> Result<u8, Error> {
|
|
// We load the configuration once because that will write our
|
|
// default configuration files to disk. We don't use the config.
|
|
let _config = Config::load()?;
|
|
|
|
// Find the preferred path.
|
|
let path = ghostty_config::file_load::preferred_default_file_path()
|
|
.map_err(|e| Error::Other(e.to_string()))?;
|
|
|
|
// We don't currently support Windows because we use the exec syscall.
|
|
#[cfg(windows)]
|
|
{
|
|
write_windows_unsupported(stderr, &path)
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
exec_editor(stderr, &path)
|
|
}
|
|
|
|
/// Windows branch of `run`: print the path and fail.
|
|
#[cfg(windows)]
|
|
fn write_windows_unsupported(stderr: &mut dyn Write, path: &EncodedOs) -> Result<u8, Error> {
|
|
write!(
|
|
stderr,
|
|
"The `ghostty +edit-config` command is not supported on Windows.\n\
|
|
Please edit the configuration file manually at the following path:\n\
|
|
\n\
|
|
{}\n",
|
|
path.as_encoded()
|
|
)?;
|
|
Ok(1)
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
fn exec_editor(stderr: &mut dyn Write, path: &EncodedOs) -> Result<u8, Error> {
|
|
// Get our editor
|
|
let Some(editor) = find_editor() else {
|
|
// If we don't have `$EDITOR` set then we can't do anything
|
|
// but we can still print a helpful message.
|
|
let msg = String::from(
|
|
"The $EDITOR or $VISUAL environment variable is not set or is empty.\n\
|
|
This environment variable is required to edit the Ghostty configuration\n\
|
|
via this CLI command.\n\
|
|
\n\
|
|
Please set the environment variable to your preferred terminal\n\
|
|
text editor and try again.\n\
|
|
\n\
|
|
If you prefer to edit the configuration file another way,\n\
|
|
you can find the configuration file at the following path:\n\
|
|
\n",
|
|
);
|
|
// Output the path using the OSC8 sequence so that it is linked.
|
|
// (Paths are raw bytes: decode the reversible escape when writing.)
|
|
let p = path.as_encoded();
|
|
let msg = format!("{msg}\x1b]8;;file://{p}\x1b\\{p}\x1b]8;;\x1b\\\n");
|
|
stderr.write_all(&ghostty_os::osbytes::config_str_to_bytes(&msg))?;
|
|
return Ok(1);
|
|
};
|
|
|
|
// Flush before exec so no output is lost.
|
|
let _ = stderr.flush();
|
|
let err = std::process::Command::new(editor.to_os())
|
|
.arg(path.to_os())
|
|
.exec();
|
|
|
|
// If we reached this point then exec failed.
|
|
let err = match err.kind() {
|
|
std::io::ErrorKind::NotFound => "error.FileNotFound".to_string(),
|
|
std::io::ErrorKind::PermissionDenied => "error.AccessDenied".to_string(),
|
|
_ => err.to_string(),
|
|
};
|
|
let msg = format!(
|
|
"Failed to execute the editor. Error code={err}.\n\
|
|
\n\
|
|
This is usually due to the executable path not existing, invalid\n\
|
|
permissions, or the shell environment not being set up\n\
|
|
correctly.\n\
|
|
\n\
|
|
Editor: {}\n\
|
|
Path: {}\n",
|
|
editor.as_encoded(),
|
|
path.as_encoded()
|
|
);
|
|
stderr.write_all(&ghostty_os::osbytes::config_str_to_bytes(&msg))?;
|
|
Ok(1)
|
|
}
|