Skip to content

Commit

Permalink
fix(TestBackend): prevent area mismatch (#1252)
Browse files Browse the repository at this point in the history
Removes the height and width fields from TestBackend, which can get
out of sync with the Buffer, which currently clamps to 255,255.

This changes the `TestBackend` serde representation. It should be
possible to read older data, but data generated after this change
can't be read by older versions.
  • Loading branch information
EdJoPaTo authored Aug 5, 2024
1 parent c08b522 commit 864cd9f
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions src/backend/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ use crate::{
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TestBackend {
width: u16,
buffer: Buffer,
height: u16,
cursor: bool,
pos: (u16, u16),
}
Expand Down Expand Up @@ -74,8 +72,6 @@ impl TestBackend {
/// Creates a new `TestBackend` with the specified width and height.
pub fn new(width: u16, height: u16) -> Self {
Self {
width,
height,
buffer: Buffer::empty(Rect::new(0, 0, width, height)),
cursor: false,
pos: (0, 0),
Expand All @@ -90,8 +86,6 @@ impl TestBackend {
/// Resizes the `TestBackend` to the specified width and height.
pub fn resize(&mut self, width: u16, height: u16) {
self.buffer.resize(Rect::new(0, 0, width, height));
self.width = width;
self.height = height;
}

/// Asserts that the `TestBackend`'s buffer is equal to the expected buffer.
Expand Down Expand Up @@ -182,12 +176,12 @@ impl Backend for TestBackend {
}
ClearType::CurrentLine => {
let line_start_index = self.buffer.index_of(0, self.pos.1);
let line_end_index = self.buffer.index_of(self.width - 1, self.pos.1);
let line_end_index = self.buffer.index_of(self.buffer.area.width - 1, self.pos.1);
&mut self.buffer.content[line_start_index..=line_end_index]
}
ClearType::UntilNewLine => {
let index = self.buffer.index_of(self.pos.0, self.pos.1);
let line_end_index = self.buffer.index_of(self.width - 1, self.pos.1);
let line_end_index = self.buffer.index_of(self.buffer.area.width - 1, self.pos.1);
&mut self.buffer.content[index..=line_end_index]
}
};
Expand All @@ -211,24 +205,23 @@ impl Backend for TestBackend {
/// be added after the current position and the cursor will be moved to the last row.
fn append_lines(&mut self, n: u16) -> io::Result<()> {
let (cur_x, cur_y) = self.get_cursor()?;
let Rect { width, height, .. } = self.buffer.area;

// the next column ensuring that we don't go past the last column
let new_cursor_x = cur_x.saturating_add(1).min(self.width.saturating_sub(1));
let new_cursor_x = cur_x.saturating_add(1).min(width.saturating_sub(1));

let max_y = self.height.saturating_sub(1);
let max_y = height.saturating_sub(1);
let lines_after_cursor = max_y.saturating_sub(cur_y);
if n > lines_after_cursor {
let rotate_by = n.saturating_sub(lines_after_cursor).min(max_y);

if rotate_by == self.height - 1 {
if rotate_by == height - 1 {
self.clear()?;
}

self.set_cursor(0, rotate_by)?;
self.clear_region(ClearType::BeforeCursor)?;
self.buffer
.content
.rotate_left((self.width * rotate_by).into());
self.buffer.content.rotate_left((width * rotate_by).into());
}

let new_cursor_y = cur_y.saturating_add(n).min(max_y);
Expand All @@ -238,7 +231,7 @@ impl Backend for TestBackend {
}

fn size(&self) -> io::Result<Rect> {
Ok(Rect::new(0, 0, self.width, self.height))
Ok(self.buffer.area)
}

fn window_size(&mut self) -> io::Result<WindowSize> {
Expand All @@ -248,7 +241,7 @@ impl Backend for TestBackend {
height: 480,
};
Ok(WindowSize {
columns_rows: (self.width, self.height).into(),
columns_rows: self.buffer.area.as_size(),
pixels: WINDOW_PIXEL_SIZE,
})
}
Expand All @@ -267,8 +260,6 @@ mod tests {
assert_eq!(
TestBackend::new(10, 2),
TestBackend {
width: 10,
height: 2,
buffer: Buffer::with_lines([" "; 2]),
cursor: false,
pos: (0, 0),
Expand Down

0 comments on commit 864cd9f

Please sign in to comment.