OpenptyOptions

Struct OpenptyOptions 

Source
pub struct OpenptyOptions {
    pub size: Option<Winsize>,
    pub nonblock: bool,
}
Expand description

Configurable options for opening PTYs

Fields§

§size: Option<Winsize>§nonblock: bool

Implementations§

Source§

impl OpenptyOptions

Source

pub fn new() -> Self

Same as default()

Examples found in repository?
examples/pty.rs (line 24)
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}
Source

pub fn with_size(self, size: Winsize) -> Self

Chainable function to set size

Examples found in repository?
examples/pty.rs (lines 25-28)
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}
Source

pub fn with_nonblocking(self, nonblock: bool) -> Self

Chainable function to set the master file to be nonblocking

Trait Implementations§

Source§

impl Clone for OpenptyOptions

Source§

fn clone(&self) -> OpenptyOptions

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for OpenptyOptions

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for OpenptyOptions

Source§

fn default() -> OpenptyOptions

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.