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>
impl<F: AsRawFd> RawTerminal<F>
Sourcepub fn new(file: F) -> Result<Self>
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
Sourcepub fn new_allow_failure(file: F) -> Self
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>
impl<F: AsRawFd> Deref for RawTerminal<F>
Source§impl<F: AsRawFd> DerefMut for RawTerminal<F>
impl<F: AsRawFd> DerefMut for RawTerminal<F>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more