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
|
|
|
|
pub fn layout(&mut self, lines: Vec<Vec<(Option<Attrs>, Vec<char>)>>, max_columns: u64) -> (u64, u64) {
|
2018-01-03 20:20:22 +00:00
|
|
|
//FIXME: lines.len is not real lines count
|
2018-01-03 09:04:01 +00:00
|
|
|
if lines.len() > self.model.rows || max_columns as usize > self.model.columns {
|
|
|
|
let model_cols = ((max_columns / ModelLayout::COLUMNS_STEP) + 1) *
|
2018-01-02 22:26:04 +00:00
|
|
|
ModelLayout::COLUMNS_STEP;
|
|
|
|
let model_rows = ((lines.len() as u64 / ModelLayout::ROWS_STEP) + 1) *
|
|
|
|
ModelLayout::ROWS_STEP;
|
|
|
|
|
|
|
|
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-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());
|
|
|
|
col_idx += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
row_idx += 1;
|
|
|
|
}
|
2018-01-03 09:04:01 +00:00
|
|
|
|
|
|
|
(max_col_idx + 1, row_idx as u64)
|
2018-01-02 22:26:04 +00:00
|
|
|
}
|
|
|
|
}
|