mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
utils::prefix_length: move current_width
out of the scan loop
To prepare returning both length and width.
This commit is contained in:
parent
e0e96ab875
commit
df835d1a10
@ -34,20 +34,23 @@ pub use self::reader::ProgressReader;
|
||||
/// prefix_length(my_text.graphemes(true), 5, "");
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn prefix_length<'a, I>(iter: I, width: usize, delimiter: &str) -> usize
|
||||
pub fn prefix_length<'a, I>(iter: I, available_width: usize, delimiter: &str) -> usize
|
||||
where I: Iterator<Item = &'a str>
|
||||
{
|
||||
let delimiter_width = delimiter.width();
|
||||
let delimiter_len = delimiter.len();
|
||||
|
||||
let sum = iter.scan(0, |w, token| {
|
||||
*w += token.width();
|
||||
if *w > width {
|
||||
None
|
||||
let mut current_width = 0;
|
||||
let sum = iter.take_while(|token| {
|
||||
let width = token.width();
|
||||
if current_width + width > available_width {
|
||||
false
|
||||
} else {
|
||||
// Add a space
|
||||
*w += delimiter_width;
|
||||
Some(token)
|
||||
if current_width != 0 {
|
||||
current_width += delimiter_width;
|
||||
}
|
||||
current_width += width;
|
||||
true
|
||||
}
|
||||
})
|
||||
.map(|token| token.len() + delimiter_len)
|
||||
|
Loading…
Reference in New Issue
Block a user