1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-24 13:52:55 +03:00

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}),
    }
  },
}
```
This commit is contained in:
Wez Furlong 2020-06-16 08:27:35 -07:00
parent ff32dc727f
commit d9f84da6ec
2 changed files with 31 additions and 6 deletions

View File

@ -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 `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 behaves more like an `AltGr` key and generates the composed input, while the
Left Alt is regular uncomposed Alt. 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 ### 20200607-144723-74889cd4

View File

@ -4,6 +4,7 @@ use crate::config::{FontAttributes, TextStyle};
use anyhow::anyhow; use anyhow::anyhow;
use bstr::BString; use bstr::BString;
use mlua::{Lua, Table, Value}; use mlua::{Lua, Table, Value};
use serde::*;
use std::path::Path; use std::path::Path;
mod serde_lua; mod serde_lua;
@ -152,6 +153,22 @@ fn hostname<'lua>(_: &'lua Lua, _: ()) -> mlua::Result<String> {
} }
} }
#[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<termwiz::color::RgbColor>,
}
impl_lua_conversion!(TextStyleAttributes);
/// Given a simple font family name, returns a text style instance. /// Given a simple font family name, returns a text style instance.
/// The second optional argument is a list of the other TextStyle /// The second optional argument is a list of the other TextStyle
/// fields, which at the time of writing includes only the /// fields, which at the time of writing includes only the
@ -163,15 +180,18 @@ fn hostname<'lua>(_: &'lua Lua, _: ()) -> mlua::Result<String> {
/// `{ font = {{ family = "foo" }}, foreground="tomato"}` /// `{ font = {{ family = "foo" }}, foreground="tomato"}`
fn font<'lua>( fn font<'lua>(
_lua: &'lua Lua, _lua: &'lua Lua,
(family, map_defaults): (String, Option<TextStyle>), (family, map_defaults): (String, Option<TextStyleAttributes>),
) -> mlua::Result<TextStyle> { ) -> mlua::Result<TextStyle> {
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.clear();
text_style.font.push(FontAttributes { text_style.font.push(FontAttributes {
family, family,
..Default::default() bold: attrs.bold,
italic: attrs.italic,
}); });
text_style.foreground = attrs.foreground;
Ok(text_style) Ok(text_style)
} }
@ -185,17 +205,20 @@ fn font<'lua>(
/// as described by the `wezterm.font` documentation. /// as described by the `wezterm.font` documentation.
fn font_with_fallback<'lua>( fn font_with_fallback<'lua>(
_lua: &'lua Lua, _lua: &'lua Lua,
(fallback, map_defaults): (Vec<String>, Option<TextStyle>), (fallback, map_defaults): (Vec<String>, Option<TextStyleAttributes>),
) -> mlua::Result<TextStyle> { ) -> mlua::Result<TextStyle> {
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.clear();
for family in fallback { for family in fallback {
text_style.font.push(FontAttributes { text_style.font.push(FontAttributes {
family, family,
..Default::default() bold: attrs.bold,
italic: attrs.italic,
}); });
} }
text_style.foreground = attrs.foreground;
Ok(text_style) Ok(text_style)
} }