Files
ghostty-rs/crates/ghostty-config/src/formatter.rs
T
ddidderr 67367dcaf5 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

97 lines
2.4 KiB
Rust

//! Config formatting (port of `config/formatter.zig`). Output matches
//! Ghostty's `+show-config`.
use crate::cli_args::ConfigField;
use std::fmt;
/// A single entry formatter for the given field name and writer.
pub struct EntryFormatter<'a> {
pub name: &'a str,
pub writer: &'a mut dyn fmt::Write,
}
impl<'a> EntryFormatter<'a> {
pub fn new(name: &'a str, writer: &'a mut dyn fmt::Write) -> Self {
Self { name, writer }
}
/// Format a value with this formatter's name.
pub fn format_entry<T: ConfigField>(&mut self, value: &T) -> fmt::Result {
value.format_entry(self)
}
}
/// Format a single value with the given name.
pub fn format_entry<T: ConfigField>(
name: &str,
value: &T,
writer: &mut dyn fmt::Write,
) -> fmt::Result {
let mut f = EntryFormatter::new(name, writer);
value.format_entry(&mut f)
}
/// `void` values (Zig) print as `name = \n`.
pub fn format_void(name: &str, writer: &mut dyn fmt::Write) -> fmt::Result {
writeln!(writer, "{name} = ")
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
use crate::config_packed;
fn fe<T: ConfigField>(name: &str, v: &T) -> String {
let mut s = String::new();
format_entry(name, v, &mut s).unwrap();
s
}
#[test]
fn format_entry_bool() {
assert_eq!(fe("a", &true), "a = true\n");
assert_eq!(fe("a", &false), "a = false\n");
}
#[test]
fn format_entry_int() {
assert_eq!(fe("a", &123u8), "a = 123\n");
}
#[test]
fn format_entry_float() {
assert_eq!(fe("a", &0.7f64), "a = 0.7\n");
}
#[test]
fn format_entry_enum() {
crate::config_enum! { enum E { One => "one", Two => "two", Three => "three" } }
assert_eq!(fe("a", &E::Two), "a = two\n");
}
#[test]
fn format_entry_void() {
let mut s = String::new();
format_void("a", &mut s).unwrap();
assert_eq!(s, "a = \n");
}
#[test]
fn format_entry_optional() {
assert_eq!(fe("a", &None::<bool>), "a = \n");
assert_eq!(fe("a", &Some(false)), "a = false\n");
}
#[test]
fn format_entry_string() {
assert_eq!(fe("a", &"hello".to_string()), "a = hello\n");
}
#[test]
fn format_entry_packed_struct() {
config_packed! { struct V { "one" => one: true, "two" => two: false } }
assert_eq!(fe("a", &V::default()), "a = one,no-two\n");
}
}