Use Frame for rendering instead of Buffer

This commit is contained in:
Joscha 2022-05-21 22:33:37 +02:00
parent cc2f102141
commit add2f25aba
5 changed files with 85 additions and 53 deletions

View file

@ -2,7 +2,7 @@ use crossterm::style::ContentStyle;
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Size {
pub width: u16,
pub height: u16,
@ -48,22 +48,13 @@ impl Cell {
}
}
#[derive(Debug, Default)]
pub struct Buffer {
size: Size,
data: Vec<Cell>,
cursor: Option<Pos>,
}
impl Buffer {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self {
size: Size::ZERO,
data: vec![],
cursor: None,
}
}
fn index(&self, x: u16, y: u16) -> usize {
assert!(x < self.size.width);
assert!(y < self.size.height);
@ -86,7 +77,6 @@ impl Buffer {
pub fn resize(&mut self, size: Size) {
if size == self.size() {
self.data.fill_with(Cell::empty);
self.cursor = None;
} else {
let width: usize = size.width.into();
let height: usize = size.height.into();
@ -95,7 +85,6 @@ impl Buffer {
self.size = size;
self.data.clear();
self.data.resize_with(len, Cell::empty);
self.cursor = None;
}
}
@ -104,7 +93,6 @@ impl Buffer {
/// `buf.reset()` is equivalent to `buf.resize(buf.size())`.
pub fn reset(&mut self) {
self.data.fill_with(Cell::empty);
self.cursor = None;
}
pub fn write(&mut self, mut pos: Pos, content: &str, style: ContentStyle) {
@ -133,14 +121,6 @@ impl Buffer {
}
}
pub fn cursor(&self) -> Option<Pos> {
self.cursor
}
pub fn set_cursor(&mut self, pos: Option<Pos>) {
self.cursor = pos;
}
pub fn cells(&self) -> Cells<'_> {
Cells {
buffer: self,

41
src/frame.rs Normal file
View file

@ -0,0 +1,41 @@
use crossterm::style::ContentStyle;
use crate::buffer::Buffer;
pub use crate::buffer::{Pos, Size};
#[derive(Debug, Default)]
pub struct Frame {
pub(crate) buffer: Buffer,
cursor: Option<Pos>,
}
impl Frame {
pub fn size(&self) -> Size {
self.buffer.size()
}
pub fn reset(&mut self) {
self.buffer.reset();
self.cursor = None;
}
pub fn cursor(&self) -> Option<Pos> {
self.cursor
}
pub fn set_cursor(&mut self, pos: Option<Pos>) {
self.cursor = pos;
}
pub fn show_cursor(&mut self, pos: Pos) {
self.set_cursor(Some(pos));
}
pub fn hide_cursor(&mut self) {
self.set_cursor(None);
}
pub fn write(&mut self, pos: Pos, content: &str, style: ContentStyle) {
self.buffer.write(pos, content, style);
}
}

View file

@ -1,2 +1,3 @@
pub mod buffer;
mod buffer;
pub mod frame;
pub mod terminal;

View file

@ -7,13 +7,15 @@ use crossterm::terminal::{Clear, ClearType, EnterAlternateScreen, LeaveAlternate
use crossterm::{ExecutableCommand, QueueableCommand};
use crate::buffer::{Buffer, Size};
use crate::frame::Frame;
pub struct Terminal {
/// Render target.
out: Box<dyn Write>,
/// Currently visible on screen.
prev_buffer: Buffer,
/// Buffer to render to.
curr_buffer: Buffer,
/// The frame being currently rendered.
frame: Frame,
/// Buffer from the previous frame.
prev_frame_buffer: Buffer,
/// When the screen is updated next, it must be cleared and redrawn fully
/// instead of performing an incremental update.
full_redraw: bool,
@ -27,11 +29,15 @@ impl Drop for Terminal {
}
impl Terminal {
pub fn new(out: Box<dyn Write>) -> io::Result<Self> {
pub fn new() -> io::Result<Self> {
Self::with_target(Box::new(io::stdout()))
}
pub fn with_target(out: Box<dyn Write>) -> io::Result<Self> {
let mut result = Self {
out,
prev_buffer: Buffer::new(),
curr_buffer: Buffer::new(),
frame: Frame::default(),
prev_frame_buffer: Buffer::default(),
full_redraw: true,
};
crossterm::terminal::enable_raw_mode()?;
@ -39,28 +45,32 @@ impl Terminal {
Ok(result)
}
pub fn buffer(&mut self) -> &mut Buffer {
&mut self.curr_buffer
}
/// Resize the frame and other internal buffers if the terminal size has
/// changed.
pub fn autoresize(&mut self) -> io::Result<()> {
let (width, height) = crossterm::terminal::size()?;
let size = Size { width, height };
if size != self.curr_buffer.size() {
self.prev_buffer.resize(size);
self.curr_buffer.resize(size);
if size != self.frame.size() {
self.frame.buffer.resize(size);
self.prev_frame_buffer.resize(size);
self.full_redraw = true;
}
Ok(())
}
/// Display the contents of the buffer on the screen and prepare rendering
/// the next frame.
pub fn frame(&mut self) -> &mut Frame {
&mut self.frame
}
/// Display the current frame on the screen and prepare the next frame.
///
/// After calling this function, the frame returned by [`Self::frame`] will
/// be empty again and have no cursor position.
pub fn present(&mut self) -> io::Result<()> {
if self.full_redraw {
io::stdout().queue(Clear(ClearType::All))?;
self.prev_buffer.reset();
self.prev_frame_buffer.reset(); // Because the screen is now empty
self.full_redraw = false;
}
@ -68,15 +78,15 @@ impl Terminal {
self.update_cursor()?;
self.out.flush()?;
mem::swap(&mut self.prev_buffer, &mut self.curr_buffer);
self.curr_buffer.reset();
mem::swap(&mut self.prev_frame_buffer, &mut self.frame.buffer);
self.frame.reset();
Ok(())
}
fn draw_differences(&mut self) -> io::Result<()> {
// TODO Only draw the differences
for (x, y, cell) in self.curr_buffer.cells() {
for (x, y, cell) in self.frame.buffer.cells() {
let content = StyledContent::new(cell.style, &cell.content as &str);
self.out
.queue(MoveTo(x, y))?
@ -86,8 +96,8 @@ impl Terminal {
}
fn update_cursor(&mut self) -> io::Result<()> {
if let Some(pos) = self.curr_buffer.cursor() {
let size = self.curr_buffer.size();
if let Some(pos) = self.frame.cursor() {
let size = self.frame.size();
let x_in_bounds = 0 <= pos.x && pos.x < size.width as i32;
let y_in_bounds = 0 <= pos.y && pos.y < size.height as i32;
if x_in_bounds && y_in_bounds {