From 5b8b9630a33f33dd5cbc7e1856b3a08ed1ebb2e9 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 19 Apr 2022 06:55:43 -0700 Subject: [PATCH] Add treat_east_asian_ambiguous_width_as_wide option I've bundled this into termwiz's UnicodeVersion type as that is a similar concept that is already routed through to the appropriate function. refs: https://github.com/wez/wezterm/issues/1888 --- config/src/config.rs | 3 +++ config/src/terminal.rs | 9 +++++++-- docs/changelog.md | 1 + .../treat_east_asian_ambiguous_width_as_wide.md | 17 +++++++++++++++++ term/src/config.rs | 8 ++++++-- term/src/terminalstate/mod.rs | 2 +- term/src/terminalstate/performer.rs | 6 +++--- termwiz/src/cell.rs | 16 +++++++++++++--- 8 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 docs/config/lua/config/treat_east_asian_ambiguous_width_as_wide.md diff --git a/config/src/config.rs b/config/src/config.rs index cd0778169..be7069d2d 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -595,6 +595,9 @@ pub struct Config { #[serde(default = "default_unicode_version")] pub unicode_version: u8, + #[serde(default)] + pub treat_east_asian_ambiguous_width_as_wide: bool, + #[serde(default = "default_true")] pub allow_download_protocols: bool, diff --git a/config/src/terminal.rs b/config/src/terminal.rs index e0425d6a2..815876774 100644 --- a/config/src/terminal.rs +++ b/config/src/terminal.rs @@ -2,6 +2,7 @@ use crate::{configuration, ConfigHandle, NewlineCanon}; use std::sync::Mutex; +use termwiz::cell::UnicodeVersion; use wezterm_term::color::ColorPalette; use wezterm_term::config::BidiMode; @@ -80,8 +81,12 @@ impl wezterm_term::TerminalConfiguration for TermConfig { } } - fn unicode_version(&self) -> u8 { - self.configuration().unicode_version + fn unicode_version(&self) -> UnicodeVersion { + let config = self.configuration(); + UnicodeVersion { + version: config.unicode_version, + ambiguous_are_wide: config.treat_east_asian_ambiguous_width_as_wide, + } } fn debug_key_events(&self) -> bool { diff --git a/docs/changelog.md b/docs/changelog.md index 0fc83b9e8..228a2cf8d 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -19,6 +19,7 @@ As features stabilize some brief notes about them will accumulate here. * [AttachDomain](config/lua/keyassignment/AttachDomain.md) and [DetachDomain](config/lua/keyassignment/DetachDomain.md) key assignments * Specifying a domain name in a [SpawnCommand](config/lua/SpawnCommand.md) will cause that domain to be attached if it is in the detached state. This is useful when combined with [SwitchToWorkspace](config/lua/keyassignment/SwitchToWorkspace.md). * X11: wezterm now sets `_NET_WM_NAME` in addition to `WM_NAME` for clients that don't know how to fallback +* [treat_east_asian_ambiguous_width_as_wide](config/lua/config/treat_east_asian_ambiguous_width_as_wide.md) for control how ambiguous width characters are resolved. [#1888](https://github.com/wez/wezterm/issues/1888) #### Changed * Debian packages now register wezterm as an alternative for `x-terminal-emulator`. Thanks to [@xpufx](https://github.com/xpufx)! [#1883](https://github.com/wez/wezterm/pull/1883) diff --git a/docs/config/lua/config/treat_east_asian_ambiguous_width_as_wide.md b/docs/config/lua/config/treat_east_asian_ambiguous_width_as_wide.md new file mode 100644 index 000000000..8cbcdec37 --- /dev/null +++ b/docs/config/lua/config/treat_east_asian_ambiguous_width_as_wide.md @@ -0,0 +1,17 @@ +# `treat_east_asian_ambiguous_width_as_wide = fasel` + +*Since: nightly builds only* + +Unicode defines a number of codepoints as having [Ambiguous +Width](http://www.unicode.org/reports/tr11/#Ambiguous). These are characters +whose width resolves differently according to context that is typically absent +from the monospaced world of the terminal. + +WezTerm will by default treat ambiguous width as occupying a single cell. + +When `treat_east_asian_ambiguous_width_as_wide = true` WezTerm will treat them +as being two cells wide. + +Note that changing this setting may have consequences for layout in text UI +applications if their expectation of width differs from your choice of +configuration. diff --git a/term/src/config.rs b/term/src/config.rs index d6e451f9e..d8c701556 100644 --- a/term/src/config.rs +++ b/term/src/config.rs @@ -1,4 +1,5 @@ use crate::color::ColorPalette; +use termwiz::cell::UnicodeVersion; use termwiz::surface::{Line, SequenceNo}; use wezterm_bidi::ParagraphDirectionHint; @@ -181,8 +182,11 @@ pub trait TerminalConfiguration: std::fmt::Debug { /// that also alter the width of certain sequences, and that is too /// new for most deployed applications. // Coupled with config/src/lib.rs:default_unicode_version - fn unicode_version(&self) -> u8 { - 9 + fn unicode_version(&self) -> UnicodeVersion { + UnicodeVersion { + version: 9, + ambiguous_are_wide: false, + } } fn debug_key_events(&self) -> bool { diff --git a/term/src/terminalstate/mod.rs b/term/src/terminalstate/mod.rs index f37088412..1bb898372 100644 --- a/term/src/terminalstate/mod.rs +++ b/term/src/terminalstate/mod.rs @@ -497,7 +497,7 @@ impl TerminalState { let color_map = default_color_map(); - let unicode_version = UnicodeVersion(config.unicode_version()); + let unicode_version = config.unicode_version(); TerminalState { config, diff --git a/term/src/terminalstate/performer.rs b/term/src/terminalstate/performer.rs index f9dfdc149..a01fb4c28 100644 --- a/term/src/terminalstate/performer.rs +++ b/term/src/terminalstate/performer.rs @@ -7,7 +7,7 @@ use log::{debug, error}; use num_traits::FromPrimitive; use std::fmt::Write; use std::ops::{Deref, DerefMut}; -use termwiz::cell::{grapheme_column_width, Cell, CellAttributes, SemanticType, UnicodeVersion}; +use termwiz::cell::{grapheme_column_width, Cell, CellAttributes, SemanticType}; use termwiz::escape::csi::{CharacterPath, EraseInDisplay}; use termwiz::escape::osc::{ ChangeColorPair, ColorOrQuery, FinalTermSemanticPrompt, ITermProprietary, @@ -559,7 +559,7 @@ impl<'a> Performer<'a> { self.palette.take(); self.top_and_bottom_margins = 0..self.screen().physical_rows as VisibleRowIndex; self.left_and_right_margins = 0..self.screen().physical_cols; - self.unicode_version = UnicodeVersion(self.config.unicode_version()); + self.unicode_version = self.config.unicode_version(); self.unicode_version_stack.clear(); self.suppress_initial_title_change = false; self.accumulating_title.take(); @@ -638,7 +638,7 @@ impl<'a> Performer<'a> { } } ITermProprietary::UnicodeVersion(ITermUnicodeVersionOp::Set(n)) => { - self.unicode_version = UnicodeVersion(n); + self.unicode_version.version = n; } ITermProprietary::UnicodeVersion(ITermUnicodeVersionOp::Push(label)) => { let vers = self.unicode_version; diff --git a/termwiz/src/cell.rs b/termwiz/src/cell.rs index 14807a893..25b379c2e 100644 --- a/termwiz/src/cell.rs +++ b/termwiz/src/cell.rs @@ -829,9 +829,15 @@ impl Cell { } #[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub struct UnicodeVersion(pub u8); +pub struct UnicodeVersion { + pub version: u8, + pub ambiguous_are_wide: bool, +} -pub const LATEST_UNICODE_VERSION: UnicodeVersion = UnicodeVersion(14); +pub const LATEST_UNICODE_VERSION: UnicodeVersion = UnicodeVersion { + version: 14, + ambiguous_are_wide: false, +}; /// Returns the number of cells visually occupied by a sequence /// of graphemes. @@ -875,7 +881,9 @@ pub fn unicode_column_width(s: &str, version: Option) -> usize { /// the Cell that is used to hold a grapheme, and that per-Cell version /// can then be used to calculate width. pub fn grapheme_column_width(s: &str, version: Option) -> usize { - let version = version.unwrap_or(LATEST_UNICODE_VERSION).0; + let version = version.unwrap_or(LATEST_UNICODE_VERSION); + let ambiguous_are_wide = version.ambiguous_are_wide; + let version = version.version; let width: usize = s .chars() @@ -887,6 +895,8 @@ pub fn grapheme_column_width(s: &str, version: Option) -> usize // if c == WcWidth::Unassigned { 1 + } else if c == WcWidth::Ambiguous && ambiguous_are_wide { + 2 } else if version >= 9 { c.width_unicode_9_or_later() } else {