//! Backend boundary for terminal output. //! //! Backends receive dirty [`Cell`](crate::Cell) runs, cursor updates, and clear //! operations from [`Terminal`](crate::Terminal). The core crate keeps this //! trait independent from any concrete terminal library so rendering can be //! tested deterministically. use std::convert::Infallible; use crate::Size; use crate::buffer::{Buffer, DirtyRun}; use crate::layout::{Point, Rect}; /// Terminal drawing backend. pub trait Backend { /// Backend error type. type Error; /// Returns the current terminal size. fn size(&mut self) -> Result; /// Draws dirty cell runs. fn draw(&mut self, runs: &[DirtyRun<'_>]) -> Result<(), Self::Error>; /// Clears the visible screen. fn clear(&mut self) -> Result<(), Self::Error>; /// Flushes buffered output. fn flush(&mut self) -> Result<(), Self::Error>; /// Moves the cursor. fn set_cursor_position(&mut self, _: Point) -> Result<(), Self::Error> { Ok(()) } /// Shows or hides the cursor. fn set_cursor_visible(&mut self, _: bool) -> Result<(), Self::Error> { Ok(()) } } /// In-memory backend for tests. #[derive(Debug, Clone)] pub struct TestBackend { buffer: Buffer, cursor: Option, } impl TestBackend { /// Creates a backend with a fixed size. pub fn new(size: Size) -> Self { Self { buffer: Buffer::empty(Rect::from_size(size)), cursor: None, } } /// Resizes the backend. pub fn resize(&mut self, size: Size) { self.buffer.resize(Rect::from_size(size)); } /// Returns the captured buffer. pub const fn buffer(&self) -> &Buffer { &self.buffer } /// Returns the captured cursor position. pub const fn cursor(&self) -> Option { self.cursor } /// Returns plain screen lines. pub fn lines(&self) -> Vec { self.buffer.lines() } } impl Backend for TestBackend { type Error = Infallible; fn size(&mut self) -> Result { Ok(self.buffer.size()) } fn draw(&mut self, runs: &[DirtyRun<'_>]) -> Result<(), Self::Error> { for run in runs { for (dx, cell) in run.cells.iter().enumerate() { self.buffer.set(run.x + dx, run.y, cell.clone()); } } Ok(()) } fn clear(&mut self) -> Result<(), Self::Error> { self.buffer .fill(self.buffer.area(), crate::buffer::Cell::blank()); Ok(()) } fn flush(&mut self) -> Result<(), Self::Error> { Ok(()) } fn set_cursor_position(&mut self, point: Point) -> Result<(), Self::Error> { self.cursor = Some(point); Ok(()) } } #[cfg(test)] #[allow(clippy::unwrap_used)] mod tests { use super::*; use crate::Style; use crate::terminal::Terminal; #[test] fn test_backend_captures_dirty_runs() { let backend = TestBackend::new(Size::new(6, 1)); let mut terminal = Terminal::new(backend).unwrap(); terminal .draw(|frame| frame.buffer.set_str(1, 0, "abc", Style::default())) .unwrap(); assert_eq!(terminal.backend().lines(), vec![" abc "]); } #[test] fn test_backend_supports_resize_clear_and_cursor_capture() { let mut backend = TestBackend::new(Size::new(3, 1)); backend .draw(&[DirtyRun { x: 0, y: 0, cells: &[crate::buffer::Cell::new('x', Style::default())], }]) .unwrap(); backend.set_cursor_position(Point::new(2, 0)).unwrap(); assert_eq!(backend.lines(), vec!["x "]); assert_eq!(backend.cursor(), Some(Point::new(2, 0))); backend.clear().unwrap(); assert_eq!(backend.lines(), vec![" "]); backend.resize(Size::new(2, 2)); assert_eq!(backend.lines(), vec![" ", " "]); } }