pub struct UnicodeWidth { /* private fields */ }Expand description
A configuration helper to measure character and string widths.
It determines the width of Unicode characters and strings, optionally treating East Asian Ambiguous characters (such as certain Greek, Cyrillic, and CJK characters) as having a width of 2 (CJK mode).
The default CJK mode is initialized at startup
based on the UNICODE_WIDTH environment variable,
but can also be dynamically modified using UnicodeWidth::set_default_cjk.
Implementations§
Source§impl UnicodeWidth
impl UnicodeWidth
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new UnicodeWidth instance using the default CJK configuration.
§Examples
use unicode_width_utils::UnicodeWidth;
let uw = UnicodeWidth::new();Sourcepub fn with_cjk(is_cjk: bool) -> Self
pub fn with_cjk(is_cjk: bool) -> Self
Create a new UnicodeWidth instance with a specific CJK flag.
If is_cjk is true,
East Asian Ambiguous characters will be treated as 2 columns wide.
If false, they will be treated as 1 column wide.
§Examples
use unicode_width_utils::UnicodeWidth;
let non_cjk = UnicodeWidth::with_cjk(false);
assert_eq!(non_cjk.char('█'), Some(1));
let cjk = UnicodeWidth::with_cjk(true);
assert_eq!(cjk.char('█'), Some(2));Sourcepub fn set_default_cjk(is_cjk: bool)
pub fn set_default_cjk(is_cjk: bool)
Set the default CJK configuration dynamically.
Future instances
created using UnicodeWidth::new or UnicodeWidth::default
will inherit this default value
unless explicitly overridden with UnicodeWidth::with_cjk.
§Examples
use unicode_width_utils::UnicodeWidth;
// Set default CJK mode to true
UnicodeWidth::set_default_cjk(true);
let uw = UnicodeWidth::new();
assert_eq!(uw.char('█'), Some(2));
// Set default CJK mode to false
UnicodeWidth::set_default_cjk(false);
let uw2 = UnicodeWidth::new();
assert_eq!(uw2.char('█'), Some(1));Sourcepub fn char(&self, ch: char) -> Option<usize>
pub fn char(&self, ch: char) -> Option<usize>
Return the column width of a character.
Return None for control characters
or other characters without a defined width.
This is a wrapper of UnicodeWidthChar,
calls width or width_cjk depending on the configuration.
§Examples
use unicode_width_utils::UnicodeWidth;
let uw = UnicodeWidth::with_cjk(false);
assert_eq!(uw.char('A'), Some(1));
assert_eq!(uw.char('\n'), None);Sourcepub fn str(&self, str: &str) -> usize
pub fn str(&self, str: &str) -> usize
Return the total column width of a string.
This is a wrapper of UnicodeWidthStr,
calls width or width_cjk depending on the configuration.
§Examples
use unicode_width_utils::UnicodeWidth;
let uw = UnicodeWidth::with_cjk(false);
assert_eq!(uw.str("Hello"), 5);Sourcepub fn truncate<'a>(&self, str: &'a str, max_width: usize) -> &'a str
pub fn truncate<'a>(&self, str: &'a str, max_width: usize) -> &'a str
Truncate a string slice to a maximum column width.
The returned slice will be the longest prefix of str
whose total column width does not exceed max_width.
§Examples
use unicode_width_utils::UnicodeWidth;
let uw = UnicodeWidth::with_cjk(false);
assert_eq!(uw.truncate("hello", 3), "hel");
// Truncating CJK text (each 'あ' is 2 columns wide)
let cjk = UnicodeWidth::with_cjk(true);
assert_eq!(cjk.truncate("あああ", 3), "あ");
assert_eq!(cjk.truncate("A█B", 2), "A");Trait Implementations§
Source§impl Clone for UnicodeWidth
impl Clone for UnicodeWidth
Source§fn clone(&self) -> UnicodeWidth
fn clone(&self) -> UnicodeWidth
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more