neovim-gtk/src/ui_model.rs

121 lines
2.7 KiB
Rust
Raw Normal View History

2016-03-31 16:19:08 +00:00
#[derive(Clone)]
pub struct Color(pub f64, pub f64, pub f64);
const COLOR_BLACK: Color = Color(0.0, 0.0, 0.0);
const COLOR_WHITE: Color = Color(1.0, 1.0, 1.0);
#[derive(Clone)]
pub struct Attrs {
2016-04-01 09:34:55 +00:00
pub italic: bool,
pub bold: bool,
2016-03-31 16:19:08 +00:00
pub foreground: Color,
pub background: Color,
}
impl Attrs {
pub fn new() -> Attrs {
Attrs {
foreground: COLOR_WHITE,
background: COLOR_BLACK,
2016-04-01 09:34:55 +00:00
italic: false,
bold: false,
2016-03-31 16:19:08 +00:00
}
}
fn clear(&mut self) {
self.italic = false;
self.bold = false;
self.foreground = COLOR_WHITE;
self.background = COLOR_BLACK;
}
2016-03-31 16:19:08 +00:00
}
2016-03-19 08:47:23 +00:00
pub struct Cell {
2016-03-31 16:19:08 +00:00
pub ch: char,
pub attrs: Attrs,
2016-03-19 08:47:23 +00:00
}
impl Cell {
pub fn new(ch: char) -> Cell {
2016-03-31 16:19:08 +00:00
Cell {
ch: ch,
attrs: Attrs::new(),
}
2016-03-19 08:47:23 +00:00
}
2016-03-29 09:22:16 +00:00
fn clear(&mut self) {
2016-03-31 16:19:08 +00:00
self.ch = ' ';
self.attrs.clear();
2016-03-29 09:22:16 +00:00
}
2016-03-19 08:47:23 +00:00
}
pub struct UiModel {
2016-03-28 15:05:10 +00:00
columns: usize,
rows: usize,
cur_row: usize,
cur_col: usize,
model: Vec<Vec<Cell>>,
2016-03-19 08:47:23 +00:00
}
impl UiModel {
2016-03-19 10:27:39 +00:00
pub fn empty() -> UiModel {
UiModel::new(0, 0)
}
2016-03-28 15:05:10 +00:00
pub fn new(rows: u64, columns: u64) -> UiModel {
let mut model = Vec::with_capacity(rows as usize);
for i in 0..rows as usize {
model.push(Vec::with_capacity(columns as usize));
2016-03-31 16:19:08 +00:00
for _ in 0..columns as usize {
2016-03-28 15:05:10 +00:00
model[i].push(Cell::new(' '));
}
2016-03-19 08:47:23 +00:00
}
2016-03-31 16:19:08 +00:00
UiModel {
2016-03-28 15:05:10 +00:00
columns: columns as usize,
rows: rows as usize,
2016-03-19 08:47:23 +00:00
cur_row: 0,
cur_col: 0,
model: model,
}
}
2016-03-31 16:19:08 +00:00
pub fn model(&self) -> &Vec<Vec<Cell>> {
&self.model
2016-03-31 10:09:34 +00:00
}
2016-03-28 14:03:21 +00:00
pub fn set_cursor(&mut self, row: u64, col: u64) {
2016-03-28 15:05:10 +00:00
self.cur_col = col as usize;
self.cur_row = row as usize;
}
2016-03-31 16:19:08 +00:00
pub fn put(&mut self, text: &str, attrs: &Option<Attrs>) {
let mut cell = &mut self.model[self.cur_row][self.cur_col];
cell.ch = text.chars().last().unwrap();
cell.attrs = attrs.as_ref().map(|o| o.clone()).unwrap_or_else(|| Attrs::new());
2016-03-28 15:05:10 +00:00
self.cur_col += 1;
2016-03-19 08:47:23 +00:00
}
2016-03-29 09:22:16 +00:00
pub fn clear(&mut self) {
for row in 0..self.rows {
for col in 0..self.columns {
self.model[row][col].clear();
}
}
}
pub fn eol_clear(&mut self) {
let (cur_row, cur_col, columns) = (self.cur_row, self.cur_col, self.columns);
self.clear_region(cur_row, cur_row, cur_col, columns - 1);
}
fn clear_region(&mut self, top: usize, bot: usize, left: usize, right: usize) {
for row in top..bot + 1 {
for col in left..right + 1 {
self.model[row][col].clear();
}
}
}
2016-03-19 08:47:23 +00:00
}