diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/grid/mod.rs | 2 | ||||
-rw-r--r-- | src/grid/storage.rs | 14 | ||||
-rw-r--r-- | src/grid/tests.rs | 79 | ||||
-rw-r--r-- | src/term/mod.rs | 24 |
4 files changed, 37 insertions, 82 deletions
diff --git a/src/grid/mod.rs b/src/grid/mod.rs index f1cec329..3495be1d 100644 --- a/src/grid/mod.rs +++ b/src/grid/mod.rs @@ -89,9 +89,11 @@ pub struct Grid<T> { /// If the displayed region isn't at the bottom of the screen, it stays /// stationary while more text is emitted. The scrolling implementation /// updates this offset accordingly. + #[serde(default)] display_offset: usize, /// An limit on how far back it's possible to scroll + #[serde(default)] scroll_limit: usize, /// Selected region diff --git a/src/grid/storage.rs b/src/grid/storage.rs index 1588b006..0f1ba9c5 100644 --- a/src/grid/storage.rs +++ b/src/grid/storage.rs @@ -13,15 +13,25 @@ /// done so manually. use std::ops::{Index, IndexMut}; -use index::Line; +use index::{IndexRange, Line}; -#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct Storage<T> { inner: Vec<T>, zero: usize, visible_lines: Line, } +impl<T: PartialEq> ::std::cmp::PartialEq for Storage<T> { + fn eq(&self, other: &Self) -> bool { + let mut equal = true; + for i in IndexRange(Line(0) .. self.visible_lines) { + equal = equal && (self[i] == other[i]) + } + equal + } +} + impl<T> Storage<T> { #[inline] pub fn with_capacity(cap: usize, lines: Line) -> Storage<T> { diff --git a/src/grid/tests.rs b/src/grid/tests.rs index 169cefa0..547fbcf9 100644 --- a/src/grid/tests.rs +++ b/src/grid/tests.rs @@ -17,78 +17,23 @@ use super::{Grid, BidirectionalIterator}; use index::{Point, Line, Column}; -#[test] -fn grid_swap_lines_ok() { - let mut grid = Grid::new(Line(10), Column(1), &0); - info!(""); - - // swap test ends - grid[Line(0)][Column(0)] = 1; - grid[Line(9)][Column(0)] = 2; - - assert_eq!(grid[Line(0)][Column(0)], 1); - assert_eq!(grid[Line(9)][Column(0)], 2); - - grid.swap_lines(Line(0), Line(9)); - - assert_eq!(grid[Line(0)][Column(0)], 2); - assert_eq!(grid[Line(9)][Column(0)], 1); - - // swap test mid - grid[Line(4)][Column(0)] = 1; - grid[Line(5)][Column(0)] = 2; - - info!("grid: {:?}", grid); - - assert_eq!(grid[Line(4)][Column(0)], 1); - assert_eq!(grid[Line(5)][Column(0)], 2); - - grid.swap_lines(Line(4), Line(5)); - - info!("grid: {:?}", grid); - - assert_eq!(grid[Line(4)][Column(0)], 2); - assert_eq!(grid[Line(5)][Column(0)], 1); -} - -#[test] -#[should_panic] -fn grid_swap_lines_oob1() { - let mut grid = Grid::new(Line(10), Column(1), &0); - grid.swap_lines(Line(0), Line(10)); -} - -#[test] -#[should_panic] -fn grid_swap_lines_oob2() { - let mut grid = Grid::new(Line(10), Column(1), &0); - grid.swap_lines(Line(10), Line(0)); -} - -#[test] -#[should_panic] -fn grid_swap_lines_oob3() { - let mut grid = Grid::new(Line(10), Column(1), &0); - grid.swap_lines(Line(10), Line(10)); -} - // Scroll up moves lines upwards #[test] fn scroll_up() { info!(""); - let mut grid = Grid::new(Line(10), Column(1), &0); + let mut grid = Grid::new(Line(10), Column(1), 0, 0); for i in 0..10 { grid[Line(i)][Column(0)] = i; } info!("grid: {:?}", grid); - grid.scroll_up(Line(0)..Line(10), Line(2)); + grid.scroll_up(&(Line(0)..Line(10)), Line(2)); info!("grid: {:?}", grid); - let mut other = Grid::new(Line(10), Column(1), &9); + let mut other = Grid::new(Line(10), Column(1), 0, 9); other[Line(0)][Column(0)] = 2; other[Line(1)][Column(0)] = 3; @@ -111,18 +56,18 @@ fn scroll_up() { fn scroll_down() { info!(""); - let mut grid = Grid::new(Line(10), Column(1), &0); + let mut grid = Grid::new(Line(10), Column(1), 0, 0); for i in 0..10 { grid[Line(i)][Column(0)] = i; } info!("grid: {:?}", grid); - grid.scroll_down(Line(0)..Line(10), Line(2)); + grid.scroll_down(&(Line(0)..Line(10)), Line(2)); info!("grid: {:?}", grid); - let mut other = Grid::new(Line(10), Column(1), &9); + let mut other = Grid::new(Line(10), Column(1), 0, 9); other[Line(0)][Column(0)] = 8; other[Line(1)][Column(0)] = 9; @@ -145,7 +90,7 @@ fn scroll_down() { fn test_iter() { info!(""); - let mut grid = Grid::new(Line(5), Column(5), &0); + let mut grid = Grid::new(Line(5), Column(5), 0, 0); for i in 0..5 { for j in 0..5 { grid[Line(i)][Column(j)] = i*5 + j; @@ -155,14 +100,14 @@ fn test_iter() { info!("grid: {:?}", grid); let mut iter = grid.iter_from(Point { - line: Line(0), + line: 4, col: Column(0), }); assert_eq!(None, iter.prev()); assert_eq!(Some(&1), iter.next()); assert_eq!(Column(1), iter.cur.col); - assert_eq!(Line(0), iter.cur.line); + assert_eq!(4, iter.cur.line); assert_eq!(Some(&2), iter.next()); assert_eq!(Some(&3), iter.next()); @@ -171,16 +116,16 @@ fn test_iter() { // test linewrapping assert_eq!(Some(&5), iter.next()); assert_eq!(Column(0), iter.cur.col); - assert_eq!(Line(1), iter.cur.line); + assert_eq!(3, iter.cur.line); assert_eq!(Some(&4), iter.prev()); assert_eq!(Column(4), iter.cur.col); - assert_eq!(Line(0), iter.cur.line); + assert_eq!(4, iter.cur.line); // test that iter ends at end of grid let mut final_iter = grid.iter_from(Point { - line: Line(4), + line: 0, col: Column(4), }); assert_eq!(None, final_iter.next()); diff --git a/src/term/mod.rs b/src/term/mod.rs index 198f8cea..7a50810e 100644 --- a/src/term/mod.rs +++ b/src/term/mod.rs @@ -2004,7 +2004,7 @@ mod tests { padding_y: 0.0, }; let mut term = Term::new(&Default::default(), size); - let mut grid: Grid<Cell> = Grid::new(Line(3), Column(5), &Cell::default()); + let mut grid: Grid<Cell> = Grid::new(Line(3), Column(5), 0, Cell::default()); for i in 0..5 { for j in 0..2 { grid[Line(j)][Column(i)].c = 'a'; @@ -2021,18 +2021,18 @@ mod tests { mem::swap(&mut term.semantic_escape_chars, &mut escape_chars); { - let selection = Selection::semantic(Point { line: Line(0), col: Column(1) }, &term); - assert_eq!(term.string_from_selection(&selection.to_span(&term).unwrap()), "aa"); + *term.selection_mut() = Some(Selection::semantic(Point { line: 2, col: Column(1) }, &term)); + assert_eq!(term.selection_to_string(), Some(String::from("aa"))); } { - let selection = Selection::semantic(Point { line: Line(0), col: Column(4) }, &term); - assert_eq!(term.string_from_selection(&selection.to_span(&term).unwrap()), "aaa"); + *term.selection_mut() = Some(Selection::semantic(Point { line: 2, col: Column(4) }, &term)); + assert_eq!(term.selection_to_string(), Some(String::from("aaa"))); } { - let selection = Selection::semantic(Point { line: Line(1), col: Column(1) }, &term); - assert_eq!(term.string_from_selection(&selection.to_span(&term).unwrap()), "aaa"); + *term.selection_mut() = Some(Selection::semantic(Point { line: 1, col: Column(1) }, &term)); + assert_eq!(term.selection_to_string(), Some(String::from("aaa"))); } } @@ -2047,7 +2047,7 @@ mod tests { padding_y: 0.0, }; let mut term = Term::new(&Default::default(), size); - let mut grid: Grid<Cell> = Grid::new(Line(1), Column(5), &Cell::default()); + let mut grid: Grid<Cell> = Grid::new(Line(1), Column(5), 0, Cell::default()); for i in 0..5 { grid[Line(0)][Column(i)].c = 'a'; } @@ -2057,10 +2057,8 @@ mod tests { mem::swap(&mut term.grid, &mut grid); - let selection = Selection::lines(Point { line: Line(0), col: Column(3) }); - if let Some(span) = selection.to_span(&term) { - assert_eq!(term.string_from_selection(&span), "\"aa\"a\n"); - } + *term.selection_mut() = Some(Selection::lines(Point { line: 0, col: Column(3) })); + assert_eq!(term.selection_to_string(), Some(String::from("\"aa\"a\n"))); } /// Check that the grid can be serialized back and forth losslessly @@ -2071,7 +2069,7 @@ mod tests { fn grid_serde() { let template = Cell::default(); - let grid = Grid::new(Line(24), Column(80), &template); + let grid: Grid<Cell> = Grid::new(Line(24), Column(80), 0, template); let serialized = serde_json::to_string(&grid).expect("ser"); let deserialized = serde_json::from_str::<Grid<Cell>>(&serialized) .expect("de"); |