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
- Configuration Object: Provides a configuration object
that is easy to pass around
for various different needs.
- The tab size and whether to expand them to spaces or not.
- The size of control characters.
- Whether to use alternate width calculation more suited for CJK contexts or not. It controls East Asian Ambiguous characters (such as Greek, Cyrillic, and some symbol characters) to be 1 or 2 columns wide.
- Also support the
UNICODE_WIDTH=cjkenvironment variable to initialize the default CJK mode. This enables end users in CJK contexts to change the default mode to match their environments.
- Safe Truncation: Truncate strings to a specific column width without breaking UTF-8 characters, including optional tab support.
- Line Wrapping: Wrap strings to multiple lines at a specific column.
Installation
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;
Line Wrapping
use UnicodeWidth;
Please see the documentation for more details, and releases for the change history.
License
Licensed under the Apache License, Version 2.0.