TerminalRestorer -> RawTerminal
This commit is contained in:
parent
9efbb4e227
commit
2335596a1a
|
@ -44,7 +44,9 @@ pub trait TermWrite {
|
|||
self.csi(b"?25l")
|
||||
}
|
||||
|
||||
// TODO insert mode
|
||||
// TODO
|
||||
// fn mode
|
||||
|
||||
|
||||
/// Reset the rendition mode.
|
||||
///
|
||||
|
|
|
@ -28,7 +28,7 @@ mod error;
|
|||
pub use error::TerminalError;
|
||||
|
||||
mod raw;
|
||||
pub use raw::{IntoRawMode, TerminalRestorer};
|
||||
pub use raw::{IntoRawMode, RawTerminal};
|
||||
|
||||
mod size;
|
||||
pub use size::terminal_size;
|
||||
|
|
22
src/raw.rs
22
src/raw.rs
|
@ -6,12 +6,12 @@ use TerminalError;
|
|||
/// A terminal restorer, which keeps the previous state of the terminal, and restores it, when
|
||||
/// dropped.
|
||||
#[cfg(target_os = "redox")]
|
||||
pub struct TerminalRestorer<W> {
|
||||
pub struct RawTerminal<W> {
|
||||
output: W,
|
||||
}
|
||||
|
||||
#[cfg(target_os = "redox")]
|
||||
impl<W: Write> Drop for TerminalRestorer<W> {
|
||||
impl<W: Write> Drop for RawTerminal<W> {
|
||||
fn drop(&mut self) {
|
||||
use TermControl;
|
||||
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
|
||||
/// dropped.
|
||||
#[cfg(not(target_os = "redox"))]
|
||||
pub struct TerminalRestorer<W> {
|
||||
pub struct RawTerminal<W> {
|
||||
prev_ios: Termios,
|
||||
output: W,
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "redox"))]
|
||||
impl<W> Drop for TerminalRestorer<W> {
|
||||
impl<W> Drop for RawTerminal<W> {
|
||||
fn drop(&mut self) {
|
||||
use termios::set_terminal_attr;
|
||||
set_terminal_attr(&mut self.prev_ios as *mut _);
|
||||
}
|
||||
}
|
||||
|
||||
impl<W> Deref for TerminalRestorer<W> {
|
||||
impl<W> Deref for RawTerminal<W> {
|
||||
type Target = W;
|
||||
|
||||
fn deref(&self) -> &W {
|
||||
&self.output
|
||||
}
|
||||
}
|
||||
impl<W> DerefMut for TerminalRestorer<W> {
|
||||
impl<W> DerefMut for RawTerminal<W> {
|
||||
fn deref_mut(&mut self) -> &mut W {
|
||||
&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
|
||||
/// 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.
|
||||
fn into_raw_mode(self) -> Result<TerminalRestorer<Self>, TerminalError>;
|
||||
fn into_raw_mode(self) -> Result<RawTerminal<Self>, TerminalError>;
|
||||
}
|
||||
|
||||
impl<W: Write> IntoRawMode for W {
|
||||
#[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};
|
||||
|
||||
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 {
|
||||
Err(TerminalError::SetAttrError)
|
||||
} else {
|
||||
Ok(TerminalRestorer {
|
||||
Ok(RawTerminal {
|
||||
prev_ios: prev_ios,
|
||||
output: self,
|
||||
})
|
||||
}
|
||||
}
|
||||
#[cfg(target_os = "redox")]
|
||||
fn into_raw_mode(self) -> Result<TerminalRestorer<W>, TerminalError> {
|
||||
fn into_raw_mode(self) -> Result<RawTerminal<W>, TerminalError> {
|
||||
use TermControl;
|
||||
|
||||
if let Err(_) = self.csi("r") {
|
||||
Err(TerminalError::StdoutError)
|
||||
} else {
|
||||
Ok(TerminalRestorer {
|
||||
Ok(RawTerminal {
|
||||
output: self,
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue