TerminalRestorer -> RawTerminal

This commit is contained in:
Ticki 2016-03-10 16:24:41 +01:00
parent 9efbb4e227
commit 2335596a1a
3 changed files with 15 additions and 13 deletions

View File

@ -44,7 +44,9 @@ pub trait TermWrite {
self.csi(b"?25l") self.csi(b"?25l")
} }
// TODO insert mode // TODO
// fn mode
/// Reset the rendition mode. /// Reset the rendition mode.
/// ///

View File

@ -28,7 +28,7 @@ mod error;
pub use error::TerminalError; pub use error::TerminalError;
mod raw; mod raw;
pub use raw::{IntoRawMode, TerminalRestorer}; pub use raw::{IntoRawMode, RawTerminal};
mod size; mod size;
pub use size::terminal_size; pub use size::terminal_size;

View File

@ -6,12 +6,12 @@ use TerminalError;
/// A terminal restorer, which keeps the previous state of the terminal, and restores it, when /// A terminal restorer, which keeps the previous state of the terminal, and restores it, when
/// dropped. /// dropped.
#[cfg(target_os = "redox")] #[cfg(target_os = "redox")]
pub struct TerminalRestorer<W> { pub struct RawTerminal<W> {
output: W, output: W,
} }
#[cfg(target_os = "redox")] #[cfg(target_os = "redox")]
impl<W: Write> Drop for TerminalRestorer<W> { impl<W: Write> Drop for RawTerminal<W> {
fn drop(&mut self) { fn drop(&mut self) {
use TermControl; use TermControl;
self.csi(b"R"); self.csi(b"R");
@ -23,27 +23,27 @@ use termios::Termios;
/// A terminal restorer, which keeps the previous state of the terminal, and restores it, when /// A terminal restorer, which keeps the previous state of the terminal, and restores it, when
/// dropped. /// dropped.
#[cfg(not(target_os = "redox"))] #[cfg(not(target_os = "redox"))]
pub struct TerminalRestorer<W> { pub struct RawTerminal<W> {
prev_ios: Termios, prev_ios: Termios,
output: W, output: W,
} }
#[cfg(not(target_os = "redox"))] #[cfg(not(target_os = "redox"))]
impl<W> Drop for TerminalRestorer<W> { impl<W> Drop for RawTerminal<W> {
fn drop(&mut self) { fn drop(&mut self) {
use termios::set_terminal_attr; use termios::set_terminal_attr;
set_terminal_attr(&mut self.prev_ios as *mut _); set_terminal_attr(&mut self.prev_ios as *mut _);
} }
} }
impl<W> Deref for TerminalRestorer<W> { impl<W> Deref for RawTerminal<W> {
type Target = W; type Target = W;
fn deref(&self) -> &W { fn deref(&self) -> &W {
&self.output &self.output
} }
} }
impl<W> DerefMut for TerminalRestorer<W> { impl<W> DerefMut for RawTerminal<W> {
fn deref_mut(&mut self) -> &mut W { fn deref_mut(&mut self) -> &mut W {
&mut self.output &mut self.output
} }
@ -56,12 +56,12 @@ pub trait IntoRawMode: Sized {
/// Raw mode means that stdin won't be printed (it will instead have to be written manually by the /// Raw mode means that stdin won't be printed (it will instead have to be written manually by the
/// program). Furthermore, the input isn't canonicalised or buffered (that is, you can read from /// program). Furthermore, the input isn't canonicalised or buffered (that is, you can read from
/// stdin one byte of a time). The output is neither modified in any way. /// stdin one byte of a time). The output is neither modified in any way.
fn into_raw_mode(self) -> Result<TerminalRestorer<Self>, TerminalError>; fn into_raw_mode(self) -> Result<RawTerminal<Self>, TerminalError>;
} }
impl<W: Write> IntoRawMode for W { impl<W: Write> IntoRawMode for W {
#[cfg(not(target_os = "redox"))] #[cfg(not(target_os = "redox"))]
fn into_raw_mode(self) -> Result<TerminalRestorer<W>, TerminalError> { fn into_raw_mode(self) -> Result<RawTerminal<W>, TerminalError> {
use termios::{cfmakeraw, get_terminal_attr, set_terminal_attr}; use termios::{cfmakeraw, get_terminal_attr, set_terminal_attr};
let (mut ios, err) = get_terminal_attr(); let (mut ios, err) = get_terminal_attr();
@ -77,20 +77,20 @@ impl<W: Write> IntoRawMode for W {
if set_terminal_attr(&mut ios as *mut _) != 0 { if set_terminal_attr(&mut ios as *mut _) != 0 {
Err(TerminalError::SetAttrError) Err(TerminalError::SetAttrError)
} else { } else {
Ok(TerminalRestorer { Ok(RawTerminal {
prev_ios: prev_ios, prev_ios: prev_ios,
output: self, output: self,
}) })
} }
} }
#[cfg(target_os = "redox")] #[cfg(target_os = "redox")]
fn into_raw_mode(self) -> Result<TerminalRestorer<W>, TerminalError> { fn into_raw_mode(self) -> Result<RawTerminal<W>, TerminalError> {
use TermControl; use TermControl;
if let Err(_) = self.csi("r") { if let Err(_) = self.csi("r") {
Err(TerminalError::StdoutError) Err(TerminalError::StdoutError)
} else { } else {
Ok(TerminalRestorer { Ok(RawTerminal {
output: self, output: self,
}) })
} }