diff --git a/examples/repl.rs b/examples/repl.rs index 4a305e46..674b4cbe 100644 --- a/examples/repl.rs +++ b/examples/repl.rs @@ -5,20 +5,15 @@ use rexpect::session::PtyReplSession; use rexpect::spawn; fn ed_session() -> Result { - let mut ed = PtyReplSession { + let mut ed = PtyReplSession::new(spawn("/bin/ed -p '> '", Some(2000))?, "> ".to_owned()) // for `echo_on` you need to figure that out by trial and error. // For bash and python repl it is false - echo_on: false, - - // used for `wait_for_prompt()` - prompt: "> ".to_owned(), - pty_session: spawn("/bin/ed -p '> '", Some(2000))?, + .echo_on(false) // command which is sent when the instance of this struct is dropped // in the below example this is not needed, but if you don't explicitly // exit a REPL then rexpect tries to send a SIGTERM and depending on the repl // this does not end the repl and would end up in an error - quit_command: Some("Q".to_owned()), - }; + .quit_command(Some("Q".to_owned())); ed.wait_for_prompt()?; Ok(ed) } diff --git a/src/error.rs b/src/error.rs index a64b906a..06ae2798 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,7 @@ use std::time; #[derive(Debug, thiserror::Error)] +#[non_exhaustive] pub enum Error { #[error("EOF (End of File): Expected {:?} but got EOF after reading {:?} process terminated with {:?}", .expected, .got, .exit_code.as_ref().unwrap_or(&"unknown".to_owned()))] EOF { diff --git a/src/process.rs b/src/process.rs index c1db0b7c..979bd142 100644 --- a/src/process.rs +++ b/src/process.rs @@ -5,6 +5,7 @@ use nix; use nix::fcntl::{OFlag, open}; use nix::libc::STDERR_FILENO; use nix::pty::{PtyMaster, grantpt, posix_openpt, unlockpt}; +use nix::sys::{signal, wait}; use nix::sys::{stat, termios}; use nix::unistd::{ ForkResult, Pid, close, dup, dup2_stderr, dup2_stdin, dup2_stdout, fork, setsid, @@ -17,7 +18,6 @@ use std::os::unix::process::CommandExt; use std::process::Command; use std::{thread, time}; -pub use nix::sys::{signal, wait}; pub use signal::Signal; pub use wait::WaitStatus; @@ -57,8 +57,8 @@ pub use wait::WaitStatus; /// # } /// ``` pub struct PtyProcess { - pub pty: PtyMaster, - pub child_pid: Pid, + pty: PtyMaster, + pub(crate) child_pid: Pid, kill_timeout: Option, } diff --git a/src/reader.rs b/src/reader.rs index 6b1efd09..e8e3e984 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -14,9 +14,9 @@ pub struct Options { /// `None`: `read_until` is blocking forever. This is probably not what you want /// /// `Some(millis)`: after millis milliseconds a timeout error is raised - pub timeout_ms: Option, + pub(crate) timeout_ms: Option, /// Whether to filter out escape codes, such as colors. - pub strip_ansi_escape_codes: bool, + pub(crate) strip_ansi_escape_codes: bool, } impl Options { @@ -214,6 +214,7 @@ impl NBReader { /// See [`NBReader::read_until`] /// /// Note that when used with a tty the lines end with \r\n +#[non_exhaustive] pub enum ReadUntil { /// Searches for string (use '\n'.`to_string()` to search for newline). /// @@ -268,7 +269,7 @@ impl fmt::Display for ReadUntil { /// Tuple with match positions: /// 1. position before match (0 in case of EOF and Nbytes) /// 2. position after match -pub fn find(needle: &ReadUntil, buffer: &str, eof: bool) -> Option<(usize, usize)> { +fn find(needle: &ReadUntil, buffer: &str, eof: bool) -> Option<(usize, usize)> { match needle { ReadUntil::String(s) => buffer.find(s).map(|pos| (pos, pos + s.len())), ReadUntil::Regex(pattern) => pattern.find(buffer).map(|mat| (mat.start(), mat.end())), diff --git a/src/session.rs b/src/session.rs index f655d0be..c48ae1e2 100644 --- a/src/session.rs +++ b/src/session.rs @@ -12,8 +12,8 @@ use std::process::Command; use tempfile; pub struct StreamSession { - pub writer: LineWriter, - pub reader: NBReader, + writer: LineWriter, + reader: NBReader, } impl StreamSession { @@ -166,8 +166,8 @@ impl StreamSession { /// Interact with a process with read/write/signals, etc. #[allow(dead_code)] pub struct PtySession { - pub process: PtyProcess, - pub stream: StreamSession, + process: PtyProcess, + stream: StreamSession, } // make StreamSession's methods available directly @@ -273,10 +273,10 @@ pub fn spawn_with_options(command: Command, options: Options) -> Result, - pub echo_on: bool, + pty_session: PtySession, + prompt: String, + quit_command: Option, + echo_on: bool, } impl PtyReplSession { @@ -484,17 +484,15 @@ pub fn spawn_stream( #[cfg(test)] mod tests { use super::*; + use nix::sys::wait; #[test] fn test_read_line() -> Result<(), Error> { let mut s = spawn("cat", Some(100000))?; s.send_line("hans")?; assert_eq!("hans", s.read_line()?); - let should = crate::process::wait::WaitStatus::Signaled( - s.process.child_pid, - crate::process::signal::Signal::SIGTERM, - false, - ); + let should = + wait::WaitStatus::Signaled(s.process.child_pid, crate::process::Signal::SIGTERM, false); assert_eq!(should, s.process.exit()?); Ok(()) }