Skip to main content

unicode_width_utils/
lib.rs

1//! A thin-wrapper for the [`unicode-width` crate]
2//! with additional functionalities.
3//!
4//! This crate provides [`UnicodeWidth`]
5//! which can dynamically measure character and string widths
6//! based on whether they should be treated as CJK (East Asian Ambiguous) widths.
7//!
8//! It also provides a helper method to truncate strings to a maximum display width.
9//!
10//! # Examples
11//! ```
12//! use unicode_width_utils::UnicodeWidth;
13//!
14//! // Create an instance with the default CJK setting.
15//! let uw = UnicodeWidth::new();
16//! assert_eq!(uw.char('A'), 1);
17//!
18//! // Explicitly specify CJK behavior.
19//! let non_cjk = UnicodeWidth::with_cjk(false);
20//! let cjk = UnicodeWidth::with_cjk(true);
21//!
22//! // Ambiguous CJK characters (like '█') have width 1 or 2.
23//! assert_eq!(non_cjk.char('█'), 1);
24//! assert_eq!(cjk.char('█'), 2);
25//!
26//! // Truncate a string to a maximum width of columns.
27//! let truncated = cjk.truncate("A█B", 2);
28//! assert_eq!(truncated, "A");
29//! ```
30//!
31//! [`unicode-width` crate]: https://crates.io/crates/unicode-width
32
33mod line_iterator;
34mod unicode_width_utils;
35mod width_iterator;
36
37pub use line_iterator::LineIterator;
38pub use unicode_width_utils::UnicodeWidth;
39pub(crate) use width_iterator::WidthIterator;