Struct Shell

Source
pub struct Shell {
Show 16 fields pub traps: TrapHandlerConfig, pub open_files: OpenFiles, pub working_dir: PathBuf, pub env: ShellEnvironment, pub funcs: FunctionEnv, pub options: RuntimeOptions, pub aliases: HashMap<String, String>, pub last_exit_status: u8, pub last_pipeline_statuses: Vec<u8>, pub shell_name: Option<String>, pub positional_parameters: Vec<String>, pub shell_product_display_str: Option<String>, pub directory_stack: Vec<PathBuf>, pub completion_config: Config, pub builtins: HashMap<String, Registration>, pub key_bindings: Option<Arc<Mutex<dyn KeyBindings>>>, /* private fields */
}
Expand description

Represents an instance of a shell.

Fields§

§traps: TrapHandlerConfig

Trap handler configuration for the shell.

§open_files: OpenFiles

Manages files opened and accessible via redirection operators.

§working_dir: PathBuf

The current working directory.

§env: ShellEnvironment

The shell environment, containing shell variables.

§funcs: FunctionEnv

Shell function definitions.

§options: RuntimeOptions

Runtime shell options.

§aliases: HashMap<String, String>

Shell aliases.

§last_exit_status: u8

The status of the last completed command.

§last_pipeline_statuses: Vec<u8>

The status of each of the commands in the last pipeline.

§shell_name: Option<String>

Shell name (a.k.a. $0)

§positional_parameters: Vec<String>

Positional parameters stack ($1 and beyond)

§shell_product_display_str: Option<String>

Detailed display string for the shell

§directory_stack: Vec<PathBuf>

Directory stack used by pushd et al.

§completion_config: Config

Completion configuration.

§builtins: HashMap<String, Registration>

Shell built-in commands.

§key_bindings: Option<Arc<Mutex<dyn KeyBindings>>>

Key bindings for the shell, optionally implemented by an interactive shell.

Implementations§

Source§

impl Shell

Source

pub async fn new(options: &CreateOptions) -> Result<Self, Error>

Returns a new shell instance created with the given options.

§Arguments
  • options - The options to use when creating the shell.
Source

pub async fn source_script<S: AsRef<str>, P: AsRef<Path>, I: Iterator<Item = S>>( &mut self, path: P, args: I, params: &ExecutionParameters, ) -> Result<ExecutionResult, Error>

Source the given file as a shell script, returning the execution result.

§Arguments
  • path - The path to the file to source.
  • args - The arguments to pass to the script as positional parameters.
  • params - Execution parameters.
Source

pub async fn invoke_function<N: AsRef<str>, I: IntoIterator<Item = A>, A: AsRef<str>>( &mut self, name: N, args: I, params: &ExecutionParameters, ) -> Result<u8, Error>

Invokes a function defined in this shell, returning the resulting exit status.

§Arguments
  • name - The name of the function to invoke.
  • args - The arguments to pass to the function.
  • params - Execution parameters to use for the invocation.
Source

pub async fn run_string<S: Into<String>>( &mut self, command: S, params: &ExecutionParameters, ) -> Result<ExecutionResult, Error>

Executes the given string as a shell program, returning the resulting exit status.

§Arguments
  • command - The command to execute.
  • params - Execution parameters.
Source

pub fn parse<R: Read>(&self, reader: R) -> Result<Program, ParseError>

Parses the given reader as a shell program, returning the resulting Abstract Syntax Tree for the program.

Source

pub fn parse_string<S: Into<String>>(&self, s: S) -> Result<Program, ParseError>

Parses the given string as a shell program, returning the resulting Abstract Syntax Tree for the program.

§Arguments
  • s - The string to parse as a program.
Source

pub async fn basic_expand_string<S: AsRef<str>>( &mut self, params: &ExecutionParameters, s: S, ) -> Result<String, Error>

Applies basic shell expansion to the provided string.

§Arguments
  • s - The string to expand.
Source

pub async fn full_expand_and_split_string<S: AsRef<str>>( &mut self, params: &ExecutionParameters, s: S, ) -> Result<Vec<String>, Error>

Applies full shell expansion and field splitting to the provided string; returns a sequence of fields.

§Arguments
  • s - The string to expand and split.
Source

pub fn default_exec_params(&self) -> ExecutionParameters

Returns the default execution parameters for this shell.

Source

pub async fn run_script<S: AsRef<str>, P: AsRef<Path>, I: Iterator<Item = S>>( &mut self, script_path: P, args: I, ) -> Result<ExecutionResult, Error>

Executes the given script file, returning the resulting exit status.

§Arguments
  • script_path - The path to the script file to execute.
  • args - The arguments to pass to the script as positional parameters.
Source

pub async fn run_program( &mut self, program: Program, params: &ExecutionParameters, ) -> Result<ExecutionResult, Error>

Executes the given parsed shell program, returning the resulting exit status.

§Arguments
  • program - The program to execute.
  • params - Execution parameters.
Source

pub async fn compose_precmd_prompt(&mut self) -> Result<String, Error>

Composes the shell’s post-input, pre-command prompt, applying all appropriate expansions.

Source

pub async fn compose_prompt(&mut self) -> Result<String, Error>

Composes the shell’s prompt, applying all appropriate expansions.

