#text #width #unicode #truncate #pad

unicode-truncate

Unicode-aware algorithm to pad or truncate str in terms of displayed width

3 unstable releases

0.2.0 Dec 17, 2020
0.1.1 Aug 15, 2019
0.1.0 Aug 15, 2019

#350 in Text processing

Download history 1322/week @ 2022-11-26 1187/week @ 2022-12-03 1153/week @ 2022-12-10 1246/week @ 2022-12-17 937/week @ 2022-12-24 819/week @ 2022-12-31 1288/week @ 2023-01-07 1405/week @ 2023-01-14 1353/week @ 2023-01-21 1913/week @ 2023-01-28 1635/week @ 2023-02-04 1846/week @ 2023-02-11 2146/week @ 2023-02-18 2937/week @ 2023-02-25 2218/week @ 2023-03-04 1661/week @ 2023-03-11

9,225 downloads per month
Used in 16 crates (13 directly)

MIT/Apache

15KB
250 lines

unicode-truncate

Unicode-aware algorithm to pad or truncate str in terms of displayed width.

crates.io Documentation Build Status

examples

Safely truncate string to display width even not at character boundaries.

use unicode_truncate::UnicodeTruncateStr;

fn main() {
    let (rv, w) = "你好吗".unicode_truncate(5);
    assert_eq!(rv, "你好");
    assert_eq!(w, 4);
}

Making sure the string is displayed in exactly number of columns by combining padding and truncating.

use unicode_truncate::UnicodeTruncateStr;
use unicode_truncate::Alignment;
use unicode_width::UnicodeWidthStr;

fn main() {
    let rv = "你好吗".unicode_pad(5, Alignment::Left, true);
    assert_eq!(rv, "你好 ");
    assert_eq!(rv.width(), 5);
}

features

unicode-truncate can be built without libstd by disabling the default feature std. However in that case unicode_truncate::UnicodeTruncateStr::unicode_pad won't be available because it depends on std::string::String and std::borrow::Cow.

Dependencies

~62KB