Struct expectrl::interact::InteractSession
source · pub struct InteractSession<Session, Input, Output> { /* private fields */ }
Expand description
InteractConfig represents options of an interactive session.
Implementations§
source§impl<S, I, O> InteractSession<S, I, O>
impl<S, I, O> InteractSession<S, I, O>
sourcepub fn new(session: S, input: I, output: O) -> InteractSession<S, I, O>
pub fn new(session: S, input: I, output: O) -> InteractSession<S, I, O>
Creates a new object of InteractSession.
sourcepub fn set_escape_character(self, c: u8) -> Self
pub fn set_escape_character(self, c: u8) -> Self
Sets an escape character after seen which the interact interactions will be stopped and controll will be returned to a caller process.
sourcepub fn get_status(&self) -> Option<WaitStatus>
pub fn get_status(&self) -> Option<WaitStatus>
Returns a status of spawned session if it was exited.
If Self::spawn
returns false but this method returns None it means that a child process was shutdown by various reasons.
Which sometimes happens and it’s not considered to be a valid WaitStatus
, so None is returned.
Examples found in repository?
examples/interact_with_callback.rs (line 67)
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
fn main() {
let mut output_action = Lookup::new();
let mut input_action = Lookup::new();
let mut state = State::default();
let opts = InteractOptions::new(&mut state)
.on_output(|mut ctx| {
let m = output_action.on(ctx.buf, ctx.eof, "Continue [y/n]:")?;
if m.is_some() {
ctx.state.wait_for_continue = Some(true);
};
let m = output_action.on(ctx.buf, ctx.eof, Regex("status:\\s*.*\\w+.*\\r\\n"))?;
if m.is_some() {
ctx.state.stutus_verification_counter =
Some(ctx.state.stutus_verification_counter.map_or(1, |c| c + 1));
output_action.clear();
}
Ok(false)
})
.on_input(|mut ctx| {
let m = input_action.on(ctx.buf, ctx.eof, "y")?;
if m.is_some() {
if let Some(_a @ true) = ctx.state.wait_for_continue {
ctx.state.pressed_yes_on_continue = Some(true);
}
};
let m = input_action.on(ctx.buf, ctx.eof, "n")?;
if m.is_some() {
if let Some(_a @ true) = ctx.state.wait_for_continue {
ctx.state.pressed_yes_on_continue = Some(false);
}
}
Ok(false)
});
let mut session = spawn("python ./tests/source/ansi.py").expect("Can't spawn a session");
let mut stdin = Stdin::open().unwrap();
let stdout = std::io::stdout();
let mut interact = session.interact(&mut stdin, stdout);
let is_alive = interact.spawn(opts).expect("Failed to start interact");
if !is_alive {
println!("The process was exited");
#[cfg(unix)]
println!("Status={:?}", interact.get_status());
}
stdin.close().unwrap();
println!("RESULTS");
println!(
"Number of time 'Y' was pressed = {}",
state.pressed_yes_on_continue.unwrap_or_default()
);
println!(
"Status counter = {}",
state.stutus_verification_counter.unwrap_or_default()
);
}
source§impl<S, I, O> InteractSession<&mut Session<OsProcess, S>, I, O>where
I: Read,
O: Write,
S: NonBlocking + Write + Read,
impl<S, I, O> InteractSession<&mut Session<OsProcess, S>, I, O>where I: Read, O: Write, S: NonBlocking + Write + Read,
sourcepub fn spawn<C, IF, OF, IA, OA, WA, OPS>(
&mut self,
ops: OPS
) -> Result<bool, Error>where
OPS: BorrowMut<InteractOptions<C, IF, OF, IA, OA, WA>>,
IF: FnMut(&[u8]) -> Result<Cow<'_, [u8]>, Error>,
OF: FnMut(&[u8]) -> Result<Cow<'_, [u8]>, Error>,
IA: FnMut(Context<'_, Session<OsProcess, S>, I, O, C>) -> Result<bool, Error>,
OA: FnMut(Context<'_, Session<OsProcess, S>, I, O, C>) -> Result<bool, Error>,
WA: FnMut(Context<'_, Session<OsProcess, S>, I, O, C>) -> Result<bool, Error>,
pub fn spawn<C, IF, OF, IA, OA, WA, OPS>( &mut self, ops: OPS ) -> Result<bool, Error>where OPS: BorrowMut<InteractOptions<C, IF, OF, IA, OA, WA>>, IF: FnMut(&[u8]) -> Result<Cow<'_, [u8]>, Error>, OF: FnMut(&[u8]) -> Result<Cow<'_, [u8]>, Error>, IA: FnMut(Context<'_, Session<OsProcess, S>, I, O, C>) -> Result<bool, Error>, OA: FnMut(Context<'_, Session<OsProcess, S>, I, O, C>) -> Result<bool, Error>, WA: FnMut(Context<'_, Session<OsProcess, S>, I, O, C>) -> Result<bool, Error>,
Runs the session.
See Session::interact
.
Examples found in repository?
examples/interact.rs (line 23)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
fn main() {
let mut sh = spawn(SHELL).expect("Error while spawning sh");
println!("Now you're in interacting mode");
println!("To return control back to main type CTRL-] combination");
let mut stdin = Stdin::open().expect("Failed to create stdin");
sh.interact(&mut stdin, stdout())
.spawn(&mut InteractOptions::default())
.expect("Failed to start interact");
stdin.close().expect("Failed to close a stdin");
println!("Exiting");
}
More examples
examples/ftp_interact.rs (line 29)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
fn main() -> Result<(), Error> {
let mut auth = false;
let mut login_lookup = Lookup::new();
let opts = InteractOptions::new(&mut auth).on_output(|ctx| {
if login_lookup
.on(ctx.buf, ctx.eof, "Login successful")?
.is_some()
{
**ctx.state = true;
return Ok(true);
}
Ok(false)
});
let mut p = spawn("ftp bks4-speedtest-1.tele2.net")?;
let mut stdin = Stdin::open()?;
p.interact(&mut stdin, stdout()).spawn(opts)?;
stdin.close()?;
if !auth {
println!("An authefication was not passed");
return Ok(());
}
p.expect("ftp>")?;
p.send_line("cd upload")?;
p.expect("successfully changed.")?;
p.send_line("pwd")?;
p.expect(Regex("[0-9]+ \"/upload\""))?;
p.send(ControlCode::EndOfTransmission)?;
p.expect("Goodbye.")?;
Ok(())
}
examples/interact_with_callback.rs (line 62)
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
fn main() {
let mut output_action = Lookup::new();
let mut input_action = Lookup::new();
let mut state = State::default();
let opts = InteractOptions::new(&mut state)
.on_output(|mut ctx| {
let m = output_action.on(ctx.buf, ctx.eof, "Continue [y/n]:")?;
if m.is_some() {
ctx.state.wait_for_continue = Some(true);
};
let m = output_action.on(ctx.buf, ctx.eof, Regex("status:\\s*.*\\w+.*\\r\\n"))?;
if m.is_some() {
ctx.state.stutus_verification_counter =
Some(ctx.state.stutus_verification_counter.map_or(1, |c| c + 1));
output_action.clear();
}
Ok(false)
})
.on_input(|mut ctx| {
let m = input_action.on(ctx.buf, ctx.eof, "y")?;
if m.is_some() {
if let Some(_a @ true) = ctx.state.wait_for_continue {
ctx.state.pressed_yes_on_continue = Some(true);
}
};
let m = input_action.on(ctx.buf, ctx.eof, "n")?;
if m.is_some() {
if let Some(_a @ true) = ctx.state.wait_for_continue {
ctx.state.pressed_yes_on_continue = Some(false);
}
}
Ok(false)
});
let mut session = spawn("python ./tests/source/ansi.py").expect("Can't spawn a session");
let mut stdin = Stdin::open().unwrap();
let stdout = std::io::stdout();
let mut interact = session.interact(&mut stdin, stdout);
let is_alive = interact.spawn(opts).expect("Failed to start interact");
if !is_alive {
println!("The process was exited");
#[cfg(unix)]
println!("Status={:?}", interact.get_status());
}
stdin.close().unwrap();
println!("RESULTS");
println!(
"Number of time 'Y' was pressed = {}",
state.pressed_yes_on_continue.unwrap_or_default()
);
println!(
"Status counter = {}",
state.stutus_verification_counter.unwrap_or_default()
);
}
Trait Implementations§
Auto Trait Implementations§
impl<Session, Input, Output> RefUnwindSafe for InteractSession<Session, Input, Output>where Input: RefUnwindSafe, Output: RefUnwindSafe, Session: RefUnwindSafe,
impl<Session, Input, Output> Send for InteractSession<Session, Input, Output>where Input: Send, Output: Send, Session: Send,
impl<Session, Input, Output> Sync for InteractSession<Session, Input, Output>where Input: Sync, Output: Sync, Session: Sync,
impl<Session, Input, Output> Unpin for InteractSession<Session, Input, Output>where Input: Unpin, Output: Unpin, Session: Unpin,
impl<Session, Input, Output> UnwindSafe for InteractSession<Session, Input, Output>where Input: UnwindSafe, Output: UnwindSafe, Session: 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