From d9f84da6ec0a9808b5bef9343311fcc7ef371bae Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 16 Jun 2020 08:27:35 -0700 Subject: [PATCH] fonts: fix second parameter of wezterm.font The following configuration now works as intended: ```lua local wezterm = require 'wezterm'; return { -- font = wezterm.font('JetBrains Mono'), font_rules = { { italic = false, intensity = "Normal", font = wezterm.font("JetBrains Mono"), }, { italic = true, font = wezterm.font("JetBrains Mono", {italic=true}), }, { intensity = "Bold", font = wezterm.font("JetBrains Mono", {bold=true}), }, { italic = true, intensity = "Bold", font = wezterm.font("JetBrains Mono", {bold=true, italic=true}), } }, } ``` --- docs/changelog.markdown | 2 ++ src/scripting/mod.rs | 35 +++++++++++++++++++++++++++++------ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/docs/changelog.markdown b/docs/changelog.markdown index 56649e04b..33714d4ef 100644 --- a/docs/changelog.markdown +++ b/docs/changelog.markdown @@ -34,6 +34,8 @@ brief notes about them may accumulate here. `send_composed_key_when_right_alt_is_pressed=true` so that the right Alt key behaves more like an `AltGr` key and generates the composed input, while the Left Alt is regular uncomposed Alt. +* Fonts: fixed an issue where specifying italic or bold in the second parameter + of `wezterm.font` didn't work as intended or documented ### 20200607-144723-74889cd4 diff --git a/src/scripting/mod.rs b/src/scripting/mod.rs index 4b7fb8416..d45c53dda 100644 --- a/src/scripting/mod.rs +++ b/src/scripting/mod.rs @@ -4,6 +4,7 @@ use crate::config::{FontAttributes, TextStyle}; use anyhow::anyhow; use bstr::BString; use mlua::{Lua, Table, Value}; +use serde::*; use std::path::Path; mod serde_lua; @@ -152,6 +153,22 @@ fn hostname<'lua>(_: &'lua Lua, _: ()) -> mlua::Result { } } +#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)] +struct TextStyleAttributes { + /// Whether the font should be a bold variant + #[serde(default)] + pub bold: bool, + /// Whether the font should be an italic variant + #[serde(default)] + pub italic: bool, + /// If set, when rendering text that is set to the default + /// foreground color, use this color instead. This is most + /// useful in a `[[font_rules]]` section to implement changing + /// the text color for eg: bold text. + pub foreground: Option, +} +impl_lua_conversion!(TextStyleAttributes); + /// Given a simple font family name, returns a text style instance. /// The second optional argument is a list of the other TextStyle /// fields, which at the time of writing includes only the @@ -163,15 +180,18 @@ fn hostname<'lua>(_: &'lua Lua, _: ()) -> mlua::Result { /// `{ font = {{ family = "foo" }}, foreground="tomato"}` fn font<'lua>( _lua: &'lua Lua, - (family, map_defaults): (String, Option), + (family, map_defaults): (String, Option), ) -> mlua::Result { - let mut text_style = map_defaults.unwrap_or_else(TextStyle::default); + let attrs = map_defaults.unwrap_or_else(TextStyleAttributes::default); + let mut text_style = TextStyle::default(); text_style.font.clear(); text_style.font.push(FontAttributes { family, - ..Default::default() + bold: attrs.bold, + italic: attrs.italic, }); + text_style.foreground = attrs.foreground; Ok(text_style) } @@ -185,17 +205,20 @@ fn font<'lua>( /// as described by the `wezterm.font` documentation. fn font_with_fallback<'lua>( _lua: &'lua Lua, - (fallback, map_defaults): (Vec, Option), + (fallback, map_defaults): (Vec, Option), ) -> mlua::Result { - let mut text_style = map_defaults.unwrap_or_else(TextStyle::default); + let attrs = map_defaults.unwrap_or_else(TextStyleAttributes::default); + let mut text_style = TextStyle::default(); text_style.font.clear(); for family in fallback { text_style.font.push(FontAttributes { family, - ..Default::default() + bold: attrs.bold, + italic: attrs.italic, }); } + text_style.foreground = attrs.foreground; Ok(text_style) }