aboutsummaryrefslogtreecommitdiffstats
path: root/src/grid.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2019-10-30 03:00:22 -0400
committerJesse Luehrs <doy@tozt.net>2019-10-30 03:00:22 -0400
commitb198fd627ed422f55608531c7f22a7162cb87ae5 (patch)
tree198a46b0945ebb25f89a43fe13f0850c0bca5120 /src/grid.rs
parenta03e9e83016b8571ecbf0eda47dbbceaa1336a5d (diff)
downloadvt100-rust-b198fd627ed422f55608531c7f22a7162cb87ae5.tar.gz
vt100-rust-b198fd627ed422f55608531c7f22a7162cb87ae5.zip
centralize more logic for cursor movement
Diffstat (limited to 'src/grid.rs')
-rw-r--r--src/grid.rs128
1 files changed, 112 insertions, 16 deletions
diff --git a/src/grid.rs b/src/grid.rs
index bd7c1ba..b863fe2 100644
--- a/src/grid.rs
+++ b/src/grid.rs
@@ -1,27 +1,37 @@
pub struct Grid {
- size: crate::pos::Pos,
+ size: Size,
rows: Vec<crate::row::Row>,
}
impl Grid {
- pub fn new(size: crate::pos::Pos) -> Self {
+ pub fn new(size: Size) -> Self {
Self {
size,
- rows: vec![crate::row::Row::new(size.col); size.row as usize],
+ rows: vec![
+ crate::row::Row::new(size.cols());
+ size.rows() as usize
+ ],
}
}
- pub fn cell(&self, pos: crate::pos::Pos) -> Option<&crate::cell::Cell> {
- self.rows.get(pos.row as usize).and_then(|r| r.get(pos.col))
+ pub fn size(&self) -> &Size {
+ &self.size
}
- pub fn cell_mut(
- &mut self,
- pos: crate::pos::Pos,
- ) -> Option<&mut crate::cell::Cell> {
+ pub fn set_size(&mut self, size: Size) {
+ self.size = size;
+ }
+
+ pub fn cell(&self, pos: Pos) -> Option<&crate::cell::Cell> {
+ self.rows
+ .get(pos.row() as usize)
+ .and_then(|r| r.get(pos.col()))
+ }
+
+ pub fn cell_mut(&mut self, pos: Pos) -> Option<&mut crate::cell::Cell> {
self.rows
- .get_mut(pos.row as usize)
- .and_then(|v| v.get_mut(pos.col))
+ .get_mut(pos.row() as usize)
+ .and_then(|v| v.get_mut(pos.col()))
}
pub fn window_contents(
@@ -32,7 +42,7 @@ impl Grid {
col_end: u16,
) -> String {
let mut contents = String::new();
- for row in row_start..=(row_end.min(self.size.row)) {
+ for row in row_start..=(row_end.min(self.size.rows())) {
contents += &self.rows[row as usize].contents(col_start, col_end);
}
contents
@@ -40,11 +50,97 @@ impl Grid {
pub fn window_contents_formatted(
&self,
- row_start: u16,
- col_start: u16,
- row_end: u16,
- col_end: u16,
+ _row_start: u16,
+ _col_start: u16,
+ _row_end: u16,
+ _col_end: u16,
) -> String {
unimplemented!()
}
}
+
+#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
+pub struct Size {
+ rows: u16,
+ cols: u16,
+}
+
+impl Size {
+ pub fn new(rows: u16, cols: u16) -> Self {
+ Self { rows, cols }
+ }
+
+ pub fn rows(self) -> u16 {
+ self.rows
+ }
+
+ pub fn cols(self) -> u16 {
+ self.cols
+ }
+}
+
+#[derive(Copy, Clone, Debug, Eq, PartialEq)]
+pub struct Pos {
+ row: u16,
+ col: u16,
+ size: Size,
+}
+
+impl Pos {
+ pub fn new(row: u16, col: u16, size: Size) -> Self {
+ Self { row, col, size }
+ }
+
+ pub fn set_size(&mut self, size: Size) {
+ self.size = size;
+ }
+
+ pub fn row(self) -> u16 {
+ self.row
+ }
+
+ pub fn row_inc(&mut self, count: u16) {
+ // XXX this is not correct - scrolling past the bottom of the screen
+ self.row += count;
+ }
+
+ pub fn row_dec(&mut self, count: u16) {
+ // XXX this is not correct - scrolling past the top of the screen
+ self.row -= count;
+ }
+
+ pub fn row_set(&mut self, i: u16) {
+ self.row = i;
+ }
+
+ pub fn col(self) -> u16 {
+ self.col
+ }
+
+ pub fn col_inc(&mut self, count: u16) {
+ // XXX handle overflow
+ self.col += count;
+ if self.col > self.size.cols() {
+ self.col = 0;
+ self.row += 1;
+ }
+ }
+
+ pub fn col_dec(&mut self, count: u16) {
+ // XXX are there any cases where i need to wrap backwards?
+ if count > self.col {
+ self.col = 0;
+ } else {
+ self.col -= count;
+ }
+ }
+
+ pub fn col_set(&mut self, i: u16) {
+ self.col = i;
+ }
+
+ pub fn next_tabstop(&mut self) {
+ self.col -= self.col % 8;
+ self.col += 8;
+ }
+}