unicode-width-utils
A thin-wrapper for the unicode-width crate with additional functionalities,
such as dynamic configuration for CJK (East Asian Ambiguous) widths and
safe string truncation.
Features
- Dynamic CJK Config: Treat East Asian Ambiguous characters (such as Greek, Cyrillic, and CJK characters) as either 1 or 2 columns wide.
- Environment Variable Override: Initialize the default CJK mode using
UNICODE_WIDTH=cjk. - Thread-safe Global Default: Change the global CJK default at runtime.
- Safe Truncation: Truncate strings to a specific column width without breaking UTF-8 characters.
Installation
Add this to your Cargo.toml:
[]
= "0.1.1"
Or run:
Usage
Basic Example
use UnicodeWidth;
CJK Ambiguous Widths
You can explicitly configure whether East Asian Ambiguous characters are treated as 1 or 2 columns wide:
use UnicodeWidth;
Global Default Configuration
You can change the default configuration
for future instances created via UnicodeWidth::new():
use UnicodeWidth;
Alternatively, you can initialize the default CJK mode via the environment variable:
UNICODE_WIDTH=cjk
String Truncation
Truncate a string slice so that its total display width does not exceed a maximum limit:
use UnicodeWidth;
License
Licensed under the Apache License, Version 2.0.