Merge branch 'rawterminal-alternatescreen-errors' into 'master'

Improve error handling in the `raw` and `screen` modules

See merge request redox-os/termion!178
This commit is contained in:
Jeremy Soller 2022-10-21 07:09:08 +00:00
commit 0082c8da04
4 changed files with 18 additions and 14 deletions

View File

@ -1,12 +1,12 @@
extern crate termion;
use termion::screen::*;
use termion::screen::IntoAlternateScreen;
use std::io::{Write, stdout};
use std::{time, thread};
fn main() {
{
let mut screen = AlternateScreen::from(stdout());
let mut screen = stdout().into_alternate_screen().unwrap();
write!(screen, "Welcome to the alternate screen.\n\nPlease wait patiently until we arrive back at the main screen in a about three seconds.").unwrap();
screen.flush().unwrap();

View File

@ -3,7 +3,7 @@ extern crate termion;
use termion::event::Key;
use termion::input::TermRead;
use termion::raw::IntoRawMode;
use termion::screen::*;
use termion::screen::{IntoAlternateScreen, ToAlternateScreen, ToMainScreen};
use std::io::{Write, stdout, stdin};
fn write_alt_screen_msg<W: Write>(screen: &mut W) {
@ -16,7 +16,7 @@ fn write_alt_screen_msg<W: Write>(screen: &mut W) {
fn main() {
let stdin = stdin();
let mut screen = AlternateScreen::from(stdout().into_raw_mode().unwrap());
let mut screen = stdout().into_raw_mode().unwrap().into_alternate_screen().unwrap();
write!(screen, "{}", termion::cursor::Hide).unwrap();
write_alt_screen_msg(&mut screen);

View File

@ -42,7 +42,7 @@ pub struct RawTerminal<W: Write> {
impl<W: Write> Drop for RawTerminal<W> {
fn drop(&mut self) {
set_terminal_attr(&self.prev_ios).unwrap();
let _ = set_terminal_attr(&self.prev_ios);
}
}

View File

@ -6,12 +6,12 @@
//! # Example
//!
//! ```rust
//! use termion::screen::AlternateScreen;
//! use termion::screen::IntoAlternateScreen;
//! use std::io::{Write, stdout};
//!
//! fn main() {
//! {
//! let mut screen = AlternateScreen::from(stdout());
//! let mut screen = stdout().into_alternate_screen().unwrap();
//! write!(screen, "Writing to alternate screen!").unwrap();
//! screen.flush().unwrap();
//! }
@ -51,18 +51,22 @@ pub struct AlternateScreen<W: Write> {
output: W,
}
impl<W: Write> AlternateScreen<W> {
/// Create an alternate screen wrapper struct for the provided output and switch the terminal
/// to the alternate screen.
pub fn from(mut output: W) -> Self {
write!(output, "{}", ToAlternateScreen).expect("switch to alternate screen");
AlternateScreen { output: output }
/// Extension trait for writers, providing the `into_alternate_screen` function.
pub trait IntoAlternateScreen: Write + Sized {
/// Switch the terminal controlled by this writer to use the alternate screen. The terminal will be
/// restored to the main screen when the `AlternateScreen` returned by this function is
/// dropped.
fn into_alternate_screen(mut self) -> io::Result<AlternateScreen<Self>> {
write!(self, "{}", ToAlternateScreen)?;
Ok(AlternateScreen { output: self })
}
}
impl<W: Write> IntoAlternateScreen for W {}
impl<W: Write> Drop for AlternateScreen<W> {
fn drop(&mut self) {
write!(self, "{}", ToMainScreen).expect("switch to main screen");
let _ = write!(self, "{}", ToMainScreen);
}
}