-
Notifications
You must be signed in to change notification settings - Fork 107
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -707,25 +707,26 @@ 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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")] | ||
#[cfg(feature = "unicode-display-width")] | ||
{ | ||
use unicode_width::UnicodeWidthChar; | ||
c.width().unwrap_or(0) | ||
match unicode_display_width::is_double_width(c) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For compatibility, maybe we should replicate the logic from match c.is_control() {
true if c == '\x00' => ?,
true => 0,
false => match unicode_display_width::is_double_width(c) {
true => 2,
false => 1,
}
} |
||
true => 2, | ||
false => 1, | ||
} | ||
} | ||
#[cfg(not(feature = "unicode-width"))] | ||
#[cfg(not(feature = "unicode-display-width"))] | ||
{ | ||
let _c = c; | ||
1 | ||
|
@@ -873,7 +874,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 | ||
|
@@ -882,7 +883,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!( | ||
|
There was a problem hiding this comment.
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?