aboutsummaryrefslogtreecommitdiff
path: root/src/grid.rs
diff options
context:
space:
mode:
authorJoe Wilm <joe@jwilm.com>2016-04-10 16:19:39 -0700
committerJoe Wilm <joe@jwilm.com>2016-04-10 16:25:05 -0700
commitb84eb9e921c040c4eadaabd8f3087690efa267b6 (patch)
treea2cbfc7f71e5477905b823b4b1f0f4420c486eee /src/grid.rs
parent99474ef78a75c3f07b54770c95231af575e4b64c (diff)
downloadalacritty-b84eb9e921c040c4eadaabd8f3087690efa267b6.tar.gz
alacritty-b84eb9e921c040c4eadaabd8f3087690efa267b6.zip
Add a Grid
The grid holds the state of the terminal with row-major ordering. Eventually, the grid::Cell type will hold other attributes such as color, background color, decorations, and weight. An initialization list is added for common ASCII symbols.
Diffstat (limited to 'src/grid.rs')
-rw-r--r--src/grid.rs95
1 files changed, 95 insertions, 0 deletions
diff --git a/src/grid.rs b/src/grid.rs
new file mode 100644
index 00000000..93d9cd28
--- /dev/null
+++ b/src/grid.rs
@@ -0,0 +1,95 @@
+//! Functions for computing properties of the terminal grid
+
+use std::ops::{Index, IndexMut};
+
+/// 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
+}
+
+#[derive(Clone)]
+pub struct Cell {
+ pub character: Option<String>,
+}
+
+impl Cell {
+ pub fn new(c: Option<String>) -> Cell {
+ Cell {
+ character: c,
+ }
+ }
+}
+
+/// Represents the terminal display contents
+pub struct Grid {
+ /// Rows in the grid. Each row holds a list of cells corresponding to the columns in that row.
+ raw: Vec<Row>,
+
+ /// Number of columns
+ _cols: usize,
+
+ /// Number of rows.
+ ///
+ /// Invariant: rows is equivalent to cells.len()
+ rows: usize,
+}
+
+impl Grid {
+ pub fn new(rows: usize, cols: usize) -> Grid {
+ let mut raw = Vec::with_capacity(rows);
+ for _ in 0..raw.capacity() {
+ raw.push(Row::new(cols));
+ }
+
+ Grid {
+ raw: raw,
+ _cols: cols,
+ rows: rows,
+ }
+ }
+
+ pub fn rows(&self) -> usize {
+ self.rows
+ }
+}
+
+impl Index<usize> for Grid {
+ type Output = Row;
+
+ fn index<'a>(&'a self, index: usize) -> &'a Row {
+ &self.raw[index]
+ }
+}
+
+impl IndexMut<usize> for Grid {
+ fn index_mut<'a>(&'a mut self, index: usize) -> &'a mut Row {
+ &mut self.raw[index]
+ }
+}
+
+/// A row in the grid
+pub struct Row(Vec<Cell>);
+
+impl Row {
+ pub fn new(columns: usize) -> Row {
+ Row(vec![Cell::new(None); columns])
+ }
+
+ pub fn cols(&self) -> usize {
+ self.0.len()
+ }
+}
+
+impl Index<usize> for Row {
+ type Output = Cell;
+
+ fn index<'a>(&'a self, index: usize) -> &'a Cell {
+ &self.0[index]
+ }
+}
+
+impl IndexMut<usize> for Row {
+ fn index_mut<'a>(&'a mut self, index: usize) -> &'a mut Cell {
+ &mut self.0[index]
+ }
+}