summaryrefslogtreecommitdiff
path: root/src/buffer.rs
diff options
context:
space:
mode:
authorMica White <botahamec@outlook.com>2026-10-31 10:59:52 -0400
committerMica White <botahamec@outlook.com>2026-10-31 10:59:52 -0400
commitfde01cceb898b944b646a8fed1ce81cb14e523ef (patch)
treed1d5c97350fbbba2cc522c1f539c320ad4c2d1fc /src/buffer.rs
parent0c128e472b1b49e32c313874eaeccc8c5f1b1202 (diff)
Testing and optimization
Diffstat (limited to 'src/buffer.rs')
-rw-r--r--src/buffer.rs120
1 files changed, 110 insertions, 10 deletions
diff --git a/src/buffer.rs b/src/buffer.rs
index dcb7b8b..35692b1 100644
--- a/src/buffer.rs
+++ b/src/buffer.rs
@@ -13,6 +13,9 @@ use crossterm::{
LeaveAlternateScreen, WindowSize, disable_raw_mode, enable_raw_mode, window_size,
},
};
+use rustc_hash::FxHashMap;
+
+use crate::input::Position;
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq)]
pub struct Cell {
@@ -25,7 +28,11 @@ pub struct Cell {
pub struct Buffer {
stdout: Box<dyn Write>,
- pub cells: Vec<Cell>,
+ old_ephemeral_cells: Vec<Cell>,
+ new_eternal_cells: Vec<Cell>,
+ old_eternal_cells: Vec<Cell>,
+ eternal_cells: FxHashMap<Position, Vec<Cell>>,
+ ephemeral_cells: Vec<Cell>,
pub window_size: WindowSize,
}
@@ -51,15 +58,45 @@ impl TryFrom<crossterm::event::Event> for Event {
}
}
+impl Cell {
+ pub fn position(&self) -> Position {
+ Position {
+ x: self.x,
+ y: self.y,
+ }
+ }
+}
+
impl Buffer {
pub fn new(stdout: Box<dyn Write>) -> std::io::Result<Self> {
Ok(Self {
stdout,
- cells: Vec::new(),
+ old_eternal_cells: Vec::new(),
+ new_eternal_cells: Vec::new(),
+ old_ephemeral_cells: Vec::new(),
+ eternal_cells: FxHashMap::default(),
+ ephemeral_cells: Vec::new(),
window_size: window_size()?,
})
}
+ pub fn add_ephemeral_cell(&mut self, cell: Cell) {
+ self.ephemeral_cells.push(cell);
+ }
+
+ pub fn add_eternal_cell(&mut self, cell: Cell) {
+ self.new_eternal_cells.push(cell);
+ }
+
+ pub fn remove_eternal_cell(&mut self, cell: Cell) {
+ self.old_eternal_cells.push(cell);
+ if let Some(cells) = self.eternal_cells.get_mut(&cell.position())
+ && let Some(index) = cells.iter().rposition(|old_cell| *old_cell == cell)
+ {
+ cells.remove(index);
+ }
+ }
+
fn initialize(writer: &mut impl Write) -> std::io::Result<()> {
enable_raw_mode()?;
queue!(
@@ -72,24 +109,84 @@ impl Buffer {
Ok(())
}
+ fn queue_cell(&mut self, cell: Cell) -> std::io::Result<()> {
+ queue!(
+ self.stdout,
+ MoveTo(cell.x, cell.y),
+ PrintStyledContent(StyledContent::new(cell.style, cell.character))
+ )
+ }
+
+ fn queue_delete_cell(&mut self, cell: Cell) -> std::io::Result<()> {
+ if let Some(eternal_cells) = self.eternal_cells.get(&cell.position())
+ && let Some(new_cell) = eternal_cells.last()
+ {
+ if new_cell.z > cell.z {
+ return Ok(());
+ }
+
+ self.queue_cell(*new_cell)
+ } else {
+ self.queue_cell(Cell {
+ character: ' ',
+ style: ContentStyle::default(),
+ x: cell.x,
+ y: cell.y,
+ z: 0,
+ })
+ }
+ }
+
fn render(&mut self) -> std::io::Result<()> {
+ #[cfg(feature = "puffin")]
puffin::profile_function!();
{
+ #[cfg(feature = "puffin")]
puffin::profile_scope!("sort cells");
- self.cells.sort_unstable_by_key(|cell| cell.z);
+ self.ephemeral_cells
+ .sort_unstable_by_key(|cell| u8::MAX - cell.z);
+ self.eternal_cells
+ .values_mut()
+ .for_each(|cells| cells.sort_unstable_by_key(|cell| u8::MAX - cell.z));
}
{
+ #[cfg(feature = "puffin")]
puffin::profile_scope!("queue");
- queue!(self.stdout, BeginSynchronizedUpdate, Clear(ClearType::All))?;
- for cell in &self.cells {
- queue!(
- self.stdout,
- MoveTo(cell.x, cell.y),
- PrintStyledContent(StyledContent::new(cell.style, cell.character))
- )?;
+ queue!(self.stdout, BeginSynchronizedUpdate)?;
+ if self.eternal_cells.is_empty() && self.old_eternal_cells.is_empty() {
+ queue!(self.stdout, Clear(ClearType::All))?;
+ self.old_ephemeral_cells.clear();
+ self.old_eternal_cells.clear();
+ }
+ while let Some(cell) = self.old_ephemeral_cells.pop() {
+ self.queue_delete_cell(cell)?;
+ }
+ while let Some(cell) = self.old_eternal_cells.pop() {
+ self.queue_delete_cell(cell)?;
+ }
+
+ while let Some(cell) = self.new_eternal_cells.pop() {
+ self.eternal_cells
+ .entry(cell.position())
+ .or_default()
+ .push(cell);
+ self.queue_cell(cell)?;
+ }
+ while let Some(cell) = self.ephemeral_cells.pop() {
+ if self
+ .eternal_cells
+ .get(&cell.position())
+ .and_then(|cells| cells.last())
+ .is_some_and(|eternal_cell| eternal_cell.z > cell.z)
+ {
+ continue;
+ }
+ self.queue_cell(cell)?;
+ self.old_ephemeral_cells.push(cell);
}
queue!(self.stdout, EndSynchronizedUpdate)?;
}
+ #[cfg(feature = "puffin")]
puffin::profile_scope!("flush");
self.stdout.flush()?;
Ok(())
@@ -113,6 +210,7 @@ impl Buffer {
) -> std::io::Result<()> {
Self::initialize(&mut self.stdout)?;
loop {
+ #[cfg(feature = "puffin")]
puffin::profile_scope!("frame");
while matches!(event::poll(Duration::ZERO), Ok(true)) {
let Ok(event) = event::read() else { continue };
@@ -122,7 +220,9 @@ impl Buffer {
event_handler(self, event);
}
event_handler(self, Event::Render);
+ self.window_size = window_size()?;
self.render()?;
+ #[cfg(feature = "puffin")]
puffin::GlobalProfiler::lock().new_frame();
}
}