2018-01-02 22:26:04 +00:00
|
|
|
use ui_model::{UiModel, Attrs};
|
|
|
|
|
|
|
|
pub struct ModelLayout {
|
|
|
|
pub model: UiModel,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ModelLayout {
|
|
|
|
const COLUMNS_STEP: u64 = 50;
|
|
|
|
const ROWS_STEP: u64 = 10;
|
|
|
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
ModelLayout { model: UiModel::new(ModelLayout::ROWS_STEP, ModelLayout::COLUMNS_STEP) }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wrap all lines into model
|
2018-01-03 09:04:01 +00:00
|
|
|
///
|
|
|
|
/// returns actual width
|
2018-01-08 20:14:17 +00:00
|
|
|
pub fn layout(
|
|
|
|
&mut self,
|
|
|
|
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 {
|
2018-01-03 09:04:01 +00:00
|
|
|
let model_cols = ((max_columns / ModelLayout::COLUMNS_STEP) + 1) *
|
2018-01-02 22:26:04 +00:00
|
|
|
ModelLayout::COLUMNS_STEP;
|
2018-01-08 20:14:17 +00:00
|
|
|
|
|
|
|
let model_rows = ((rows as u64 / ModelLayout::ROWS_STEP) + 1) * ModelLayout::ROWS_STEP;
|
2018-01-02 22:26:04 +00:00
|
|
|
|
|
|
|
self.model = UiModel::new(model_rows, model_cols);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-03 09:04:01 +00:00
|
|
|
let mut max_col_idx = 0;
|
2018-01-02 22:26:04 +00:00
|
|
|
let mut col_idx = 0;
|
|
|
|
let mut row_idx = 0;
|
|
|
|
for content in lines {
|
|
|
|
for (attr, ch_list) in content {
|
|
|
|
for ch in ch_list {
|
2018-01-03 09:04:01 +00:00
|
|
|
if col_idx >= max_columns {
|
2018-01-02 22:26:04 +00:00
|
|
|
col_idx = 0;
|
|
|
|
row_idx += 1;
|
2018-01-08 20:14:17 +00:00
|
|
|
} else {
|
|
|
|
col_idx += 1;
|
2018-01-02 22:26:04 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 09:04:01 +00:00
|
|
|
if max_col_idx < col_idx {
|
|
|
|
max_col_idx = col_idx;
|
|
|
|
}
|
|
|
|
|
2018-01-02 22:26:04 +00:00
|
|
|
self.model.set_cursor(row_idx, col_idx as usize);
|
|
|
|
self.model.put(ch, false, attr.as_ref());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
row_idx += 1;
|
|
|
|
}
|
2018-01-03 09:04:01 +00:00
|
|
|
|
2018-01-08 20:14:17 +00:00
|
|
|
(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);
|
2018-01-02 22:26:04 +00:00
|
|
|
}
|
|
|
|
}
|