Return Redraw enum instead of bool

Boolean values were too easy to accidentally interpret the wrong way.
This commit is contained in:
Joscha 2022-05-28 19:05:31 +02:00
parent 3b2ea37ba5
commit 8fae7d2bf1
3 changed files with 51 additions and 31 deletions

View file

@ -1,8 +1,7 @@
use std::io; use crossterm::event::Event;
use crossterm::style::{ContentStyle, Stylize}; use crossterm::style::{ContentStyle, Stylize};
use toss::frame::{Frame, Pos}; use toss::frame::{Frame, Pos};
use toss::terminal::Terminal; use toss::terminal::{Redraw, Terminal};
fn draw(f: &mut Frame) { fn draw(f: &mut Frame) {
f.write( f.write(
@ -18,24 +17,32 @@ fn draw(f: &mut Frame) {
f.show_cursor(Pos::new(16, 0)); f.show_cursor(Pos::new(16, 0));
} }
fn main() -> io::Result<()> { fn render_frame(term: &mut Terminal) {
// Automatically enters alternate screen and enables raw mode
let mut term = Terminal::new()?;
loop { loop {
// 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().unwrap();
draw(term.frame()); draw(term.frame());
if !term.present()? { if term.present().unwrap() == Redraw::NotRequired {
break;
}
}
}
fn main() {
// Automatically enters alternate screen and enables raw mode
let mut term = Terminal::new().unwrap();
loop {
// Render and display a frame. A full frame is displayed on the terminal
// once this function exits.
render_frame(&mut term);
// Exit if the user presses any buttons
if !matches!(crossterm::event::read().unwrap(), Event::Resize(_, _)) {
break; break;
} }
} }
// Wait for input before exiting
let _ = crossterm::event::read();
Ok(())
} }

View file

@ -1,8 +1,7 @@
use std::io; use crossterm::event::Event;
use crossterm::style::{ContentStyle, Stylize}; use crossterm::style::{ContentStyle, Stylize};
use toss::frame::{Frame, Pos}; use toss::frame::{Frame, Pos};
use toss::terminal::Terminal; use toss::terminal::{Redraw, Terminal};
fn draw(f: &mut Frame) { fn draw(f: &mut Frame) {
f.write( f.write(
@ -58,24 +57,32 @@ fn draw(f: &mut Frame) {
} }
} }
fn main() -> io::Result<()> { fn render_frame(term: &mut Terminal) {
// Automatically enters alternate screen and enables raw mode
let mut term = Terminal::new()?;
loop { loop {
// 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().unwrap();
draw(term.frame()); draw(term.frame());
if !term.present()? { if term.present().unwrap() == Redraw::NotRequired {
break;
}
}
}
fn main() {
// Automatically enters alternate screen and enables raw mode
let mut term = Terminal::new().unwrap();
loop {
// Render and display a frame. A full frame is displayed on the terminal
// once this function exits.
render_frame(&mut term);
// Exit if the user presses any buttons
if !matches!(crossterm::event::read().unwrap(), Event::Resize(_, _)) {
break; break;
} }
} }
// Wait for input before exiting
let _ = crossterm::event::read();
Ok(())
} }

View file

@ -11,6 +11,12 @@ use crossterm::{ExecutableCommand, QueueableCommand};
use crate::buffer::{Buffer, Size}; use crate::buffer::{Buffer, Size};
use crate::frame::Frame; use crate::frame::Frame;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Redraw {
Required,
NotRequired,
}
pub struct Terminal { pub struct Terminal {
/// Render target. /// Render target.
out: Box<dyn Write>, out: Box<dyn Write>,
@ -71,7 +77,7 @@ impl Terminal {
/// ///
/// After calling this function, the frame returned by [`Self::frame`] will /// After calling this function, the frame returned by [`Self::frame`] will
/// be empty again and have no cursor position. /// be empty again and have no cursor position.
pub fn present(&mut self) -> io::Result<bool> { pub fn present(&mut self) -> io::Result<Redraw> {
if self.frame.widthdb.measuring_required() { if self.frame.widthdb.measuring_required() {
self.frame.widthdb.measure_widths(&mut self.out)?; self.frame.widthdb.measure_widths(&mut self.out)?;
// Since we messed up the screen by measuring widths, we'll need to // Since we messed up the screen by measuring widths, we'll need to
@ -81,7 +87,7 @@ impl Terminal {
// with unconfirmed width data. Also, this function guarantees that // with unconfirmed width data. Also, this function guarantees that
// after it is called, the frame is empty. // after it is called, the frame is empty.
self.frame.reset(); self.frame.reset();
return Ok(true); return Ok(Redraw::Required);
} }
if self.full_redraw { if self.full_redraw {
@ -97,7 +103,7 @@ impl Terminal {
mem::swap(&mut self.prev_frame_buffer, &mut self.frame.buffer); mem::swap(&mut self.prev_frame_buffer, &mut self.frame.buffer);
self.frame.reset(); self.frame.reset();
Ok(false) Ok(Redraw::NotRequired)
} }
fn draw_differences(&mut self) -> io::Result<()> { fn draw_differences(&mut self) -> io::Result<()> {