neovim-gtk/src/ui_model/model_layout.rs

174 lines
4.8 KiB
Rust
Raw Normal View History

use std::cmp::max;
2018-01-13 18:32:21 +00:00
use ui_model::{Attrs, UiModel};
2018-01-02 22:26:04 +00:00
pub struct ModelLayout {
pub model: UiModel,
2018-01-13 18:32:21 +00:00
rows_filled: usize,
cols_filled: usize,
2018-01-02 22:26:04 +00:00
}
impl ModelLayout {
2018-01-13 18:32:21 +00:00
const ROWS_STEP: usize = 10;
2018-01-02 22:26:04 +00:00
pub fn new(columns: u64) -> Self {
2018-01-13 18:32:21 +00:00
ModelLayout {
model: UiModel::new(ModelLayout::ROWS_STEP as u64, columns),
2018-01-13 18:32:21 +00:00
rows_filled: 0,
cols_filled: 0,
2018-01-13 18:32:21 +00:00
}
}
pub fn layout_append(&mut self, lines: &Vec<Vec<(Option<Attrs>, Vec<char>)>>) {
let rows_filled = self.rows_filled;
self.layout_replace(rows_filled, lines);
}
pub fn layout(&mut self, lines: &Vec<Vec<(Option<Attrs>, Vec<char>)>>) {
self.layout_replace(0, lines);
}
pub fn set_cursor(&mut self, col: usize) {
let row = if self.rows_filled > 0 {
self.rows_filled - 1
} else {
0
};
self.model.set_cursor(row, col);
}
pub fn size(&self) -> (usize, usize) {
(
max(self.cols_filled, self.model.get_cursor().1 + 1),
self.rows_filled,
)
}
2018-01-13 18:32:21 +00:00
fn check_model_size(&mut self, rows: usize) {
if rows > self.model.rows {
let model_cols = self.model.columns;
let model_rows = ((rows / (ModelLayout::ROWS_STEP + 1)) + 1) * ModelLayout::ROWS_STEP;
let (cur_row, cur_col) = self.model.get_cursor();
2018-01-13 18:32:21 +00:00
let mut model = UiModel::new(model_rows as u64, model_cols as u64);
self.model.copy_rows(&mut model, self.rows_filled - 1);
model.set_cursor(cur_row, cur_col);
self.model = model;
2018-01-13 18:32:21 +00:00
}
2018-01-02 22:26:04 +00:00
}
pub fn insert(&mut self, c: &str, shift: bool) {
if c.is_empty() {
return;
}
if shift {
//TODO: insert special char
} else {
self.model.put(c.chars().next().unwrap(), false, None);
}
}
2018-01-02 22:26:04 +00:00
/// Wrap all lines into model
2018-01-03 09:04:01 +00:00
///
/// returns actual width
fn layout_replace(&mut self, row_offset: usize, lines: &Vec<Vec<(Option<Attrs>, Vec<char>)>>) {
let rows = ModelLayout::count_lines(&lines, self.model.columns);
2018-01-08 20:14:17 +00:00
self.check_model_size(rows + row_offset);
self.rows_filled = rows + row_offset;
2018-01-02 22:26:04 +00:00
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 = row_offset;
2018-01-02 22:26:04 +00:00
for content in lines {
for &(ref attr, ref ch_list) in content {
2018-01-02 22:26:04 +00:00
for ch in ch_list {
2018-01-29 19:30:55 +00:00
if col_idx >= self.model.columns {
col_idx = 0;
row_idx += 1;
}
self.model.set_cursor(row_idx, col_idx as usize);
self.model.put(*ch, false, attr.as_ref());
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;
}
col_idx += 1;
2018-01-02 22:26:04 +00:00
}
if col_idx < self.model.columns {
self.model.model[row_idx].clear(col_idx, self.model.columns - 1);
}
2018-01-02 22:26:04 +00:00
}
row_idx += 1;
}
2018-01-03 09:04:01 +00:00
2018-02-27 19:24:17 +00:00
if self.rows_filled == 1 {
self.cols_filled = max_col_idx + 1;
} else {
self.cols_filled = max(self.cols_filled, max_col_idx + 1);
}
2018-01-08 20:14:17 +00:00
}
2018-01-13 18:32:21 +00:00
fn count_lines(lines: &Vec<Vec<(Option<Attrs>, Vec<char>)>>, max_columns: usize) -> usize {
2018-01-08 20:14:17 +00:00
let mut row_count = 0;
for line in lines {
let len: usize = line.iter().map(|c| c.1.len()).sum();
2018-01-13 18:32:21 +00:00
row_count += len / (max_columns + 1) + 1;
2018-01-08 20:14:17 +00:00
}
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
}
#[test]
fn test_resize() {
let lines = vec![vec![(None, vec!['a'; 5])]; ModelLayout::ROWS_STEP];
let mut model = ModelLayout::new(5);
model.layout(&lines);
let (cols, rows) = model.size();
assert_eq!(5, cols);
assert_eq!(ModelLayout::ROWS_STEP, rows);
model.layout_append(&lines);
let (cols, rows) = model.size();
assert_eq!(5, cols);
assert_eq!(ModelLayout::ROWS_STEP * 2, rows);
assert_eq!(ModelLayout::ROWS_STEP * 2, model.model.rows);
}
#[test]
fn test_cols_filled() {
let lines = vec![vec![(None, vec!['a'; 3])]; 1];
let mut model = ModelLayout::new(5);
model.layout(&lines);
let (cols, _) = model.size();
assert_eq!(4, cols); // size is 3 and 4 - is with cursor position
let lines = vec![vec![(None, vec!['a'; 2])]; 1];
model.layout_append(&lines);
let (cols, _) = model.size();
assert_eq!(3, cols);
}
2018-01-02 22:26:04 +00:00
}