Add restore() method for restoring the defaults

This commit is contained in:
Ticki 2016-03-09 13:07:38 +01:00
parent 0efce912d0
commit 7269b5f07d
2 changed files with 15 additions and 10 deletions

View File

@ -44,9 +44,19 @@ pub trait TermWrite {
self.csi(b"?25l") self.csi(b"?25l")
} }
/// Reset the rendition mode. /// Reset the rendition mode.
///
/// This will reset both the current style and color.
fn reset(&mut self) -> IoResult<usize> { fn reset(&mut self) -> IoResult<usize> {
self.csi(b"m") self.csi(b"m")
} }
/// Restore the defaults.
///
/// This will reset color, position, cursor state, and so on. It is recommended that you use
/// this before you exit your program, to avoid messing up the user's terminal.
fn restore(&mut self) -> IoResult<usize> {
Ok(try!(self.reset()) + try!(self.clear()) + try!(self.goto(0, 0)) + try!(self.show_cursor()))
}
/// Go to a given position. /// Go to a given position.
/// ///
/// The position is 0-based. /// The position is 0-based.
@ -116,18 +126,12 @@ pub trait TermWrite {
impl<W: Write> TermWrite for W { impl<W: Write> TermWrite for W {
fn csi(&mut self, b: &[u8]) -> IoResult<usize> { fn csi(&mut self, b: &[u8]) -> IoResult<usize> {
self.write(b"\x1B[").and_then(|x| { Ok(try!(self.write(b"\x1B[")) + try!(self.write(b)))
self.write(b).map(|y| x + y)
})
} }
fn osc(&mut self, b: &[u8]) -> IoResult<usize> { fn osc(&mut self, b: &[u8]) -> IoResult<usize> {
self.write(b"\x1B]").and_then(|x| { Ok(try!(self.write(b"\x1B]")) + try!(self.write(b)))
self.write(b).map(|y| x + y)
})
} }
fn dsc(&mut self, b: &[u8]) -> IoResult<usize> { fn dsc(&mut self, b: &[u8]) -> IoResult<usize> {
self.write(b"\x1BP").and_then(|x| { Ok(try!(self.write(b"\x1BP")) + try!(self.write(b)))
self.write(b).map(|y| x + y)
})
} }
} }

View File

@ -1,4 +1,4 @@
use std::io::{self, Read, Write}; use std::io::{Read, Write};
use {IntoRawMode, TerminalError}; use {IntoRawMode, TerminalError};
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
@ -29,6 +29,7 @@ pub enum Key {
} }
/// An iterator over input keys. /// An iterator over input keys.
#[cfg(feature = "nightly")]
pub struct Keys<I> { pub struct Keys<I> {
chars: I, chars: I,
} }