diff options
Diffstat (limited to 'src/grid/storage.rs')
-rw-r--r-- | src/grid/storage.rs | 110 |
1 files changed, 82 insertions, 28 deletions
diff --git a/src/grid/storage.rs b/src/grid/storage.rs index 6a119ead..32260426 100644 --- a/src/grid/storage.rs +++ b/src/grid/storage.rs @@ -15,9 +15,9 @@ use std::ops::{Index, IndexMut}; use static_assertions::assert_eq_size; -use crate::index::{Column, Line}; -use crate::grid::GridCell; use super::Row; +use crate::grid::GridCell; +use crate::index::{Column, Line}; /// Maximum number of invisible lines before buffer is resized const TRUNCATE_STEP: usize = 100; @@ -46,11 +46,8 @@ impl<T: PartialEq> ::std::cmp::PartialEq for Storage<T> { } // Check which vec has the bigger zero - let (ref bigger, ref smaller) = if self.zero >= other.zero { - (self, other) - } else { - (other, self) - }; + let (ref bigger, ref smaller) = + if self.zero >= other.zero { (self, other) } else { (other, self) }; // Calculate the actual zero offset let len = self.inner.len(); @@ -88,12 +85,7 @@ impl<T> Storage<T> { // Initialize visible lines, the scrollback buffer is initialized dynamically let inner = vec![template; lines.0]; - Storage { - inner, - zero: 0, - visible_lines: lines - 1, - len: lines.0, - } + Storage { inner, zero: 0, visible_lines: lines - 1, len: lines.0 } } /// Update the size of the scrollback history @@ -179,7 +171,8 @@ impl<T> Storage<T> { /// Dynamically grow the storage buffer at runtime pub fn initialize(&mut self, num_rows: usize, template_row: Row<T>) - where T: Clone + where + T: Clone, { let mut new = vec![template_row; num_rows]; @@ -297,7 +290,7 @@ impl<T> Storage<T> { #[inline] pub fn shrink_hidden(&mut self, cols: Column) where - T: GridCell + Copy + T: GridCell + Copy, { let start = self.zero + self.len; let end = self.zero + self.inner.len(); @@ -317,7 +310,7 @@ impl<T> Storage<T> { #[inline] pub fn grow_hidden(&mut self, cols: Column, template: &T) where - T: Copy + Clone + T: Copy + Clone, { let start = self.zero + self.len; let end = self.zero + self.inner.len(); @@ -333,6 +326,7 @@ impl<T> Storage<T> { impl<T> Index<usize> for Storage<T> { type Output = Row<T>; + #[inline] fn index(&self, index: usize) -> &Self::Output { &self.inner[self.compute_index(index)] @@ -349,6 +343,7 @@ impl<T> IndexMut<usize> for Storage<T> { impl<T> Index<Line> for Storage<T> { type Output = Row<T>; + #[inline] fn index(&self, index: Line) -> &Self::Output { let index = self.visible_lines - index; @@ -379,7 +374,11 @@ impl<T> IndexMut<Line> for Storage<T> { fn grow_after_zero() { // Setup storage area let mut storage = Storage { - inner: vec![Row::new(Column(1), &'0'), Row::new(Column(1), &'1'), Row::new(Column(1), &'-')], + inner: vec![ + Row::new(Column(1), &'0'), + Row::new(Column(1), &'1'), + Row::new(Column(1), &'-'), + ], zero: 0, visible_lines: Line(2), len: 3, @@ -390,7 +389,12 @@ fn grow_after_zero() { // Make sure the result is correct let expected = Storage { - inner: vec![Row::new(Column(1), &'-'), Row::new(Column(1), &'0'), Row::new(Column(1), &'1'), Row::new(Column(1), &'-')], + inner: vec![ + Row::new(Column(1), &'-'), + Row::new(Column(1), &'0'), + Row::new(Column(1), &'1'), + Row::new(Column(1), &'-'), + ], zero: 1, visible_lines: Line(0), len: 4, @@ -415,7 +419,11 @@ fn grow_after_zero() { fn grow_before_zero() { // Setup storage area let mut storage = Storage { - inner: vec![Row::new(Column(1), &'-'), Row::new(Column(1), &'0'), Row::new(Column(1), &'1')], + inner: vec![ + Row::new(Column(1), &'-'), + Row::new(Column(1), &'0'), + Row::new(Column(1), &'1'), + ], zero: 1, visible_lines: Line(2), len: 3, @@ -426,7 +434,12 @@ fn grow_before_zero() { // Make sure the result is correct let expected = Storage { - inner: vec![Row::new(Column(1), &'-'), Row::new(Column(1), &'-'), Row::new(Column(1), &'0'), Row::new(Column(1), &'1')], + inner: vec![ + Row::new(Column(1), &'-'), + Row::new(Column(1), &'-'), + Row::new(Column(1), &'0'), + Row::new(Column(1), &'1'), + ], zero: 2, visible_lines: Line(0), len: 4, @@ -450,7 +463,11 @@ fn grow_before_zero() { fn shrink_before_zero() { // Setup storage area let mut storage = Storage { - inner: vec![Row::new(Column(1), &'2'), Row::new(Column(1), &'0'), Row::new(Column(1), &'1')], + inner: vec![ + Row::new(Column(1), &'2'), + Row::new(Column(1), &'0'), + Row::new(Column(1), &'1'), + ], zero: 1, visible_lines: Line(2), len: 3, @@ -461,7 +478,11 @@ fn shrink_before_zero() { // Make sure the result is correct let expected = Storage { - inner: vec![Row::new(Column(1), &'2'), Row::new(Column(1), &'0'), Row::new(Column(1), &'1')], + inner: vec![ + Row::new(Column(1), &'2'), + Row::new(Column(1), &'0'), + Row::new(Column(1), &'1'), + ], zero: 1, visible_lines: Line(0), len: 2, @@ -485,7 +506,11 @@ fn shrink_before_zero() { fn shrink_after_zero() { // Setup storage area let mut storage = Storage { - inner: vec![Row::new(Column(1), &'0'), Row::new(Column(1), &'1'), Row::new(Column(1), &'2')], + inner: vec![ + Row::new(Column(1), &'0'), + Row::new(Column(1), &'1'), + Row::new(Column(1), &'2'), + ], zero: 0, visible_lines: Line(2), len: 3, @@ -496,7 +521,11 @@ fn shrink_after_zero() { // Make sure the result is correct let expected = Storage { - inner: vec![Row::new(Column(1), &'0'), Row::new(Column(1), &'1'), Row::new(Column(1), &'2')], + inner: vec![ + Row::new(Column(1), &'0'), + Row::new(Column(1), &'1'), + Row::new(Column(1), &'2'), + ], zero: 0, visible_lines: Line(0), len: 2, @@ -526,7 +555,14 @@ fn shrink_after_zero() { fn shrink_before_and_after_zero() { // Setup storage area let mut storage = Storage { - inner: vec![Row::new(Column(1), &'4'), Row::new(Column(1), &'5'), Row::new(Column(1), &'0'), Row::new(Column(1), &'1'), Row::new(Column(1), &'2'), Row::new(Column(1), &'3')], + inner: vec![ + Row::new(Column(1), &'4'), + Row::new(Column(1), &'5'), + Row::new(Column(1), &'0'), + Row::new(Column(1), &'1'), + Row::new(Column(1), &'2'), + Row::new(Column(1), &'3'), + ], zero: 2, visible_lines: Line(5), len: 6, @@ -537,7 +573,14 @@ fn shrink_before_and_after_zero() { // Make sure the result is correct let expected = Storage { - inner: vec![Row::new(Column(1), &'4'), Row::new(Column(1), &'5'), Row::new(Column(1), &'0'), Row::new(Column(1), &'1'), Row::new(Column(1), &'2'), Row::new(Column(1), &'3')], + inner: vec![ + Row::new(Column(1), &'4'), + Row::new(Column(1), &'5'), + Row::new(Column(1), &'0'), + Row::new(Column(1), &'1'), + Row::new(Column(1), &'2'), + Row::new(Column(1), &'3'), + ], zero: 2, visible_lines: Line(0), len: 2, @@ -563,7 +606,14 @@ fn shrink_before_and_after_zero() { fn truncate_invisible_lines() { // Setup storage area let mut storage = Storage { - inner: vec![Row::new(Column(1), &'4'), Row::new(Column(1), &'5'), Row::new(Column(1), &'0'), Row::new(Column(1), &'1'), Row::new(Column(1), &'2'), Row::new(Column(1), &'3')], + inner: vec![ + Row::new(Column(1), &'4'), + Row::new(Column(1), &'5'), + Row::new(Column(1), &'0'), + Row::new(Column(1), &'1'), + Row::new(Column(1), &'2'), + Row::new(Column(1), &'3'), + ], zero: 2, visible_lines: Line(1), len: 2, @@ -598,7 +648,11 @@ fn truncate_invisible_lines() { fn truncate_invisible_lines_beginning() { // Setup storage area let mut storage = Storage { - inner: vec![Row::new(Column(1), &'1'), Row::new(Column(1), &'2'), Row::new(Column(1), &'0')], + inner: vec![ + Row::new(Column(1), &'1'), + Row::new(Column(1), &'2'), + Row::new(Column(1), &'0'), + ], zero: 2, visible_lines: Line(1), len: 2, |