Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved from unicode-width to unicode-display-width for visual grapheme width estimation #210

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "console"
description = "A terminal and console abstraction for Rust"
version = "0.15.8"
version = "0.16.0"
keywords = ["cli", "terminal", "colors", "console", "ansi"]
authors = ["Armin Ronacher <armin.ronacher@active-4.com>"]
license = "MIT"
Expand All @@ -13,13 +13,13 @@ readme = "README.md"
rust-version = "1.56.0"

[features]
default = ["unicode-width", "ansi-parsing"]
default = ["unicode-display-width", "ansi-parsing"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use a feature named unicode-width that then enables ["dep:unicode-display-width"] this might be backwards incompatible?

windows-console-colors = ["ansi-parsing"]
ansi-parsing = []

[dependencies]
libc = "0.2.99"
unicode-width = { version = "0.1", optional = true }
unicode-display-width = { version = "0.3", optional = true }
lazy_static = "1.4.0"

[target.'cfg(windows)'.dependencies]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ are useful for more complex formatting.

## Unicode Width Support

By default this crate depends on the `unicode-width` crate to calculate
By default this crate depends on the `unicode-display-width` crate to calculate
the width of terminal characters. If you do not need this you can disable
the `unicode-width` feature which will cut down on dependencies.
the `unicode-display-width` feature which will cut down on dependencies.

License: MIT
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@
//!
//! # Unicode Width Support
//!
//! By default this crate depends on the `unicode-width` crate to calculate
//! By default this crate depends on the `unicode-display-width` crate to calculate
//! the width of terminal characters. If you do not need this you can disable
//! the `unicode-width` feature which will cut down on dependencies.
//! the `unicode-display-width` feature which will cut down on dependencies.
//!
//! # Features
//!
//! By default all features are enabled. The following features exist:
//!
//! * `unicode-width`: adds support for unicode width calculations
//! * `unicode-display-width`: adds support for unicode width calculations
//! * `ansi-parsing`: adds support for parsing ansi codes (this adds support
//! for stripping and taking ansi escape codes into account for length
//! calculations).
Expand Down
39 changes: 28 additions & 11 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,25 +707,42 @@ impl<'a, 'b> fmt::Display for Emoji<'a, 'b> {
}

fn str_width(s: &str) -> usize {
#[cfg(feature = "unicode-width")]
#[cfg(feature = "unicode-display-width")]
{
use unicode_width::UnicodeWidthStr;
s.width()
unicode_display_width::width(s) as usize
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this usize cast is here to avoid changing APIs. It may not be what is wanted?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems okay to me. Would sure be nice to avoid breaking compatibility for this.

}
#[cfg(not(feature = "unicode-width"))]
#[cfg(not(feature = "unicode-display-width"))]
{
s.chars().count()
}
}

#[cfg(feature = "ansi-parsing")]
fn char_width(c: char) -> usize {
#[cfg(feature = "unicode-width")]
{
use unicode_width::UnicodeWidthChar;
c.width().unwrap_or(0)
#[cfg(feature = "unicode-display-width")]
if c < '\u{7F}' {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you go this route? I'd like to avoid referring to explicit code points as much as possible as it would be likely for these to fall out of sync for Unicode updates -- that's what we're trying to rely on other crates for.

if c >= '\u{20}' {
// U+0020 to U+007F (exclusive) are single-width ASCII codepoints
1
} else if c == '\0' {
// U+0000 *is* a control code, but it's special-cased
0
} else {
// U+0001 to U+0020 (exclusive) are control codes
0
}
} else if c >= '\u{A0}' {
// No characters >= U+00A0 are control codes, so we can consult the lookup tables
if unicode_display_width::is_double_width(c) {
2
} else {
1
}
} else {
// U+007F to U+00A0 (exclusive) are control codes
0
}
#[cfg(not(feature = "unicode-width"))]
#[cfg(not(feature = "unicode-display-width"))]
{
let _c = c;
1
Expand Down Expand Up @@ -873,7 +890,7 @@ fn test_text_width() {
measure_text_width(&s),
if cfg!(feature = "ansi-parsing") {
3
} else if cfg!(feature = "unicode-width") {
} else if cfg!(feature = "unicode-display-width") {
17
} else {
21
Expand All @@ -882,7 +899,7 @@ fn test_text_width() {
}

#[test]
#[cfg(all(feature = "unicode-width", feature = "ansi-parsing"))]
#[cfg(all(feature = "unicode-display-width", feature = "ansi-parsing"))]
fn test_truncate_str() {
let s = format!("foo {}", style("bar").red().force_styling(true));
assert_eq!(
Expand Down