utils::prefix_length: move current_width out of the scan loop

To prepare returning both length and width.
This commit is contained in:
Alexandre Bury 2017-01-31 16:30:53 -08:00
parent e0e96ab875
commit df835d1a10

View File

@ -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)