Use Frame for rendering instead of Buffer
This commit is contained in:
parent
cc2f102141
commit
add2f25aba
5 changed files with 85 additions and 53 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue