https://github.com/Aetf/unicode-truncate
  
  
    Rust library for unicode-aware algorithm to pad or truncate `str` in terms of displayed width. 
    https://github.com/Aetf/unicode-truncate
  
padding rust truncate unicode width
        Last synced: 7 months ago 
        JSON representation
    
Rust library for unicode-aware algorithm to pad or truncate `str` in terms of displayed width.
- Host: GitHub
- URL: https://github.com/Aetf/unicode-truncate
- Owner: Aetf
- License: other
- Created: 2019-08-15T01:24:50.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-12-31T12:55:06.000Z (10 months ago)
- Last Synced: 2025-03-01T23:44:32.549Z (8 months ago)
- Topics: padding, rust, truncate, unicode, width
- Language: Rust
- Homepage:
- Size: 78.1 KB
- Stars: 17
- Watchers: 5
- Forks: 5
- Open Issues: 2
- 
            Metadata Files:
            - Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
 
Awesome Lists containing this project
README
          # unicode-truncate
Unicode-aware algorithm to pad or truncate `str` in terms of displayed width.
[](https://crates.io/crates/unicode-truncate)
[](https://docs.rs/unicode-truncate)
[](https://github.com/Aetf/unicode-truncate/actions)
## Examples
Safely truncate string to display width even not at character boundaries.
```rust
use unicode_truncate::UnicodeTruncateStr;
fn main() {
    assert_eq!("你好吗".unicode_truncate(5), ("你好", 4));
}
```
Making sure the string is displayed in exactly number of columns by combining padding and
truncating.
```rust
use unicode_truncate::UnicodeTruncateStr;
use unicode_truncate::Alignment;
use unicode_width::UnicodeWidthStr;
fn main() {
    let str = "你好吗".unicode_pad(5, Alignment::Left, true);
    assert_eq!(str, "你好 ");
    assert_eq!(str.width(), 5);
}
```
## Features
`unicode-truncate` can be built without `std` 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`.