Fix wrapping calculation

This commit is contained in:
daa 2018-01-08 23:14:17 +03:00
parent 02cf3be95e
commit 0191579f4d

View File

@ -15,13 +15,18 @@ impl ModelLayout {
/// Wrap all lines into model /// Wrap all lines into model
/// ///
/// returns actual width /// returns actual width
pub fn layout(&mut self, lines: Vec<Vec<(Option<Attrs>, Vec<char>)>>, max_columns: u64) -> (u64, u64) { pub fn layout(
//FIXME: lines.len is not real lines count &mut self,
if lines.len() > self.model.rows || max_columns as usize > self.model.columns { lines: Vec<Vec<(Option<Attrs>, Vec<char>)>>,
max_columns: u64,
) -> (u64, u64) {
let rows = ModelLayout::count_lines(&lines, max_columns);
if rows as usize > self.model.rows || max_columns as usize > self.model.columns {
let model_cols = ((max_columns / ModelLayout::COLUMNS_STEP) + 1) * let model_cols = ((max_columns / ModelLayout::COLUMNS_STEP) + 1) *
ModelLayout::COLUMNS_STEP; ModelLayout::COLUMNS_STEP;
let model_rows = ((lines.len() as u64 / ModelLayout::ROWS_STEP) + 1) *
ModelLayout::ROWS_STEP; let model_rows = ((rows as u64 / ModelLayout::ROWS_STEP) + 1) * ModelLayout::ROWS_STEP;
self.model = UiModel::new(model_rows, model_cols); self.model = UiModel::new(model_rows, model_cols);
} }
@ -36,6 +41,8 @@ impl ModelLayout {
if col_idx >= max_columns { if col_idx >= max_columns {
col_idx = 0; col_idx = 0;
row_idx += 1; row_idx += 1;
} else {
col_idx += 1;
} }
if max_col_idx < col_idx { if max_col_idx < col_idx {
@ -44,12 +51,35 @@ impl ModelLayout {
self.model.set_cursor(row_idx, col_idx as usize); self.model.set_cursor(row_idx, col_idx as usize);
self.model.put(ch, false, attr.as_ref()); self.model.put(ch, false, attr.as_ref());
col_idx += 1;
} }
} }
row_idx += 1; row_idx += 1;
} }
(max_col_idx + 1, row_idx as u64) (max_col_idx + 1, rows)
}
fn count_lines(lines: &Vec<Vec<(Option<Attrs>, Vec<char>)>>, max_columns: u64) -> u64 {
let mut row_count = 0;
for line in lines {
let len: usize = line.iter().map(|c| c.1.len()).sum();
row_count += len as u64 / (max_columns + 1) + 1;
}
row_count
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_count_lines() {
let lines = vec![vec![(None, vec!['a'; 5])]];
let rows = ModelLayout::count_lines(&lines, 4);
assert_eq!(2, rows);
} }
} }