mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-24 01:46:31 +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, "");
|
/// 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>
|
where I: Iterator<Item = &'a str>
|
||||||
{
|
{
|
||||||
let delimiter_width = delimiter.width();
|
let delimiter_width = delimiter.width();
|
||||||
let delimiter_len = delimiter.len();
|
let delimiter_len = delimiter.len();
|
||||||
|
|
||||||
let sum = iter.scan(0, |w, token| {
|
let mut current_width = 0;
|
||||||
*w += token.width();
|
let sum = iter.take_while(|token| {
|
||||||
if *w > width {
|
let width = token.width();
|
||||||
None
|
if current_width + width > available_width {
|
||||||
|
false
|
||||||
} else {
|
} else {
|
||||||
// Add a space
|
if current_width != 0 {
|
||||||
*w += delimiter_width;
|
current_width += delimiter_width;
|
||||||
Some(token)
|
}
|
||||||
|
current_width += width;
|
||||||
|
true
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.map(|token| token.len() + delimiter_len)
|
.map(|token| token.len() + delimiter_len)
|
||||||
|
Loading…
Reference in New Issue
Block a user