.cargo/
crates/
brain/
diff/
files/
highlight/
repo/
signals/
tui/
tui-core/
src/
ansi/
aligned.rs
2.3 KiB
ansi.rs
2.3 KiB
app.rs
4.2 KiB
backend.rs
3.9 KiB
border.rs
6.5 KiB
buffer.rs
20.6 KiB
cell.rs
7.2 KiB
colors.rs
4.2 KiB
event.rs
3.4 KiB
frame.rs
3.7 KiB
hstack.rs
6.5 KiB
label.rs
5.5 KiB
layout.rs
11.2 KiB
lib.rs
2.4 KiB
primitive.rs
19.4 KiB
table.rs
23.5 KiB
terminal.rs
5.2 KiB
text.rs
3.0 KiB
textarea.rs
6.4 KiB
vstack.rs
16.4 KiB
widget.rs
11.0 KiB
Cargo.toml
298 B
README.md
521 B
tui-termion/
tui-widgets/
scripts/
src/
.gitignore
30 B
.gitsigners
198 B
AGENTS.md
1.9 KiB
CONTRIBUTING
1.7 KiB
Cargo.lock
5.4 KiB
Cargo.toml
1.9 KiB
LICENSE
1.1 KiB
README.md
245 B
STYLE
3.7 KiB
clippy.toml
213 B
crates/tui-core/src/backend.rs
raw
| 1 | //! Backend boundary for terminal output. |
| 2 | //! |
| 3 | //! Backends receive dirty [`Cell`](crate::Cell) runs, cursor updates, and clear |
| 4 | //! operations from [`Terminal`](crate::Terminal). The core crate keeps this |
| 5 | //! trait independent from any concrete terminal library so rendering can be |
| 6 | //! tested deterministically. |
| 7 | |
| 8 | use std::convert::Infallible; |
| 9 | |
| 10 | use crate::Size; |
| 11 | use crate::buffer::{Buffer, DirtyRun}; |
| 12 | use crate::layout::{Point, Rect}; |
| 13 | |
| 14 | /// Terminal drawing backend. |
| 15 | pub trait Backend { |
| 16 | /// Backend error type. |
| 17 | type Error; |
| 18 | |
| 19 | /// Returns the current terminal size. |
| 20 | fn size(&mut self) -> Result<Size, Self::Error>; |
| 21 | /// Draws dirty cell runs. |
| 22 | fn draw(&mut self, runs: &[DirtyRun<'_>]) -> Result<(), Self::Error>; |
| 23 | /// Clears the visible screen. |
| 24 | fn clear(&mut self) -> Result<(), Self::Error>; |
| 25 | /// Flushes buffered output. |
| 26 | fn flush(&mut self) -> Result<(), Self::Error>; |
| 27 | |
| 28 | /// Moves the cursor. |
| 29 | fn set_cursor_position(&mut self, _: Point) -> Result<(), Self::Error> { |
| 30 | Ok(()) |
| 31 | } |
| 32 | /// Shows or hides the cursor. |
| 33 | fn set_cursor_visible(&mut self, _: bool) -> Result<(), Self::Error> { |
| 34 | Ok(()) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /// In-memory backend for tests. |
| 39 | #[derive(Debug, Clone)] |
| 40 | pub struct TestBackend { |
| 41 | buffer: Buffer, |
| 42 | cursor: Option<Point>, |
| 43 | } |
| 44 | |
| 45 | impl TestBackend { |
| 46 | /// Creates a backend with a fixed size. |
| 47 | pub fn new(size: Size) -> Self { |
| 48 | Self { |
| 49 | buffer: Buffer::empty(Rect::from_size(size)), |
| 50 | cursor: None, |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /// Resizes the backend. |
| 55 | pub fn resize(&mut self, size: Size) { |
| 56 | self.buffer.resize(Rect::from_size(size)); |
| 57 | } |
| 58 | |
| 59 | /// Returns the captured buffer. |
| 60 | pub const fn buffer(&self) -> &Buffer { |
| 61 | &self.buffer |
| 62 | } |
| 63 | |
| 64 | /// Returns the captured cursor position. |
| 65 | pub const fn cursor(&self) -> Option<Point> { |
| 66 | self.cursor |
| 67 | } |
| 68 | |
| 69 | /// Returns plain screen lines. |
| 70 | pub fn lines(&self) -> Vec<String> { |
| 71 | self.buffer.lines() |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | impl Backend for TestBackend { |
| 76 | type Error = Infallible; |
| 77 | |
| 78 | fn size(&mut self) -> Result<Size, Self::Error> { |
| 79 | Ok(self.buffer.size()) |
| 80 | } |
| 81 | |
| 82 | fn draw(&mut self, runs: &[DirtyRun<'_>]) -> Result<(), Self::Error> { |
| 83 | for run in runs { |
| 84 | for (dx, cell) in run.cells.iter().enumerate() { |
| 85 | self.buffer.set(run.x + dx, run.y, cell.clone()); |
| 86 | } |
| 87 | } |
| 88 | Ok(()) |
| 89 | } |
| 90 | |
| 91 | fn clear(&mut self) -> Result<(), Self::Error> { |
| 92 | self.buffer |
| 93 | .fill(self.buffer.area(), crate::buffer::Cell::blank()); |
| 94 | Ok(()) |
| 95 | } |
| 96 | |
| 97 | fn flush(&mut self) -> Result<(), Self::Error> { |
| 98 | Ok(()) |
| 99 | } |
| 100 | |
| 101 | fn set_cursor_position(&mut self, point: Point) -> Result<(), Self::Error> { |
| 102 | self.cursor = Some(point); |
| 103 | Ok(()) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | #[cfg(test)] |
| 108 | #[allow(clippy::unwrap_used)] |
| 109 | mod tests { |
| 110 | use super::*; |
| 111 | use crate::Style; |
| 112 | use crate::terminal::Terminal; |
| 113 | |
| 114 | #[test] |
| 115 | fn test_backend_captures_dirty_runs() { |
| 116 | let backend = TestBackend::new(Size::new(6, 1)); |
| 117 | let mut terminal = Terminal::new(backend).unwrap(); |
| 118 | |
| 119 | terminal |
| 120 | .draw(|frame| frame.buffer.set_str(1, 0, "abc", Style::default())) |
| 121 | .unwrap(); |
| 122 | |
| 123 | assert_eq!(terminal.backend().lines(), vec![" abc "]); |
| 124 | } |
| 125 | |
| 126 | #[test] |
| 127 | fn test_backend_supports_resize_clear_and_cursor_capture() { |
| 128 | let mut backend = TestBackend::new(Size::new(3, 1)); |
| 129 | |
| 130 | backend |
| 131 | .draw(&[DirtyRun { |
| 132 | x: 0, |
| 133 | y: 0, |
| 134 | cells: &[crate::buffer::Cell::new('x', Style::default())], |
| 135 | }]) |
| 136 | .unwrap(); |
| 137 | backend.set_cursor_position(Point::new(2, 0)).unwrap(); |
| 138 | assert_eq!(backend.lines(), vec!["x "]); |
| 139 | assert_eq!(backend.cursor(), Some(Point::new(2, 0))); |
| 140 | |
| 141 | backend.clear().unwrap(); |
| 142 | assert_eq!(backend.lines(), vec![" "]); |
| 143 | |
| 144 | backend.resize(Size::new(2, 2)); |
| 145 | assert_eq!(backend.lines(), vec![" ", " "]); |
| 146 | } |
| 147 | } |