Struct term_transcript::ShellOptions
source · pub struct ShellOptions<Cmd = Command> { /* private fields */ }
Expand description
Options for executing commands in the shell. Used in Transcript::from_inputs()
and in TestConfig
.
The type param maps to extensions available for the shell. For example, StdShell
extension allows to specify custom aliases for the executed commands.
Implementations§
source§impl ShellOptions<StdShell>
impl ShellOptions<StdShell>
sourcepub fn with_alias(self, name: &str, path_to_bin: &str) -> Self
pub fn with_alias(self, name: &str, path_to_bin: &str) -> Self
Creates an alias for the binary at path_to_bin
, which should be an absolute path.
This allows to call the binary using this alias without complex preparations (such as
installing it globally via cargo install
), and is more flexible than
Self::with_cargo_path()
.
In integration tests, you may use env!("CARGO_BIN_EXE_<name>")
to get a path
to binary targets.
§Limitations
- For Bash and PowerShell,
name
must be a valid name of a function. Forsh
,name
must be a valid name for thealias
command. Thename
validity is not checked.
source§impl<Cmd: ConfigureCommand> ShellOptions<Cmd>
impl<Cmd: ConfigureCommand> ShellOptions<Cmd>
sourcepub fn echoing(self, is_echoing: bool) -> ShellOptions<Echoing<Cmd>>
pub fn echoing(self, is_echoing: bool) -> ShellOptions<Echoing<Cmd>>
Sets the echoing flag for the shell.
sourcepub fn with_current_dir(self, current_dir: impl AsRef<Path>) -> Self
pub fn with_current_dir(self, current_dir: impl AsRef<Path>) -> Self
Changes the current directory of the command.
sourcepub fn with_io_timeout(self, io_timeout: Duration) -> Self
pub fn with_io_timeout(self, io_timeout: Duration) -> Self
Sets the I/O timeout for shell commands. This determines how long methods like
Transcript::from_inputs()
wait
for output of a command before proceeding to the next command. Longer values
allow to capture output more accurately, but result in longer execution.
By default, the I/O timeout is 0.5 seconds.
sourcepub fn with_init_timeout(self, init_timeout: Duration) -> Self
pub fn with_init_timeout(self, init_timeout: Duration) -> Self
Sets an additional initialization timeout (relative to the one set by
Self::with_io_timeout()
) before reading the output of the each command
input into the shell.
By default, the initialization timeout is 1.5 seconds.
sourcepub fn with_init_command(self, command: impl Into<String>) -> Self
pub fn with_init_command(self, command: impl Into<String>) -> Self
Adds an initialization command. Such commands are sent to the shell before executing any user input. The corresponding output from the shell is not captured.
sourcepub fn with_env(self, name: impl AsRef<str>, value: impl AsRef<OsStr>) -> Self
pub fn with_env(self, name: impl AsRef<str>, value: impl AsRef<OsStr>) -> Self
Sets the value
of an environment variable with the specified name
.
sourcepub fn with_line_decoder<E, F>(self, mapper: F) -> Self
pub fn with_line_decoder<E, F>(self, mapper: F) -> Self
Sets the line decoder for the shell. This allows for custom shell text encodings.
The default decoder used is the UTF-8 one.
It halts processing with an error if the input is not UTF-8;
you may use Self::with_lossy_utf8_decoder()
to swallow errors in this case.
sourcepub fn with_lossy_utf8_decoder(self) -> Self
pub fn with_lossy_utf8_decoder(self) -> Self
Sets the lossy UTF-8 decoder which always succeeds at decoding at the cost of replacing non-UTF-8 chars.
sourcepub fn with_status_check<F>(
self,
command: impl Into<String>,
checker: F,
) -> Self
pub fn with_status_check<F>( self, command: impl Into<String>, checker: F, ) -> Self
Sets the ExitStatus
checker for the shell. See ExitStatus
docs for the semantics
of exit statuses.
§Arguments
command
is a command that will be executed to check the exit status of the latest executed command. E.g., insh
-like shells one can useecho $?
.checker
is a closure that transforms the output ofcommand
into anExitStatus
. The output is provided as aCaptured
string; it usually makes sense to useCaptured::to_plaintext()
to strip it of possible escape sequences (especially important if captured from PTY). If the exit status is inconclusive or not applicable, the closure should returnNone
.
The command
will be executed after each UserInput
is input into the shell and
the corresponding output is captured. After this, the Captured
output will be supplied to the checker
closure and its output will be recorded as
Interaction::exit_status()
.
§Panics
Panics if command
contains newline chars ('\n'
or '\r'
).
sourcepub fn with_cargo_path(self) -> Self
pub fn with_cargo_path(self) -> Self
Adds paths to cargo binaries (including examples) to the PATH
env variable
for the shell described by these options.
This allows to call them by the corresponding filename, without specifying a path
or doing complex preparations (e.g., calling cargo install
).
§Limitations
- The caller must be a unit or integration test; the method will work improperly otherwise.
sourcepub fn with_additional_path(self, path: impl Into<PathBuf>) -> Self
pub fn with_additional_path(self, path: impl Into<PathBuf>) -> Self
Adds a specified path to the PATH
env variable for the shell described by these options.
This method can be called multiple times to add multiple paths and is composable
with Self::with_cargo_path()
.
Trait Implementations§
source§impl<Cmd: Debug> Debug for ShellOptions<Cmd>
impl<Cmd: Debug> Debug for ShellOptions<Cmd>
source§impl Default for ShellOptions
impl Default for ShellOptions
source§impl<Cmd: ConfigureCommand> From<Cmd> for ShellOptions<Cmd>
impl<Cmd: ConfigureCommand> From<Cmd> for ShellOptions<Cmd>
Auto Trait Implementations§
impl<Cmd> Freeze for ShellOptions<Cmd>where
Cmd: Freeze,
impl<Cmd = Command> !RefUnwindSafe for ShellOptions<Cmd>
impl<Cmd = Command> !Send for ShellOptions<Cmd>
impl<Cmd = Command> !Sync for ShellOptions<Cmd>
impl<Cmd> Unpin for ShellOptions<Cmd>where
Cmd: Unpin,
impl<Cmd = Command> !UnwindSafe for ShellOptions<Cmd>
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
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
§Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling [Attribute
] value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
§Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
[Quirk
] value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the [Condition
] value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);