2016-04-10 23:19:39 +00:00
|
|
|
//! Functions for computing properties of the terminal grid
|
|
|
|
|
2016-05-31 03:44:37 +00:00
|
|
|
use std::collections::VecDeque;
|
|
|
|
|
|
|
|
use std::ops::{Index, IndexMut, Deref, DerefMut};
|
|
|
|
|
|
|
|
use term::Cursor;
|
|
|
|
use ::Rgb;
|
2016-04-10 23:19:39 +00:00
|
|
|
|
|
|
|
/// Calculate the number of cells for an axis
|
|
|
|
pub fn num_cells_axis(cell_width: u32, cell_sep: i32, screen_width: u32) -> u32 {
|
2016-05-31 03:44:37 +00:00
|
|
|
println!("num_cells_axis(cell_width: {}, cell_sep: {}, screen_width: {}",
|
|
|
|
cell_width, cell_sep, screen_width);
|
|
|
|
((screen_width as i32 - cell_sep) as f64 / (cell_width as i32 + cell_sep) as f64) as u32
|
2016-04-10 23:19:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 03:44:37 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2016-04-10 23:19:39 +00:00
|
|
|
pub struct Cell {
|
2016-05-31 03:44:37 +00:00
|
|
|
pub c: char,
|
|
|
|
pub fg: Rgb,
|
|
|
|
pub bg: Rgb,
|
2016-04-10 23:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Cell {
|
2016-05-31 03:44:37 +00:00
|
|
|
pub fn new(c: char) -> Cell {
|
2016-04-10 23:19:39 +00:00
|
|
|
Cell {
|
2016-05-31 03:44:37 +00:00
|
|
|
c: c.into(),
|
|
|
|
bg: Default::default(),
|
|
|
|
fg: Default::default(),
|
2016-04-10 23:19:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents the terminal display contents
|
2016-05-31 03:44:37 +00:00
|
|
|
#[derive(Clone)]
|
2016-04-10 23:19:39 +00:00
|
|
|
pub struct Grid {
|
|
|
|
/// Rows in the grid. Each row holds a list of cells corresponding to the columns in that row.
|
2016-05-31 03:44:37 +00:00
|
|
|
raw: VecDeque<Row>,
|
2016-04-10 23:19:39 +00:00
|
|
|
|
|
|
|
/// Number of columns
|
2016-04-11 15:05:19 +00:00
|
|
|
cols: usize,
|
2016-04-10 23:19:39 +00:00
|
|
|
|
|
|
|
/// Number of rows.
|
|
|
|
///
|
|
|
|
/// Invariant: rows is equivalent to cells.len()
|
|
|
|
rows: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Grid {
|
|
|
|
pub fn new(rows: usize, cols: usize) -> Grid {
|
2016-05-31 03:44:37 +00:00
|
|
|
let mut raw = VecDeque::with_capacity(rows);
|
2016-04-10 23:19:39 +00:00
|
|
|
for _ in 0..raw.capacity() {
|
2016-05-31 03:44:37 +00:00
|
|
|
raw.push_back(Row::new(cols));
|
2016-04-10 23:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Grid {
|
|
|
|
raw: raw,
|
2016-04-11 15:05:19 +00:00
|
|
|
cols: cols,
|
2016-04-10 23:19:39 +00:00
|
|
|
rows: rows,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rows(&self) -> usize {
|
|
|
|
self.rows
|
|
|
|
}
|
2016-04-11 15:05:19 +00:00
|
|
|
|
|
|
|
pub fn cols(&self) -> usize {
|
|
|
|
self.cols
|
|
|
|
}
|
2016-05-31 03:44:37 +00:00
|
|
|
|
|
|
|
pub fn feed(&mut self) {
|
|
|
|
// do the borrowck dance
|
|
|
|
let row = self.raw.pop_front().unwrap();
|
|
|
|
self.raw.push_back(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unfeed(&mut self) {
|
|
|
|
// do the borrowck dance
|
|
|
|
let row = self.raw.pop_back().unwrap();
|
|
|
|
self.raw.push_front(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
for row in self.raw.iter_mut() {
|
|
|
|
for cell in row.iter_mut() {
|
|
|
|
cell.c = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-10 23:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Index<usize> for Grid {
|
|
|
|
type Output = Row;
|
|
|
|
|
2016-05-31 03:44:37 +00:00
|
|
|
#[inline]
|
2016-04-10 23:19:39 +00:00
|
|
|
fn index<'a>(&'a self, index: usize) -> &'a Row {
|
|
|
|
&self.raw[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IndexMut<usize> for Grid {
|
2016-05-31 03:44:37 +00:00
|
|
|
#[inline]
|
2016-04-10 23:19:39 +00:00
|
|
|
fn index_mut<'a>(&'a mut self, index: usize) -> &'a mut Row {
|
|
|
|
&mut self.raw[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-31 03:44:37 +00:00
|
|
|
impl Index<Cursor> for Grid {
|
|
|
|
type Output = Cell;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index<'a>(&'a self, cursor: Cursor) -> &'a Cell {
|
|
|
|
&self.raw[cursor.y as usize][cursor.x as usize]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IndexMut<Cursor> for Grid {
|
|
|
|
#[inline]
|
|
|
|
fn index_mut<'a>(&'a mut self, cursor: Cursor) -> &'a mut Cell {
|
|
|
|
&mut self.raw[cursor.y as usize][cursor.x as usize]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-10 23:19:39 +00:00
|
|
|
/// A row in the grid
|
2016-05-31 03:44:37 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2016-04-10 23:19:39 +00:00
|
|
|
pub struct Row(Vec<Cell>);
|
|
|
|
|
|
|
|
impl Row {
|
|
|
|
pub fn new(columns: usize) -> Row {
|
2016-05-31 03:44:37 +00:00
|
|
|
Row(vec![Cell::new(' '); columns])
|
2016-04-10 23:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cols(&self) -> usize {
|
|
|
|
self.0.len()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-31 03:44:37 +00:00
|
|
|
impl Deref for Row {
|
|
|
|
type Target = Vec<Cell>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for Row {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-10 23:19:39 +00:00
|
|
|
impl Index<usize> for Row {
|
|
|
|
type Output = Cell;
|
|
|
|
|
2016-05-31 03:44:37 +00:00
|
|
|
#[inline]
|
2016-04-10 23:19:39 +00:00
|
|
|
fn index<'a>(&'a self, index: usize) -> &'a Cell {
|
|
|
|
&self.0[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IndexMut<usize> for Row {
|
2016-05-31 03:44:37 +00:00
|
|
|
#[inline]
|
2016-04-10 23:19:39 +00:00
|
|
|
fn index_mut<'a>(&'a mut self, index: usize) -> &'a mut Cell {
|
|
|
|
&mut self.0[index]
|
|
|
|
}
|
|
|
|
}
|