diff options
author | Joe Wilm <joe@jwilm.com> | 2016-05-30 20:44:37 -0700 |
---|---|---|
committer | Joe Wilm <joe@jwilm.com> | 2016-06-02 19:42:28 -0700 |
commit | 30ec14510935d46e7454863f9a4e63e53bf7728c (patch) | |
tree | 9501fe70ecf582e57903fbc061d3e6a0928f3f33 /src/grid.rs | |
parent | 70b0423a31016798592fc0e96ce316cb3f1e9d46 (diff) | |
download | alacritty-30ec14510935d46e7454863f9a4e63e53bf7728c.tar.gz alacritty-30ec14510935d46e7454863f9a4e63e53bf7728c.zip |
Initial support for Terminal Emulation (woo!)
This patch introduces basic support for terminal emulation. Basic means
commands that don't use paging and are not full screen applications like
vim or tmux. Some paging applications are working properly, such as as
`git log`. Other pagers work reasonably well as long as the help menu is
not accessed.
There is now a central Rgb color type which is shared by the renderer,
terminal emulation, and the pty parser.
The parser no longer owns a Handler. Instead, a mutable reference to a
Handler is provided whenever advancing the parser. This resolved some
potential ownership issues (eg parser owning the `Term` type would've
been unworkable).
Diffstat (limited to 'src/grid.rs')
-rw-r--r-- | src/grid.rs | 88 |
1 files changed, 76 insertions, 12 deletions
diff --git a/src/grid.rs b/src/grid.rs index 86a2e45f..765a5e1a 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -1,31 +1,41 @@ //! Functions for computing properties of the terminal grid -use std::ops::{Index, IndexMut}; +use std::collections::VecDeque; + +use std::ops::{Index, IndexMut, Deref, DerefMut}; + +use term::Cursor; +use ::Rgb; /// Calculate the number of cells for an axis pub fn num_cells_axis(cell_width: u32, cell_sep: i32, screen_width: u32) -> u32 { - ((screen_width as i32 + cell_sep) as f64 / (cell_width as i32 - cell_sep) as f64) as u32 + 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 } -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct Cell { - pub character: String, + pub c: char, + pub fg: Rgb, + pub bg: Rgb, } impl Cell { - pub fn new<S>(c: S) -> Cell - where S: Into<String> - { + pub fn new(c: char) -> Cell { Cell { - character: c.into(), + c: c.into(), + bg: Default::default(), + fg: Default::default(), } } } /// Represents the terminal display contents +#[derive(Clone)] pub struct Grid { /// Rows in the grid. Each row holds a list of cells corresponding to the columns in that row. - raw: Vec<Row>, + raw: VecDeque<Row>, /// Number of columns cols: usize, @@ -38,9 +48,9 @@ pub struct Grid { impl Grid { pub fn new(rows: usize, cols: usize) -> Grid { - let mut raw = Vec::with_capacity(rows); + let mut raw = VecDeque::with_capacity(rows); for _ in 0..raw.capacity() { - raw.push(Row::new(cols)); + raw.push_back(Row::new(cols)); } Grid { @@ -57,28 +67,67 @@ impl Grid { pub fn cols(&self) -> usize { self.cols } + + 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 = ' '; + } + } + } } impl Index<usize> for Grid { type Output = Row; + #[inline] fn index<'a>(&'a self, index: usize) -> &'a Row { &self.raw[index] } } impl IndexMut<usize> for Grid { + #[inline] fn index_mut<'a>(&'a mut self, index: usize) -> &'a mut Row { &mut self.raw[index] } } +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] + } +} + /// A row in the grid +#[derive(Debug, Clone)] pub struct Row(Vec<Cell>); impl Row { pub fn new(columns: usize) -> Row { - Row(vec![Cell::new(""); columns]) + Row(vec![Cell::new(' '); columns]) } pub fn cols(&self) -> usize { @@ -86,15 +135,30 @@ impl Row { } } +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 + } +} + impl Index<usize> for Row { type Output = Cell; + #[inline] fn index<'a>(&'a self, index: usize) -> &'a Cell { &self.0[index] } } impl IndexMut<usize> for Row { + #[inline] fn index_mut<'a>(&'a mut self, index: usize) -> &'a mut Cell { &mut self.0[index] } |