Source

pub async fn compose_alt_side_prompt(&mut self) -> Result<String, Error>

Compose’s the shell’s alternate-side prompt, applying all appropriate expansions.

Source

pub async fn compose_continuation_prompt(&mut self) -> Result<String, Error>

Composes the shell’s continuation prompt.

Source

pub const fn last_result(&self) -> u8

Returns the exit status of the last command executed in this shell.

Source

pub const fn parser_options(&self) -> ParserOptions

Returns the options that should be used for parsing shell programs; reflects the current configuration state of the shell and may change over time.

Source

pub fn get_history_file_path(&self) -> Option<PathBuf>

Returns the path to the history file used by the shell, if one is set.

Source

pub fn get_env_str(&self, name: &str) -> Option<Cow<'_, str>>

Tries to retrieve a variable from the shell’s environment, converting it into its string form.

§Arguments
  • name - The name of the variable to retrieve.
Source

pub fn get_env_var(&self, name: &str) -> Option<&ShellVariable>

Tries to retrieve a variable from the shell’s environment.

§Arguments
  • name - The name of the variable to retrieve.
Source

pub fn set_env_global( &mut self, name: &str, var: ShellVariable, ) -> Result<(), Error>

Tries to set a global variable in the shell’s environment.

§Arguments
  • name - The name of the variable to add.
  • var - The variable contents to add.
Source

pub fn register_builtin<S: Into<String>>( &mut self, name: S, registration: Registration, )

Register a builtin to the shell’s environment.

§Arguments
  • name - The in-shell name of the builtin.
  • registration - The registration handle for the builtin.
Source

pub async fn get_completions( &mut self, input: &str, position: usize, ) -> Result<Completions, Error>

Generates command completions for the shell.

§Arguments
  • input - The input string to generate completions for.
  • position - The position in the input string to generate completions at.
Source

pub fn find_executables_in_path<'a>( &'a self, filename: &'a str, ) -> impl Iterator<Item = PathBuf> + 'a

Finds executables in the shell’s current default PATH, matching the given glob pattern.

§Arguments
  • required_glob_pattern - The glob pattern to match against.
Source

pub fn find_executables_in_path_with_prefix( &self, filename_prefix: &str, case_insensitive: bool, ) -> impl Iterator<Item = PathBuf>

Finds executables in the shell’s current default PATH, with filenames matching the given prefix.

§Arguments
  • filename_prefix - The prefix to match against executable filenames.
Source

pub fn find_first_executable_in_path<S: AsRef<str>>( &self, candidate_name: S, ) -> Option<PathBuf>

Determines whether the given filename is the name of an executable in one of the directories in the shell’s current PATH. If found, returns the path.

§Arguments
  • candidate_name - The name of the file to look for.
Source

pub fn find_first_executable_in_path_using_cache<S: AsRef<str>>( &mut self, candidate_name: S, ) -> Option<PathBuf>

Uses the shell’s hash-based path cache to check whether the given filename is the name of an executable in one of the directories in the shell’s current PATH. If found, ensures the path is in the cache and returns it.

§Arguments
  • candidate_name - The name of the file to look for.
Source

pub fn get_absolute_path(&self, path: impl AsRef<Path>) -> PathBuf

Gets the absolute form of the given path.

§Arguments
  • path - The path to get the absolute form of.
Source

pub fn set_working_dir( &mut self, target_dir: impl AsRef<Path>, ) -> Result<(), Error>

Sets the shell’s current working directory to the given path.

§Arguments
  • target_dir - The path to set as the working directory.
Source

pub fn stdout(&self) -> impl Write

Returns a value that can be used to write to the shell’s currently configured standard output stream using write! at al.

Source

pub fn stderr(&self) -> impl Write

Returns a value that can be used to write to the shell’s currently configured standard error stream using write! et al.

Source

pub fn is_keyword(&self, s: &str) -> bool

Checks if the given string is a keyword reserved in this shell.

§Arguments
  • s - The string to check.
Source

pub fn check_for_completed_jobs(&mut self) -> Result<(), Error>

Checks for completed jobs in the shell, reporting any changes found.

Source

pub fn eval_arithmetic(&mut self, expr: &ArithmeticExpr) -> Result<i64, Error>

Evaluate the given arithmetic expression, returning the result.

Source

pub fn set_edit_buffer( &mut self, contents: String, cursor: usize, ) -> Result<(), Error>

Updates the shell state to reflect the given edit buffer contents.

§Arguments
  • contents - The contents of the edit buffer.
  • cursor - The cursor position in the edit buffer.
Source

pub fn pop_edit_buffer(&mut self) -> Result<Option<(String, usize)>, Error>

Returns the contents of the shell’s edit buffer, if any. The buffer state is cleared from the shell.

Trait Implementations§

Source§

impl AsMut<Shell> for Shell

Source§

fn as_mut(&mut self) -> &mut Self

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsRef<Shell> for Shell

Source§

fn as_ref(&self) -> &Self

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Shell

Source§

fn clone(&self) -> Self

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

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

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl Freeze for Shell

§

impl !RefUnwindSafe for Shell

§

impl Send for Shell

§

impl Sync for Shell

§

impl Unpin for Shell

§

impl !UnwindSafe for Shell

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more