diff --git a/config/src/terminal.rs b/config/src/terminal.rs index dedf4c07edf..9d96c41f141 100644 --- a/config/src/terminal.rs +++ b/config/src/terminal.rs @@ -3,6 +3,7 @@ use crate::{configuration, ConfigHandle, NewlineCanon}; use std::sync::Mutex; use termwiz::cell::UnicodeVersion; +use termwiz::cell::setcellwidths; use wezterm_term::color::ColorPalette; use wezterm_term::config::BidiMode; @@ -105,7 +106,7 @@ impl wezterm_term::TerminalConfiguration for TermConfig { UnicodeVersion { version: config.unicode_version, ambiguous_are_wide: config.treat_east_asian_ambiguous_width_as_wide, - cellwidths: config.cellwidths.clone() + cellwidths: setcellwidths(config.cellwidths.clone()), } } diff --git a/termwiz/src/cell.rs b/termwiz/src/cell.rs index 08f124d7dbd..09d6bbc6fec 100644 --- a/termwiz/src/cell.rs +++ b/termwiz/src/cell.rs @@ -12,6 +12,7 @@ use std::hash::{Hash, Hasher}; use std::mem; use std::sync::Arc; use wezterm_dynamic::{FromDynamic, ToDynamic}; +use std::collections::HashMap; #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] @@ -888,11 +889,25 @@ pub struct CellWidth { pub width: u8, } +pub fn setcellwidths(cellwidths: Option>) -> Option> { + if let Some(ref cellwidths) = cellwidths { + let mut map: HashMap = HashMap::new(); + for cellwidth in cellwidths { + for i in cellwidth.first..cellwidth.last+1 { + map.insert(i, cellwidth.width); + } + } + return Some(map); + } else { + return None; + } +} + #[derive(Clone, Debug, Eq, PartialEq)] pub struct UnicodeVersion { pub version: u8, pub ambiguous_are_wide: bool, - pub cellwidths: Option>, + pub cellwidths: Option>, } impl UnicodeVersion { @@ -923,10 +938,8 @@ impl UnicodeVersion { #[inline] fn wcwidth(&self, c: char) -> usize { if let Some(ref cellwidths) = self.cellwidths { - for cellwidth in cellwidths { - if cellwidth.first <= c as u32 && c as u32 <= cellwidth.last { - return cellwidth.width.into() - } + if let Some(width) = cellwidths.get(&(c as u32)) { + return (*width).into() } } self.width(WCWIDTH_TABLE.classify(c)) diff --git a/wezterm-gui/src/main.rs b/wezterm-gui/src/main.rs index bbe24e2fa89..cdc0e6a2181 100644 --- a/wezterm-gui/src/main.rs +++ b/wezterm-gui/src/main.rs @@ -33,6 +33,7 @@ use wezterm_font::FontConfiguration; use wezterm_gui_subcommands::*; use wezterm_mux_server_impl::update_mux_domains; use wezterm_toast_notification::*; +use termwiz::cell::setcellwidths; mod colorease; mod commands; @@ -886,7 +887,7 @@ pub fn run_ls_fonts(config: config::ConfigHandle, cmd: &LsFontsCommand) -> anyho let unicode_version = UnicodeVersion { version: config.unicode_version, ambiguous_are_wide: config.treat_east_asian_ambiguous_width_as_wide, - cellwidths: config.cellwidths.clone(), + cellwidths: setcellwidths(config.cellwidths.clone()), }; let text = match (&cmd.text, &cmd.codepoints) {