Files
ghostty-rs/crates/ghostty-cli/src/show_config.rs
T
ddidderr f2c4b40062 feat(cli): port ghostty +action CLI commands
Ports ZIG/src/cli into ghostty-cli: action detection/dispatch with
per-action --help, and the actions help, version, list-actions,
list-colors, list-keybinds, list-themes (plain listing plus an
interactive ratatui browser mirroring Ghostty's libvaxis UI),
show-config, validate-config, edit-config, crash-report (listing),
ssh-cache and boo (235 embedded frames).

Verified byte-for-byte against the installed Ghostty 1.3.1 for stdout,
stderr and exit codes of most actions, incl. tty output in a pty at
widths 20-200. Known differences: +list-colors --plain prints all 782
colors (Ghostty truncates at 617 because it never flushes), +version
has placeholder build info until build_info::set is wired up.

Deferred: list-fonts, show-face (need font discovery integration) and
new-window (needs GTK IPC); they return NotYetAvailable for now.

Test Plan:
- cargo test -p ghostty-cli (86 tests)
- cargo run -p ghostty-cli --example cli -- +list-keybinds --plain
  and diff against `ghostty +list-keybinds --plain`
2026-10-11 22:22:01 +02:00

129 lines
3.9 KiB
Rust

//! `+show-config` (ZIG/src/cli/show_config.zig).
use std::io::Write;
use ghostty_config::config::Config;
use ghostty_config::config_struct;
use ghostty_config::file_formatter::FileFormatter;
use crate::error::Error;
config_struct! {
pub struct Options {
/// If true, do not load the user configuration, only load the defaults.
"default" => default: bool = false,
/// Only show the options that have been changed from the default.
/// This has no effect if `--default` is specified.
"changes-only" => changes_only: bool = true,
/// If true print the documentation above each option as a comment,
/// if available.
"docs" => docs: bool = false,
}
}
cli_options!(Options, help);
/// The `show-config` command shows the current configuration in a valid Ghostty
/// configuration file format.
///
/// When executed without any arguments this will output the current
/// configuration that is different from the default configuration. If you're
/// using the default configuration this will output nothing.
///
/// If you are a new user and want to see all available options with
/// documentation, run `ghostty +show-config --default --docs`.
///
/// The output is not in any specific order, but the order should be consistent
/// between runs. The output is not guaranteed to be exactly match the input
/// configuration files, but it will result in the same behavior. Comments,
/// whitespace, and other formatting is not preserved from user configuration
/// files.
///
/// Flags:
///
/// * `--default`: Show the default configuration instead of loading
/// the user configuration.
///
/// * `--changes-only`: Only show the options that have been changed
/// from the default. This has no effect if `--default` is specified.
///
/// * `--docs`: Print the documentation above each option as a comment,
/// This is very noisy but is very useful to learn about available
/// options, especially paired with `--default`.
pub fn run(args: &[String]) -> Result<u8, Error> {
let mut opts = Options::default();
crate::parse_options(&mut opts, args)?;
let config = if opts.default {
Config::new_default()
} else {
Config::load()?
};
let text = format_config(&config, &opts)?;
let mut stdout = std::io::stdout().lock();
stdout.write_all(text.as_bytes())?;
stdout.flush()?;
Ok(0)
}
/// Format `config` according to `opts`.
pub fn format_config(config: &Config, opts: &Options) -> Result<String, Error> {
let fmt = FileFormatter {
config,
changed: !opts.default && opts.changes_only,
docs: opts.docs,
};
let mut out = String::new();
fmt.format(&mut out)?;
Ok(out)
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used)]
use super::*;
#[test]
fn default_changes_only_is_empty() {
let config = Config::new_default();
let opts = Options::default();
assert_eq!("", format_config(&config, &opts).unwrap());
}
#[test]
fn default_docs_has_comments() {
let config = Config::new_default();
let opts = Options {
default: true,
docs: true,
..Options::default()
};
let s = format_config(&config, &opts).unwrap();
assert!(s.contains("# The font families to use."));
assert!(s.contains("\nfont-size = 12\n"));
}
#[test]
fn options_parse() {
let mut opts = Options::default();
let args: Vec<String> = [
"ghostty",
"+show-config",
"--default",
"--changes-only=false",
]
.iter()
.map(ToString::to_string)
.collect();
crate::parse_options(&mut opts, &args).unwrap();
assert!(opts.default);
assert!(!opts.changes_only);
assert!(matches!(
crate::parse_options(&mut opts, &["g".into(), "--help".into()]),
Err(Error::ActionHelpRequested)
));
}
}