Skip to content

Commit

Permalink
wez#6289 more efficient version, converting from a list of codepoints…
Browse files Browse the repository at this point in the history
… to a hashmap
  • Loading branch information
hamano committed Oct 18, 2024
1 parent a96cf37 commit 38aa08b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
3 changes: 2 additions & 1 deletion config/src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()),
}
}

Expand Down
23 changes: 18 additions & 5 deletions termwiz/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -888,11 +889,25 @@ pub struct CellWidth {
pub width: u8,
}

pub fn setcellwidths(cellwidths: Option<Vec<CellWidth>>) -> Option<HashMap<u32, u8>> {
if let Some(ref cellwidths) = cellwidths {
let mut map: HashMap<u32, u8> = 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<Vec<CellWidth>>,
pub cellwidths: Option<HashMap<u32, u8>>,
}

impl UnicodeVersion {
Expand Down Expand Up @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion wezterm-gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 38aa08b

Please sign in to comment.