2016-03-31 16:19:08 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Color(pub f64, pub f64, pub f64);
|
|
|
|
|
2016-04-05 22:04:40 +00:00
|
|
|
pub const COLOR_BLACK: Color = Color(0.0, 0.0, 0.0);
|
|
|
|
pub const COLOR_WHITE: Color = Color(1.0, 1.0, 1.0);
|
2017-03-09 10:19:44 +00:00
|
|
|
pub const COLOR_RED: Color = Color(1.0, 0.0, 0.0);
|
2016-03-31 16:19:08 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Attrs {
|
2016-04-01 09:34:55 +00:00
|
|
|
pub italic: bool,
|
|
|
|
pub bold: bool,
|
2017-03-09 10:19:44 +00:00
|
|
|
pub underline: bool,
|
|
|
|
pub undercurl: bool,
|
2016-04-05 22:04:40 +00:00
|
|
|
pub foreground: Option<Color>,
|
|
|
|
pub background: Option<Color>,
|
2017-03-09 10:19:44 +00:00
|
|
|
pub special: Option<Color>,
|
2016-03-31 16:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Attrs {
|
|
|
|
pub fn new() -> Attrs {
|
|
|
|
Attrs {
|
2016-04-05 22:04:40 +00:00
|
|
|
foreground: None,
|
|
|
|
background: None,
|
2017-03-09 10:19:44 +00:00
|
|
|
special: None,
|
2016-04-01 09:34:55 +00:00
|
|
|
italic: false,
|
|
|
|
bold: false,
|
2017-03-09 10:19:44 +00:00
|
|
|
underline: false,
|
|
|
|
undercurl: false,
|
2016-03-31 16:19:08 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-03 13:04:52 +00:00
|
|
|
|
|
|
|
fn clear(&mut self) {
|
|
|
|
self.italic = false;
|
|
|
|
self.bold = false;
|
2017-03-09 10:19:44 +00:00
|
|
|
self.underline = false;
|
|
|
|
self.undercurl = false;
|
2016-04-05 22:04:40 +00:00
|
|
|
self.foreground = None;
|
|
|
|
self.background = None;
|
2017-03-09 10:19:44 +00:00
|
|
|
self.special = None;
|
2016-04-03 13:04:52 +00:00
|
|
|
}
|
2016-03-31 16:19:08 +00:00
|
|
|
}
|
|
|
|
|
2016-04-03 15:13:18 +00:00
|
|
|
#[derive(Clone)]
|
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-04-03 13:04:52 +00:00
|
|
|
self.attrs.clear();
|
2016-03-29 09:22:16 +00:00
|
|
|
}
|
2016-03-19 08:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct UiModel {
|
2016-04-03 22:34:44 +00:00
|
|
|
pub columns: usize,
|
|
|
|
pub rows: usize,
|
2016-03-28 15:05:10 +00:00
|
|
|
cur_row: usize,
|
|
|
|
cur_col: usize,
|
|
|
|
model: Vec<Vec<Cell>>,
|
2016-04-03 15:13:18 +00:00
|
|
|
top: usize,
|
|
|
|
bot: usize,
|
|
|
|
left: usize,
|
|
|
|
right: usize,
|
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-04-03 15:13:18 +00:00
|
|
|
top: 0,
|
|
|
|
bot: 0,
|
|
|
|
left: 0,
|
|
|
|
right: 0,
|
2016-03-19 08:47:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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_row = row as usize;
|
2016-05-03 17:25:52 +00:00
|
|
|
self.cur_col = col as usize;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_cursor(&self) -> (usize, usize) {
|
|
|
|
(self.cur_row, self.cur_col)
|
2016-03-28 15:05:10 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-04-03 15:13:18 +00:00
|
|
|
pub fn set_scroll_region(&mut self, top: u64, bot: u64, left: u64, right: u64) {
|
|
|
|
self.top = top as usize;
|
|
|
|
self.bot = bot as usize;
|
|
|
|
self.left = left as usize;
|
|
|
|
self.right = right as usize;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn copy_row(&mut self, row: usize, offset: i64, left: usize, right: usize) {
|
|
|
|
for col in left..right + 1 {
|
2017-02-26 19:33:44 +00:00
|
|
|
let from_row = (row as i64 + offset) as usize;
|
|
|
|
let from_cell = self.model[from_row][col].clone();
|
|
|
|
self.model[row][col] = from_cell;
|
|
|
|
}
|
2016-04-03 15:13:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn scroll(&mut self, count: i64) {
|
|
|
|
let (top, bot, left, right) = (self.top as i64, self.bot as i64, self.left, self.right);
|
|
|
|
|
|
|
|
if count > 0 {
|
|
|
|
for row in top as usize..(bot - count + 1) as usize {
|
|
|
|
self.copy_row(row, count, left, right);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for row in ((top - count) as usize..(bot + 1) as usize).rev() {
|
|
|
|
self.copy_row(row, count, left, right);
|
2016-03-29 09:22:16 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-03 15:13:18 +00:00
|
|
|
|
|
|
|
if count > 0 {
|
|
|
|
self.clear_region((bot - count + 1) as usize, bot as usize, left, right);
|
|
|
|
} else {
|
|
|
|
self.clear_region(top as usize, (top - count - 1) as usize, left, right);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
let (rows, columns) = (self.rows, self.columns);
|
|
|
|
self.clear_region(0, rows - 1, 0, columns - 1);
|
2016-03-29 09:22:16 +00:00
|
|
|
}
|
2016-04-03 13:04:52 +00:00
|
|
|
|
|
|
|
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) {
|
2016-04-03 15:13:18 +00:00
|
|
|
for row in &mut self.model[top..bot + 1] {
|
|
|
|
for cell in &mut row[left..right + 1] {
|
|
|
|
cell.clear();
|
2016-04-03 13:04:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-19 08:47:23 +00:00
|
|
|
}
|