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
impl OpenptyOptions
Sourcepub fn new() -> Self
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}
Sourcepub fn with_size(self, size: Winsize) -> Self
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}
Sourcepub fn with_nonblocking(self, nonblock: bool) -> Self
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
impl Clone for OpenptyOptions
Source§fn clone(&self) -> OpenptyOptions
fn clone(&self) -> OpenptyOptions
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for OpenptyOptions
impl Debug for OpenptyOptions
Source§impl Default for OpenptyOptions
impl Default for OpenptyOptions
Source§fn default() -> OpenptyOptions
fn default() -> OpenptyOptions
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for OpenptyOptions
impl RefUnwindSafe for OpenptyOptions
impl Send for OpenptyOptions
impl Sync for OpenptyOptions
impl Unpin for OpenptyOptions
impl UnwindSafe for OpenptyOptions
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