api: publicly expose backend error types

pull/478/head
Phillip Cloud 3 years ago
parent 6a49bf294e
commit 54cda04fc4

@ -7,7 +7,7 @@ use crate::symbols::{bar, block};
use crate::symbols::{line, DOT};
#[cfg(unix)]
use pancurses::{chtype, ToChtype};
use std::convert::Infallible;
pub use std::convert::Infallible as Error;
use unicode_segmentation::UnicodeSegmentation;
pub struct CursesBackend {
@ -34,7 +34,7 @@ impl CursesBackend {
}
impl Backend for CursesBackend {
type Error = Infallible;
type Error = Error;
fn draw<'a, I>(&mut self, content: I) -> Result<(), Self::Error>
where

@ -4,22 +4,30 @@ use crate::layout::Rect;
#[cfg(feature = "rustbox")]
pub(super) mod rustbox;
#[cfg(feature = "rustbox")]
pub use self::rustbox::Error as RustboxError;
#[cfg(feature = "rustbox")]
pub use self::rustbox::RustboxBackend;
#[cfg(feature = "termion")]
pub(super) mod termion;
#[cfg(feature = "termion")]
pub use self::termion::Error as TermionError;
#[cfg(feature = "termion")]
pub use self::termion::TermionBackend;
#[cfg(feature = "crossterm")]
pub(super) mod crossterm;
#[cfg(feature = "crossterm")]
pub use self::crossterm::CrosstermBackend;
#[cfg(feature = "crossterm")]
pub use self::crossterm::Error as CrosstermError;
#[cfg(feature = "curses")]
pub(super) mod curses;
#[cfg(feature = "curses")]
pub use self::curses::CursesBackend;
#[cfg(feature = "curses")]
pub use self::curses::Error as CursesError;
mod test;
pub use self::test::TestBackend;

@ -1,7 +1,10 @@
#[derive(Debug, thiserror::Error)]
pub enum Error {
pub enum Error<B>
where
B: std::error::Error + Send + Sync + 'static,
{
#[error("failed to execute backend operation")]
Backend(#[source] Box<dyn std::error::Error + Send + Sync + 'static>),
Backend(#[source] B),
#[error("failed to draw frame")]
DrawFrame(#[source] Box<dyn std::error::Error + Send + Sync + 'static>),

@ -176,8 +176,8 @@ where
{
/// Wrapper around Terminal initialization. Each buffer is initialized with a blank string and
/// default colors for the foreground and the background
pub fn new(backend: B) -> Result<Terminal<B>, Error> {
let size = backend.size().map_err(|e| Error::Backend(Box::new(e)))?;
pub fn new(backend: B) -> Result<Terminal<B>, B::Error> {
let size = backend.size()?;
Terminal::with_options(
backend,
TerminalOptions {
@ -190,7 +190,7 @@ where
}
/// UNSTABLE
pub fn with_options(backend: B, options: TerminalOptions) -> Result<Terminal<B>, Error> {
pub fn with_options(backend: B, options: TerminalOptions) -> Result<Terminal<B>, B::Error> {
Ok(Terminal {
backend,
buffers: [
@ -225,27 +225,25 @@ where
/// Obtains a difference between the previous and the current buffer and passes it to the
/// current backend for drawing.
pub fn flush(&mut self) -> Result<(), Error> {
pub fn flush(&mut self) -> Result<(), B::Error> {
let previous_buffer = &self.buffers[1 - self.current];
let current_buffer = &self.buffers[self.current];
let updates = previous_buffer.diff(current_buffer);
self.backend
.draw(updates.into_iter())
.map_err(|e| Error::Backend(Box::new(e)))
self.backend.draw(updates.into_iter())
}
/// Updates the Terminal so that internal buffers match the requested size. Requested size will
/// be saved so the size can remain consistent when rendering.
/// This leads to a full clear of the screen.
pub fn resize(&mut self, area: Rect) -> Result<(), Error> {
pub fn resize(&mut self, area: Rect) -> Result<(), B::Error> {
self.buffers[self.current].resize(area);
self.buffers[1 - self.current].resize(area);
self.viewport.area = area;
self.clear().map_err(|e| Error::Backend(Box::new(e)))
self.clear()
}
/// Queries the backend for size and resizes if it doesn't match the previous size.
pub fn autoresize(&mut self) -> Result<(), Error> {
pub fn autoresize(&mut self) -> Result<(), B::Error> {
if self.viewport.resize_behavior == ResizeBehavior::Auto {
let size = self.size()?;
if size != self.viewport.area {
@ -257,14 +255,14 @@ where
/// Synchronizes terminal size, calls the rendering closure, flushes the current internal state
/// and prepares for the next draw call.
pub fn draw<E, F>(&mut self, f: F) -> Result<CompletedFrame, Error>
pub fn draw<E, F>(&mut self, f: F) -> Result<CompletedFrame, Error<B::Error>>
where
E: std::error::Error + Send + Sync + 'static,
F: FnOnce(&mut Frame<B>) -> Result<(), E>,
{
// Autoresize - otherwise we get glitches if shrinking or potential desync between widgets
// and the terminal (if growing), which may OOB.
self.autoresize()?;
self.autoresize().map_err(Error::Backend)?;
let mut frame = self.get_frame();
f(&mut frame).map_err(|e| Error::DrawFrame(Box::new(e)))?;
@ -274,13 +272,13 @@ where
let cursor_position = frame.cursor_position;
// Draw to stdout
self.flush()?;
self.flush().map_err(Error::Backend)?;
match cursor_position {
None => self.hide_cursor()?,
None => self.hide_cursor().map_err(Error::Backend)?,
Some((x, y)) => {
self.show_cursor()?;
self.set_cursor(x, y)?;
self.show_cursor().map_err(Error::Backend)?;
self.set_cursor(x, y).map_err(Error::Backend)?;
}
}
@ -289,55 +287,43 @@ where
self.current = 1 - self.current;
// Flush
self.backend
.flush()
.map_err(|e| Error::Backend(Box::new(e)))?;
self.backend.flush().map_err(Error::Backend)?;
Ok(CompletedFrame {
buffer: &self.buffers[1 - self.current],
area: self.viewport.area,
})
}
pub fn hide_cursor(&mut self) -> Result<(), Error> {
self.backend
.hide_cursor()
.map_err(|e| Error::Backend(Box::new(e)))?;
pub fn hide_cursor(&mut self) -> Result<(), B::Error> {
self.backend.hide_cursor()?;
self.hidden_cursor = true;
Ok(())
}
pub fn show_cursor(&mut self) -> Result<(), Error> {
self.backend
.show_cursor()
.map_err(|e| Error::Backend(Box::new(e)))?;
pub fn show_cursor(&mut self) -> Result<(), B::Error> {
self.backend.show_cursor()?;
self.hidden_cursor = false;
Ok(())
}
pub fn get_cursor(&mut self) -> Result<(u16, u16), Error> {
self.backend
.get_cursor()
.map_err(|e| Error::Backend(Box::new(e)))
pub fn get_cursor(&mut self) -> Result<(u16, u16), B::Error> {
self.backend.get_cursor()
}
pub fn set_cursor(&mut self, x: u16, y: u16) -> Result<(), Error> {
self.backend
.set_cursor(x, y)
.map_err(|e| Error::Backend(Box::new(e)))
pub fn set_cursor(&mut self, x: u16, y: u16) -> Result<(), B::Error> {
self.backend.set_cursor(x, y)
}
/// Clear the terminal and force a full redraw on the next draw call.
pub fn clear(&mut self) -> Result<(), Error> {
self.backend
.clear()
.map_err(|e| Error::Backend(Box::new(e)))?;
pub fn clear(&mut self) -> Result<(), B::Error> {
self.backend.clear()?;
// Reset the back buffer to make sure the next update will redraw everything.
self.buffers[1 - self.current].reset();
Ok(())
}
/// Queries the real size of the backend.
pub fn size(&self) -> Result<Rect, Error> {
self.backend.size().map_err(|e| Error::Backend(Box::new(e)))
pub fn size(&self) -> Result<Rect, B::Error> {
self.backend.size()
}
}

Loading…
Cancel
Save