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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 = ' ';
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-19 08:47:23 +00:00
|
|
|
}
|