-
Notifications
You must be signed in to change notification settings - Fork 115
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
Add utility function ansi::slice_ansi_str
#206
Open
remi-dupre
wants to merge
6
commits into
console-rs:main
Choose a base branch
from
remi-dupre:ansi-slice
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+172
−76
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
047fea2
Add new function ansi::slice_ansi_str
remi-dupre cd1a6b4
Fix clippy lints
remi-dupre ce77cc5
Make `slice_str` similar to `truncate_str`
remi-dupre 7c68426
More verbose but more readable implementation of `slice_str`
remi-dupre 07a96f4
Remove rust >1.56 syntax
remi-dupre 51e4d4f
Fix MSRV & update changelog
remi-dupre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
# Changelog | ||
|
||
## 0.16.0 | ||
|
||
### Enhancements | ||
|
||
* Added `slice_str` util. | ||
|
||
## 0.15.8 | ||
|
||
### Enhancements | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,19 +2,15 @@ use std::borrow::Cow; | |
use std::collections::BTreeSet; | ||
use std::env; | ||
use std::fmt; | ||
use std::ops::Range; | ||
use std::sync::atomic::{AtomicBool, Ordering}; | ||
|
||
use lazy_static::lazy_static; | ||
|
||
use crate::term::{wants_emoji, Term}; | ||
|
||
#[cfg(feature = "ansi-parsing")] | ||
use crate::ansi::{strip_ansi_codes, AnsiCodeIterator}; | ||
|
||
#[cfg(not(feature = "ansi-parsing"))] | ||
fn strip_ansi_codes(s: &str) -> &str { | ||
s | ||
} | ||
use crate::ansi::AnsiCodeIterator; | ||
|
||
fn default_colors_enabled(out: &Term) -> bool { | ||
(out.features().colors_supported() | ||
|
@@ -71,7 +67,17 @@ pub fn set_colors_enabled_stderr(val: bool) { | |
|
||
/// Measure the width of a string in terminal characters. | ||
pub fn measure_text_width(s: &str) -> usize { | ||
str_width(&strip_ansi_codes(s)) | ||
#[cfg(feature = "ansi-parsing")] | ||
{ | ||
AnsiCodeIterator::new(s) | ||
.filter(|(_, is_ansi)| !is_ansi) | ||
.map(|(sub, _)| str_width(sub)) | ||
.sum() | ||
Comment on lines
+72
to
+75
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. Not related to the MR, but while implementing a less optimized draft of my fix I thought this could spare a few allocs. This is probably not something to care about so I wouldn't mind if you want to revert this for the sake of simplification. |
||
} | ||
#[cfg(not(feature = "ansi-parsing"))] | ||
{ | ||
str_width(s) | ||
} | ||
} | ||
|
||
/// A terminal color. | ||
|
@@ -732,80 +738,119 @@ fn char_width(c: char) -> usize { | |
} | ||
} | ||
|
||
/// Truncates a string to a certain number of characters. | ||
/// Slice a `&str` in terms of text width. This means that only the text | ||
/// columns strictly between `start` and `stop` will be kept. | ||
/// | ||
/// This ensures that escape codes are not screwed up in the process. | ||
/// If the maximum length is hit the string will be truncated but | ||
/// escapes code will still be honored. If truncation takes place | ||
/// the tail string will be appended. | ||
pub fn truncate_str<'a>(s: &'a str, width: usize, tail: &str) -> Cow<'a, str> { | ||
/// If a multi-columns character overlaps with the end of the interval it will | ||
/// not be included. In such a case, the result will be less than `end - start` | ||
/// columns wide. | ||
/// | ||
/// This ensures that escape codes are not screwed up in the process. And if | ||
/// non-empty head and tail are specified, they are inserted between the ANSI | ||
/// codes from truncated bounds and the slice. | ||
pub fn slice_str<'a>(s: &'a str, head: &str, bounds: Range<usize>, tail: &str) -> Cow<'a, str> { | ||
#[cfg(feature = "ansi-parsing")] | ||
{ | ||
use std::cmp::Ordering; | ||
let mut iter = AnsiCodeIterator::new(s); | ||
let mut length = 0; | ||
let mut rv = None; | ||
|
||
while let Some(item) = iter.next() { | ||
match item { | ||
(s, false) => { | ||
if rv.is_none() { | ||
if str_width(s) + length > width - str_width(tail) { | ||
let ts = iter.current_slice(); | ||
|
||
let mut s_byte = 0; | ||
let mut s_width = 0; | ||
let rest_width = width - str_width(tail) - length; | ||
for c in s.chars() { | ||
s_byte += c.len_utf8(); | ||
s_width += char_width(c); | ||
match s_width.cmp(&rest_width) { | ||
Ordering::Equal => break, | ||
Ordering::Greater => { | ||
s_byte -= c.len_utf8(); | ||
break; | ||
} | ||
Ordering::Less => continue, | ||
} | ||
} | ||
|
||
let idx = ts.len() - s.len() + s_byte; | ||
let mut buf = ts[..idx].to_string(); | ||
buf.push_str(tail); | ||
rv = Some(buf); | ||
} | ||
length += str_width(s); | ||
} | ||
} | ||
(s, true) => { | ||
if let Some(ref mut rv) = rv { | ||
rv.push_str(s); | ||
} | ||
let mut pos = 0; | ||
let mut code_iter = AnsiCodeIterator::new(s).peekable(); | ||
|
||
// Search for the begining of the slice while collecting heading ANSI | ||
// codes | ||
let mut slice_start = 0; | ||
let mut front_ansi = String::new(); | ||
|
||
while pos < bounds.start { | ||
let (sub, is_ansi) = match code_iter.peek_mut() { | ||
Some(x) => x, | ||
None => break, | ||
}; | ||
|
||
if *is_ansi { | ||
front_ansi.push_str(sub); | ||
slice_start += sub.len(); | ||
} else if let Some(c) = sub.chars().next() { | ||
// Pop the head char of `sub` while keeping `sub` on top of | ||
// the iterator | ||
pos += char_width(c); | ||
slice_start += c.len_utf8(); | ||
*sub = &sub[c.len_utf8()..]; | ||
continue; | ||
} | ||
|
||
code_iter.next(); | ||
} | ||
|
||
// Search for the end of the slice | ||
let mut slice_end = slice_start; | ||
|
||
'search_slice_end: for (sub, is_ansi) in &mut code_iter { | ||
if is_ansi { | ||
slice_end += sub.len(); | ||
continue; | ||
} | ||
|
||
for c in sub.chars() { | ||
let c_width = char_width(c); | ||
|
||
if pos + c_width > bounds.end { | ||
// We will only search for ANSI codes after breaking this | ||
// loop, so we can safely drop the remaining of `sub` | ||
break 'search_slice_end; | ||
} | ||
|
||
pos += c_width; | ||
slice_end += c.len_utf8(); | ||
} | ||
} | ||
|
||
if let Some(buf) = rv { | ||
Cow::Owned(buf) | ||
} else { | ||
Cow::Borrowed(s) | ||
// Initialise the result, no allocation may have to be performed if | ||
// both head and front are empty | ||
let slice = &s[slice_start..slice_end]; | ||
|
||
let mut result = { | ||
if front_ansi.is_empty() && head.is_empty() && tail.is_empty() { | ||
Cow::Borrowed(slice) | ||
} else { | ||
Cow::Owned(front_ansi + head + slice + tail) | ||
} | ||
}; | ||
|
||
// Push back remaining ANSI codes to result | ||
for (sub, is_ansi) in code_iter { | ||
if is_ansi { | ||
*result.to_mut() += sub; | ||
} | ||
} | ||
} | ||
|
||
result | ||
} | ||
#[cfg(not(feature = "ansi-parsing"))] | ||
{ | ||
if s.len() <= width - tail.len() { | ||
Cow::Borrowed(s) | ||
let slice = s.get(bounds).unwrap_or(""); | ||
|
||
if head.is_empty() && tail.is_empty() { | ||
Cow::Borrowed(slice) | ||
} else { | ||
Cow::Owned(format!( | ||
"{}{}", | ||
s.get(..width - tail.len()).unwrap_or_default(), | ||
tail | ||
)) | ||
Cow::Owned(format!("{}{}{}", head, slice, tail)) | ||
} | ||
} | ||
} | ||
|
||
/// Truncates a string to a certain number of characters. | ||
/// | ||
/// This ensures that escape codes are not screwed up in the process. | ||
/// If the maximum length is hit the string will be truncated but | ||
/// escapes code will still be honored. If truncation takes place | ||
/// the tail string will be appended. | ||
pub fn truncate_str<'a>(s: &'a str, width: usize, tail: &str) -> Cow<'a, str> { | ||
if measure_text_width(s) > width { | ||
let tail_width = measure_text_width(tail); | ||
slice_str(s, "", 0..width.saturating_sub(tail_width), tail) | ||
} else { | ||
Cow::Borrowed(s) | ||
} | ||
} | ||
|
||
/// Pads a string to fill a certain number of characters. | ||
/// | ||
/// This will honor ansi codes correctly and allows you to align a string | ||
|
@@ -868,15 +913,18 @@ fn test_text_width() { | |
.on_black() | ||
.bold() | ||
.force_styling(true) | ||
.to_string(); | ||
.to_string() | ||
+ "🐶bar"; | ||
assert_eq!( | ||
measure_text_width(&s), | ||
if cfg!(feature = "ansi-parsing") { | ||
3 | ||
} else if cfg!(feature = "unicode-width") { | ||
17 | ||
} else { | ||
21 | ||
match ( | ||
cfg!(feature = "ansi-parsing"), | ||
cfg!(feature = "unicode-width") | ||
) { | ||
(true, true) => 8, | ||
(true, false) => 7, | ||
(false, true) => 22, | ||
(false, false) => 25, | ||
} | ||
); | ||
} | ||
|
@@ -911,8 +959,50 @@ fn test_truncate_str() { | |
); | ||
} | ||
|
||
#[test] | ||
fn test_slice_ansi_str() { | ||
// Note that 🐶 is two columns wide | ||
let test_str = "Hello\x1b[31m🐶\x1b[1m🐶\x1b[0m world!"; | ||
assert_eq!(slice_str(test_str, "", 0..test_str.len(), ""), test_str); | ||
|
||
if cfg!(feature = "unicode-width") && cfg!(feature = "ansi-parsing") { | ||
assert_eq!(measure_text_width(test_str), 16); | ||
|
||
assert_eq!( | ||
slice_str(test_str, "", 5..5, ""), | ||
"\u{1b}[31m\u{1b}[1m\u{1b}[0m" | ||
); | ||
|
||
assert_eq!( | ||
slice_str(test_str, "", 0..5, ""), | ||
"Hello\x1b[31m\x1b[1m\x1b[0m" | ||
); | ||
|
||
assert_eq!( | ||
slice_str(test_str, "", 0..6, ""), | ||
"Hello\x1b[31m\x1b[1m\x1b[0m" | ||
); | ||
|
||
assert_eq!( | ||
slice_str(test_str, "", 0..7, ""), | ||
"Hello\x1b[31m🐶\x1b[1m\x1b[0m" | ||
); | ||
|
||
assert_eq!( | ||
slice_str(test_str, "", 4..9, ""), | ||
"o\x1b[31m🐶\x1b[1m🐶\x1b[0m" | ||
); | ||
|
||
assert_eq!( | ||
slice_str(test_str, "", 7..21, ""), | ||
"\x1b[31m\x1b[1m🐶\x1b[0m world!" | ||
); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_truncate_str_no_ansi() { | ||
assert_eq!(&truncate_str("foo bar", 7, "!"), "foo bar"); | ||
assert_eq!(&truncate_str("foo bar", 5, ""), "foo b"); | ||
assert_eq!(&truncate_str("foo bar", 5, "!"), "foo !"); | ||
assert_eq!(&truncate_str("foo bar baz", 10, "..."), "foo bar..."); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This was just a random (new?) clippy lint : these are already u16 🤷