Skip to content

Commit

Permalink
refactor(cell): reset instead of applying default (#1127)
Browse files Browse the repository at this point in the history
Using reset is clearer to me what actually happens. On the other case a
struct is created to override the old one completely which basically
does the same in a less clear way.
  • Loading branch information
EdJoPaTo committed May 22, 2024
1 parent 0b5fd6b commit bf20369
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
4 changes: 2 additions & 2 deletions examples/demo2/destroy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rand::Rng;
use rand_chacha::rand_core::SeedableRng;
use ratatui::{buffer::Cell, layout::Flex, prelude::*};
use ratatui::{layout::Flex, prelude::*};
use unicode_width::UnicodeWidthStr;

use crate::big_text::{BigTextBuilder, PixelSize};
Expand Down Expand Up @@ -59,7 +59,7 @@ fn drip(frame_count: usize, area: Rect, buf: &mut Buffer) {
if rng.gen_ratio(1, 10) {
*dest = src;
} else {
*dest = Cell::default();
dest.reset();
}
} else {
// move the pixel down one row
Expand Down
15 changes: 9 additions & 6 deletions src/backend/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,26 +170,29 @@ impl Backend for TestBackend {
}

fn clear_region(&mut self, clear_type: super::ClearType) -> io::Result<()> {
match clear_type {
ClearType::All => self.clear()?,
let region = match clear_type {
ClearType::All => return self.clear(),
ClearType::AfterCursor => {
let index = self.buffer.index_of(self.pos.0, self.pos.1) + 1;
self.buffer.content[index..].fill(Cell::default());
&mut self.buffer.content[index..]
}
ClearType::BeforeCursor => {
let index = self.buffer.index_of(self.pos.0, self.pos.1);
self.buffer.content[..index].fill(Cell::default());
&mut self.buffer.content[..index]
}
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);
self.buffer.content[line_start_index..=line_end_index].fill(Cell::default());
&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);
self.buffer.content[index..=line_end_index].fill(Cell::default());
&mut self.buffer.content[index..=line_end_index]
}
};
for cell in region {
cell.reset();
}
Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions src/buffer/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ impl Buffer {

/// Reset all cells in the buffer
pub fn reset(&mut self) {
for c in &mut self.content {
c.reset();
for cell in &mut self.content {
cell.reset();
}
}

Expand All @@ -303,7 +303,7 @@ impl Buffer {
let k = ((y - area.y) * area.width + x - area.x) as usize;
if i != k {
self.content[k] = self.content[i].clone();
self.content[i] = Cell::default();
self.content[i].reset();
}
}

Expand Down

0 comments on commit bf20369

Please sign in to comment.