Struct RawTerminal

Source
pub struct RawTerminal<F: AsRawFd> { /* private fields */ }
Expand description

A structure that will automatically reset terminal mode when dropped

Implementations§

Source§

impl<F: AsRawFd> RawTerminal<F>

Source

pub fn new(file: F) -> Result<Self>

Switch the terminal to raw mode and return a wrapper that will exit raw mode automatically when dropped

Source

pub fn new_allow_failure(file: F) -> Self

Like new(), but ignores any failure. Useful for switching to raw mode only if that’s actually possible.

Examples found in repository?
examples/pty.rs (line 58)
11fn main() -> io::Result<()> {
12    let mut args = env::args().skip(1);
13
14    let cmd = match args.next() {
15        Some(cmd) => cmd,
16        None => {
17            eprintln!("Usage: pty <cmd>");
18            return Ok(());
19        }
20    };
21
22    // Step 1: Create PTY
23    let (mut master, slave) = pseudoterm::openpty(
24        &OpenptyOptions::new()
25            .with_size(Winsize {
26                cols: 80,
27                rows: 32,
28            })
29    )?;
30
31    // Step 2: Launch command
32    let mut child = pseudoterm::prepare_cmd(slave, &mut Command::new(cmd))?
33        .args(args)
34        .spawn()?;
35
36    // Step 3: Forward I/O
37    let mut clone = master.try_clone()?;
38    thread::spawn(move || {
39        let stdin = io::stdin();
40        let mut stdin = stdin.lock();
41
42        let mut buf = [0; 1024];
43        loop {
44            let res = match stdin.read(&mut buf) {
45                Ok(0) => break,
46                Ok(n) => clone.write_all(&buf[..n]),
47                Err(err) => Err(err)
48            };
49            if let Err(err) = res {
50                eprintln!("{}", err);
51                return;
52            }
53        }
54    });
55
56    let stdout = io::stdout();
57    //let stdout = stdout.lock();
58    let mut stdout = RawTerminal::new_allow_failure(stdout);
59
60    let mut buf = [0; 1024];
61    loop {
62        match master.read(&mut buf) {
63            Ok(0) | Err(_) => break,
64            Ok(n) => {
65                stdout.write_all(&buf[..n])?;
66                stdout.flush()?;
67            }
68        }
69    }
70
71    // Step 4: Wait until the process is completely dead
72    child.wait()?;
73
74    Ok(())
75}

Trait Implementations§

Source§

impl<F: AsRawFd> Deref for RawTerminal<F>

Source§

type Target = F

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<F: AsRawFd> DerefMut for RawTerminal<F>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<F: AsRawFd> Drop for RawTerminal<F>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<F> Freeze for RawTerminal<F>
where F: Freeze,

§

impl<F> RefUnwindSafe for RawTerminal<F>
where F: RefUnwindSafe,

§

impl<F> Send for RawTerminal<F>
where F: Send,

§

impl<F> Sync for RawTerminal<F>
where F: Sync,

§

impl<F> Unpin for RawTerminal<F>
where F: Unpin,

§

impl<F> UnwindSafe for RawTerminal<F>
where F: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.