diff --git a/src/event.rs b/src/event.rs index 4d50efda..1cdf1302 100644 --- a/src/event.rs +++ b/src/event.rs @@ -267,7 +267,8 @@ impl Processor { Closed => { if ref_test { // dump grid state - let grid = processor.ctx.terminal.grid(); + let mut grid = processor.ctx.terminal.grid().clone(); + grid.truncate(); let serialized_grid = json::to_string(&grid) .expect("serialize grid"); diff --git a/src/grid/mod.rs b/src/grid/mod.rs index 803f1e6e..0eea4201 100644 --- a/src/grid/mod.rs +++ b/src/grid/mod.rs @@ -407,6 +407,11 @@ impl Grid { self.raw.len() } + /// This is used only for truncating before saving ref-tests + pub fn truncate(&mut self) { + self.raw.truncate(); + } + pub fn iter_from(&self, point: Point) -> GridIterator { GridIterator { grid: self, diff --git a/src/grid/storage.rs b/src/grid/storage.rs index f59b01b7..885cb94c 100644 --- a/src/grid/storage.rs +++ b/src/grid/storage.rs @@ -15,6 +15,9 @@ use std::ops::{Index, IndexMut}; use index::Line; +/// Maximum number of invisible lines before buffer is resized +const TRUNCATE_STEP: usize = 100; + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Storage { inner: Vec, @@ -134,6 +137,32 @@ impl Storage { // Update visible lines self.visible_lines = next - 1; + + // Free memory + if self.inner.len() > self.len() + TRUNCATE_STEP { + self.truncate(); + } + } + + /// Truncate the invisible elements from the raw buffer + pub fn truncate(&mut self) { + // Calculate shrinkage/offset for indexing + let offset = self.zero % self.inner.len(); + let shrinkage = self.inner.len() - self.len; + let shrinkage_start = ::std::cmp::min(offset, shrinkage); + + // Create two vectors with correct ordering + let mut split = self.inner.split_off(offset); + + // Truncate the buffers + let len = self.inner.len(); + let split_len = split.len(); + self.inner.truncate(len - shrinkage_start); + split.truncate(split_len - (shrinkage - shrinkage_start)); + + // Merge buffers again and reset zero + self.zero = self.inner.len(); + self.inner.append(&mut split); } #[inline] @@ -411,3 +440,76 @@ fn shrink_before_and_after_zero() { assert_eq!(storage.zero, expected.zero); assert_eq!(storage.len, expected.len); } + +/// Check that when truncating all hidden lines are removed from the raw buffer +/// +/// Before: +/// 0: 4 <- Hidden +/// 1: 5 <- Hidden +/// 2: 0 <- Zero +/// 3: 1 +/// 4: 2 <- Hidden +/// 5: 3 <- Hidden +/// After: +/// 0: 0 <- Zero +/// 1: 1 +#[test] +fn truncate_invisible_lines() { + // Setup storage area + let mut storage = Storage { + inner: vec!["4", "5", "0", "1", "2", "3"], + zero: 2, + visible_lines: Line(1), + len: 2, + }; + + // Truncate buffer + storage.truncate(); + + // Make sure the result is correct + let expected = Storage { + inner: vec!["0", "1"], + zero: 0, + visible_lines: Line(1), + len: 2, + }; + assert_eq!(storage.visible_lines, expected.visible_lines); + assert_eq!(storage.inner, expected.inner); + assert_eq!(storage.zero, expected.zero); + assert_eq!(storage.len, expected.len); +} + +/// Truncate buffer only at the beginning +/// +/// Before: +/// 0: 1 +/// 1: 2 <- Hidden +/// 2: 0 <- Zero +/// After: +/// 0: 1 +/// 0: 0 <- Zero +#[test] +fn truncate_invisible_lines_beginning() { + // Setup storage area + let mut storage = Storage { + inner: vec!["1", "2", "0"], + zero: 2, + visible_lines: Line(1), + len: 2, + }; + + // Truncate buffer + storage.truncate(); + + // Make sure the result is correct + let expected = Storage { + inner: vec!["1", "0"], + zero: 1, + visible_lines: Line(1), + len: 2, + }; + assert_eq!(storage.visible_lines, expected.visible_lines); + assert_eq!(storage.inner, expected.inner); + assert_eq!(storage.zero, expected.zero); + assert_eq!(storage.len, expected.len); +} diff --git a/tests/ref.rs b/tests/ref.rs index 9ea0b80c..74cac6fa 100644 --- a/tests/ref.rs +++ b/tests/ref.rs @@ -1,5 +1,7 @@ -extern crate alacritty; +#[macro_use] +extern crate serde_derive; extern crate serde_json as json; +extern crate alacritty; use std::fs::File; use std::io::{self, Read}; @@ -61,26 +63,32 @@ fn read_u8

(path: P) -> Vec res } -fn read_string

(path: P) -> String +fn read_string

(path: P) -> Result where P: AsRef { let mut res = String::new(); - File::open(path.as_ref()).unwrap() - .read_to_string(&mut res).unwrap(); + File::open(path.as_ref()).and_then(|mut f| f.read_to_string(&mut res))?; - res + Ok(res) +} + +#[derive(Deserialize, Default)] +struct RefConfig { + history_size: u32, } fn ref_test(dir: &Path) { let recording = read_u8(dir.join("alacritty.recording")); - let serialized_size = read_string(dir.join("size.json")); - let serialized_grid = read_string(dir.join("grid.json")); + let serialized_size = read_string(dir.join("size.json")).unwrap(); + let serialized_grid = read_string(dir.join("grid.json")).unwrap(); + let serialized_cfg = read_string(dir.join("config.json")).unwrap_or_default(); let size: SizeInfo = json::from_str(&serialized_size).unwrap(); let grid: Grid = json::from_str(&serialized_grid).unwrap(); + let ref_config: RefConfig = json::from_str(&serialized_cfg).unwrap_or_default(); let mut config: Config = Default::default(); - config.set_history((grid.len() - grid.num_lines().0) as u32); + config.set_history(ref_config.history_size); let mut terminal = Term::new(&config, size); let mut parser = ansi::Processor::new(); @@ -89,7 +97,11 @@ fn ref_test(dir: &Path) { parser.advance(&mut terminal, byte, &mut io::sink()); } - if grid != *terminal.grid() { + // Truncate invisible lines from the grid + let mut term_grid = terminal.grid().clone(); + term_grid.truncate(); + + if grid != term_grid { for i in 0..grid.len() { for j in 0..grid.num_cols().0 { let cell = terminal.grid()[i][Column(j)]; @@ -104,5 +116,5 @@ fn ref_test(dir: &Path) { panic!("Ref test failed; grid doesn't match"); } - assert_eq!(grid, *terminal.grid()); + assert_eq!(grid, term_grid); } diff --git a/tests/ref/grid_reset/config.json b/tests/ref/grid_reset/config.json new file mode 100644 index 00000000..e794493c --- /dev/null +++ b/tests/ref/grid_reset/config.json @@ -0,0 +1 @@ +{"history_size":100} diff --git a/tests/ref/history/config.json b/tests/ref/history/config.json new file mode 100644 index 00000000..5cca8022 --- /dev/null +++ b/tests/ref/history/config.json @@ -0,0 +1 @@ +{"history_size":1000}