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
|
|
@ -1,32 +1,32 @@
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
use crossterm::style::{ContentStyle, Stylize};
|
use crossterm::style::{ContentStyle, Stylize};
|
||||||
use toss::buffer::Pos;
|
use toss::frame::Pos;
|
||||||
use toss::terminal::Terminal;
|
use toss::terminal::Terminal;
|
||||||
|
|
||||||
fn main() -> io::Result<()> {
|
fn main() -> io::Result<()> {
|
||||||
// Automatically enters alternate screen and enables raw mode
|
// Automatically enters alternate screen and enables raw mode
|
||||||
let mut term = Terminal::new(Box::new(io::stdout()))?;
|
let mut term = Terminal::new()?;
|
||||||
|
|
||||||
// Must be called before rendering, otherwise the terminal has out-of-date
|
// Must be called before rendering, otherwise the terminal has out-of-date
|
||||||
// size information and will present garbage.
|
// size information and will present garbage.
|
||||||
term.autoresize()?;
|
term.autoresize()?;
|
||||||
|
|
||||||
// Render things to the buffer
|
// Render stuff onto the next frame
|
||||||
let b = term.buffer();
|
let f = term.frame();
|
||||||
b.write(
|
f.write(
|
||||||
Pos::new(0, 0),
|
Pos::new(0, 0),
|
||||||
"Hello world!",
|
"Hello world!",
|
||||||
ContentStyle::default().green(),
|
ContentStyle::default().green(),
|
||||||
);
|
);
|
||||||
b.write(
|
f.write(
|
||||||
Pos::new(0, 1),
|
Pos::new(0, 1),
|
||||||
"Press any key to exit",
|
"Press any key to exit",
|
||||||
ContentStyle::default().on_dark_blue(),
|
ContentStyle::default().on_dark_blue(),
|
||||||
);
|
);
|
||||||
b.set_cursor(Some(Pos::new(16, 0)));
|
f.show_cursor(Pos::new(16, 0));
|
||||||
|
|
||||||
// Show the buffer's contents on screen
|
// Show the next frame on the screen
|
||||||
term.present()?;
|
term.present()?;
|
||||||
|
|
||||||
// Wait for input before exiting
|
// Wait for input before exiting
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use crossterm::style::ContentStyle;
|
||||||
use unicode_segmentation::UnicodeSegmentation;
|
use unicode_segmentation::UnicodeSegmentation;
|
||||||
use unicode_width::UnicodeWidthStr;
|
use unicode_width::UnicodeWidthStr;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
|
||||||
pub struct Size {
|
pub struct Size {
|
||||||
pub width: u16,
|
pub width: u16,
|
||||||
pub height: u16,
|
pub height: u16,
|
||||||
|
|
@ -48,22 +48,13 @@ impl Cell {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
pub struct Buffer {
|
pub struct Buffer {
|
||||||
size: Size,
|
size: Size,
|
||||||
data: Vec<Cell>,
|
data: Vec<Cell>,
|
||||||
cursor: Option<Pos>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Buffer {
|
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 {
|
fn index(&self, x: u16, y: u16) -> usize {
|
||||||
assert!(x < self.size.width);
|
assert!(x < self.size.width);
|
||||||
assert!(y < self.size.height);
|
assert!(y < self.size.height);
|
||||||
|
|
@ -86,7 +77,6 @@ impl Buffer {
|
||||||
pub fn resize(&mut self, size: Size) {
|
pub fn resize(&mut self, size: Size) {
|
||||||
if size == self.size() {
|
if size == self.size() {
|
||||||
self.data.fill_with(Cell::empty);
|
self.data.fill_with(Cell::empty);
|
||||||
self.cursor = None;
|
|
||||||
} else {
|
} else {
|
||||||
let width: usize = size.width.into();
|
let width: usize = size.width.into();
|
||||||
let height: usize = size.height.into();
|
let height: usize = size.height.into();
|
||||||
|
|
@ -95,7 +85,6 @@ impl Buffer {
|
||||||
self.size = size;
|
self.size = size;
|
||||||
self.data.clear();
|
self.data.clear();
|
||||||
self.data.resize_with(len, Cell::empty);
|
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())`.
|
/// `buf.reset()` is equivalent to `buf.resize(buf.size())`.
|
||||||
pub fn reset(&mut self) {
|
pub fn reset(&mut self) {
|
||||||
self.data.fill_with(Cell::empty);
|
self.data.fill_with(Cell::empty);
|
||||||
self.cursor = None;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write(&mut self, mut pos: Pos, content: &str, style: ContentStyle) {
|
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<'_> {
|
pub fn cells(&self) -> Cells<'_> {
|
||||||
Cells {
|
Cells {
|
||||||
buffer: self,
|
buffer: self,
|
||||||
|
|
|
||||||
41
src/frame.rs
Normal file
41
src/frame.rs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,2 +1,3 @@
|
||||||
pub mod buffer;
|
mod buffer;
|
||||||
|
pub mod frame;
|
||||||
pub mod terminal;
|
pub mod terminal;
|
||||||
|
|
|
||||||
|
|
@ -7,13 +7,15 @@ use crossterm::terminal::{Clear, ClearType, EnterAlternateScreen, LeaveAlternate
|
||||||
use crossterm::{ExecutableCommand, QueueableCommand};
|
use crossterm::{ExecutableCommand, QueueableCommand};
|
||||||
|
|
||||||
use crate::buffer::{Buffer, Size};
|
use crate::buffer::{Buffer, Size};
|
||||||
|
use crate::frame::Frame;
|
||||||
|
|
||||||
pub struct Terminal {
|
pub struct Terminal {
|
||||||
|
/// Render target.
|
||||||
out: Box<dyn Write>,
|
out: Box<dyn Write>,
|
||||||
/// Currently visible on screen.
|
/// The frame being currently rendered.
|
||||||
prev_buffer: Buffer,
|
frame: Frame,
|
||||||
/// Buffer to render to.
|
/// Buffer from the previous frame.
|
||||||
curr_buffer: Buffer,
|
prev_frame_buffer: Buffer,
|
||||||
/// When the screen is updated next, it must be cleared and redrawn fully
|
/// When the screen is updated next, it must be cleared and redrawn fully
|
||||||
/// instead of performing an incremental update.
|
/// instead of performing an incremental update.
|
||||||
full_redraw: bool,
|
full_redraw: bool,
|
||||||
|
|
@ -27,11 +29,15 @@ impl Drop for Terminal {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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 {
|
let mut result = Self {
|
||||||
out,
|
out,
|
||||||
prev_buffer: Buffer::new(),
|
frame: Frame::default(),
|
||||||
curr_buffer: Buffer::new(),
|
prev_frame_buffer: Buffer::default(),
|
||||||
full_redraw: true,
|
full_redraw: true,
|
||||||
};
|
};
|
||||||
crossterm::terminal::enable_raw_mode()?;
|
crossterm::terminal::enable_raw_mode()?;
|
||||||
|
|
@ -39,28 +45,32 @@ impl Terminal {
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn buffer(&mut self) -> &mut Buffer {
|
/// Resize the frame and other internal buffers if the terminal size has
|
||||||
&mut self.curr_buffer
|
/// changed.
|
||||||
}
|
|
||||||
|
|
||||||
pub fn autoresize(&mut self) -> io::Result<()> {
|
pub fn autoresize(&mut self) -> io::Result<()> {
|
||||||
let (width, height) = crossterm::terminal::size()?;
|
let (width, height) = crossterm::terminal::size()?;
|
||||||
let size = Size { width, height };
|
let size = Size { width, height };
|
||||||
if size != self.curr_buffer.size() {
|
if size != self.frame.size() {
|
||||||
self.prev_buffer.resize(size);
|
self.frame.buffer.resize(size);
|
||||||
self.curr_buffer.resize(size);
|
self.prev_frame_buffer.resize(size);
|
||||||
self.full_redraw = true;
|
self.full_redraw = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Display the contents of the buffer on the screen and prepare rendering
|
pub fn frame(&mut self) -> &mut Frame {
|
||||||
/// the next 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<()> {
|
pub fn present(&mut self) -> io::Result<()> {
|
||||||
if self.full_redraw {
|
if self.full_redraw {
|
||||||
io::stdout().queue(Clear(ClearType::All))?;
|
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;
|
self.full_redraw = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -68,15 +78,15 @@ impl Terminal {
|
||||||
self.update_cursor()?;
|
self.update_cursor()?;
|
||||||
self.out.flush()?;
|
self.out.flush()?;
|
||||||
|
|
||||||
mem::swap(&mut self.prev_buffer, &mut self.curr_buffer);
|
mem::swap(&mut self.prev_frame_buffer, &mut self.frame.buffer);
|
||||||
self.curr_buffer.reset();
|
self.frame.reset();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_differences(&mut self) -> io::Result<()> {
|
fn draw_differences(&mut self) -> io::Result<()> {
|
||||||
// TODO Only draw the differences
|
// 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);
|
let content = StyledContent::new(cell.style, &cell.content as &str);
|
||||||
self.out
|
self.out
|
||||||
.queue(MoveTo(x, y))?
|
.queue(MoveTo(x, y))?
|
||||||
|
|
@ -86,8 +96,8 @@ impl Terminal {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_cursor(&mut self) -> io::Result<()> {
|
fn update_cursor(&mut self) -> io::Result<()> {
|
||||||
if let Some(pos) = self.curr_buffer.cursor() {
|
if let Some(pos) = self.frame.cursor() {
|
||||||
let size = self.curr_buffer.size();
|
let size = self.frame.size();
|
||||||
let x_in_bounds = 0 <= pos.x && pos.x < size.width as i32;
|
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;
|
let y_in_bounds = 0 <= pos.y && pos.y < size.height as i32;
|
||||||
if x_in_bounds && y_in_bounds {
|
if x_in_bounds && y_in_bounds {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue