From 67d8848676e5280b0bb1842cdf3c5604f076490e Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Wed, 14 Apr 2021 09:06:02 -0700 Subject: [PATCH] ls-fonts: refine output a bit It now outputs something that you could conceivably put into your config file, although the intent is to show the canonical way to reference the individual fonts that were found, rather than to specify a fully baked list to paste into a config. eg: ``` ; ./target/debug/wezterm ls-fonts Primary font: wezterm.font_with_fallback({ -- /home/wez/.fonts/OperatorMonoSSmLig-Medium.otf, FontDirs {family="Operator Mono SSm Lig", weight="DemiLight"}, -- /home/wez/.fonts/OperatorMonoSSmLig-Medium.otf, FontConfig {family="Operator Mono SSm Lig", weight="DemiLight"}, -- /home/wez/.fonts/MaterialDesignIconsDesktop.ttf, FontDirs "Material Design Icons Desktop", -- /home/wez/.fonts/terminus-bold.otb, FontDirs {family="Terminus", weight="Bold"}, -- /home/wez/.fonts/JetBrainsMono-Regular.ttf, FontDirs "JetBrains Mono", -- /home/wez/.fonts/NotoColorEmoji.ttf, FontDirs "Noto Color Emoji", -- /home/wez/.fonts/MaterialDesignIconsDesktop.ttf, FontConfig "Material Design Icons Desktop", -- /usr/share/fonts/terminus-fonts/ter-u32n.otb, FontConfig "Terminus", -- /home/wez/.fonts/JetBrainsMono-Regular.ttf, FontConfig "JetBrains Mono", -- /home/wez/.fonts/NotoColorEmoji.ttf, FontConfig "Noto Color Emoji", -- , BuiltIn "Last Resort High-Efficiency", }) ``` --- wezterm-font/src/parser.rs | 27 +++++++++++++++++++++++++++ wezterm-gui/src/main.rs | 19 +++++++++---------- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/wezterm-font/src/parser.rs b/wezterm-font/src/parser.rs index 84bd0dedc..7662ddb9f 100644 --- a/wezterm-font/src/parser.rs +++ b/wezterm-font/src/parser.rs @@ -125,6 +125,33 @@ impl ParsedFont { ) } + pub fn lua_fallback(handles: &[Self]) -> String { + let mut code = "wezterm.font_with_fallback({\n".to_string(); + + for p in handles { + code.push_str(&format!(" -- {}\n", p.handle.diagnostic_string())); + + if p.weight == FontWeight::Regular && p.stretch == FontStretch::Normal && !p.italic { + code.push_str(&format!(" \"{}\",\n", p.names.family)); + } else { + code.push_str(&format!(" {{family=\"{}\"", p.names.family)); + if p.weight != FontWeight::Regular { + code.push_str(&format!(", weight=\"{}\"", p.weight)); + } + if p.stretch != FontStretch::Normal { + code.push_str(&format!(", stretch=\"{}\"", p.stretch)); + } + if p.italic { + code.push_str(", italic=true"); + } + code.push_str("},\n") + } + code.push_str("\n"); + } + code.push_str("})"); + code + } + pub fn from_face(face: &crate::ftwrap::Face, handle: FontDataHandle) -> anyhow::Result { let italic = face.italic(); let (weight, width) = face.weight_and_width(); diff --git a/wezterm-gui/src/main.rs b/wezterm-gui/src/main.rs index ac1c4b92c..43b717cee 100644 --- a/wezterm-gui/src/main.rs +++ b/wezterm-gui/src/main.rs @@ -414,6 +414,8 @@ fn maybe_show_configuration_error_window() { } pub fn run_ls_fonts(config: config::ConfigHandle, _cmd: &LsFontsCommand) -> anyhow::Result<()> { + use wezterm_font::parser::ParsedFont; + // Disable the normal config error UI window, as we don't have // a fully baked GUI environment running config::assign_error_callback(|err| eprintln!("{}", err)); @@ -422,11 +424,11 @@ pub fn run_ls_fonts(config: config::ConfigHandle, _cmd: &LsFontsCommand) -> anyh println!("Primary font:"); let default_font = font_config.default_font()?; - for f in default_font.clone_handles() { - println!(" {}", f.lua_name()); - println!(" ({})", f.handle.diagnostic_string()); - println!(); - } + println!( + "{}", + ParsedFont::lua_fallback(&default_font.clone_handles()) + ); + println!(); for rule in &config.font_rules { println!(); @@ -456,11 +458,8 @@ pub fn run_ls_fonts(config: config::ConfigHandle, _cmd: &LsFontsCommand) -> anyh println!("{}:", condition); let font = font_config.resolve_font(&rule.font)?; - for f in font.clone_handles() { - println!(" {}", f.lua_name()); - println!(" ({})", f.handle.diagnostic_string()); - println!(); - } + println!("{}", ParsedFont::lua_fallback(&font.clone_handles())); + println!(); } Ok(())