ShellState

Struct ShellState 

Source
pub struct ShellState {
Show 44 fields pub variables: HashMap<String, String>, pub exported: HashSet<String>, pub last_exit_code: i32, pub shell_pid: u32, pub script_name: String, pub dir_stack: Vec<String>, pub aliases: HashMap<String, String>, pub colors_enabled: bool, pub color_scheme: ColorScheme, pub positional_params: Vec<String>, pub functions: HashMap<String, Ast>, pub local_vars: Vec<HashMap<String, String>>, pub function_depth: usize, pub max_recursion_depth: usize, pub returning: bool, pub return_value: Option<i32>, pub loop_depth: usize, pub breaking: bool, pub break_level: usize, pub continuing: bool, pub continue_level: usize, pub capture_output: Option<Rc<RefCell<Vec<u8>>>>, pub condensed_cwd: bool, pub trap_handlers: Arc<Mutex<HashMap<String, String>>>, pub exit_trap_executed: bool, pub exit_requested: bool, pub exit_code: i32, pub pending_signals: bool, pub pending_heredoc_content: Option<String>, pub collecting_heredoc: Option<(String, String, String)>, pub fd_table: Rc<RefCell<FileDescriptorTable>>, pub subshell_depth: usize, pub stdin_override: Option<RawFd>, pub options: ShellOptions, pub in_condition: bool, pub in_logical_chain: bool, pub in_negation: bool, pub last_was_negation: bool, pub current_line_number: usize, pub line_number_stack: Vec<usize>, pub job_table: Rc<RefCell<JobTable>>, pub interactive: bool, pub terminal_fd: Option<RawFd>, pub last_background_pid: Option<u32>,
}

Fields§

§variables: HashMap<String, String>

Shell variables (local to the shell session)

§exported: HashSet<String>

Which variables are exported to child processes

§last_exit_code: i32

Last exit code ($?)

§shell_pid: u32

Shell process ID ($$)

§script_name: String

Script name or command ($0)

§dir_stack: Vec<String>

Directory stack for pushd/popd

§aliases: HashMap<String, String>

Command aliases

§colors_enabled: bool

Whether colors are enabled

§color_scheme: ColorScheme

Current color scheme

§positional_params: Vec<String>

Positional parameters ($1, $2, $3, …)

§functions: HashMap<String, Ast>

Function definitions

§local_vars: Vec<HashMap<String, String>>

Local variable stack for function scoping

§function_depth: usize

Function call depth for local scope management

§max_recursion_depth: usize

Maximum allowed recursion depth

§returning: bool

Flag to indicate if we’re currently returning from a function

§return_value: Option<i32>

Return value when returning from a function

§loop_depth: usize

Loop nesting depth for break/continue

§breaking: bool

Flag to indicate if we’re breaking out of a loop

§break_level: usize

Number of loop levels to break out of

§continuing: bool

Flag to indicate if we’re continuing to next loop iteration

§continue_level: usize

Number of loop levels to continue from

§capture_output: Option<Rc<RefCell<Vec<u8>>>>

Output capture buffer for command substitution

§condensed_cwd: bool

Whether to use condensed cwd display in prompt

§trap_handlers: Arc<Mutex<HashMap<String, String>>>

Signal trap handlers: maps signal name to command string

§exit_trap_executed: bool

Flag to track if EXIT trap has been executed

§exit_requested: bool

Flag to indicate that the shell should exit

§exit_code: i32

Exit code to use when exiting

§pending_signals: bool

Flag to indicate pending signals need processing Set by signal handler, checked by executor

§pending_heredoc_content: Option<String>

Pending here-document content from script execution

§collecting_heredoc: Option<(String, String, String)>

Interactive mode heredoc collection state

§fd_table: Rc<RefCell<FileDescriptorTable>>

File descriptor table for managing open file descriptors

§subshell_depth: usize

Current subshell nesting depth (for recursion limit)

§stdin_override: Option<RawFd>

Override for stdin (used for pipeline subshells to avoid process-global fd manipulation)

§options: ShellOptions

Shell option flags (set builtin)

§in_condition: bool

Context tracking for errexit option - true when executing commands in if/while/until conditions

§in_logical_chain: bool

Context tracking for errexit option - true when executing commands in && or || chains

§in_negation: bool

Context tracking for errexit option - true when executing negated commands (!)

§last_was_negation: bool

Track if the last command executed was a negation (to skip errexit check on inverted code)

§current_line_number: usize

Current line number in script execution (for $LINENO)

§line_number_stack: Vec<usize>

Stack of line numbers for function calls (to restore after function returns)

§job_table: Rc<RefCell<JobTable>>

Job table for managing background jobs

§interactive: bool

Whether the shell is running in interactive mode

§terminal_fd: Option<RawFd>

Terminal file descriptor for job control (None if not interactive)

§last_background_pid: Option<u32>

PID of the last background process started ($!)

Implementations§

Source§

impl ShellState

Source

pub fn new() -> Self

Creates a new ShellState initialized with sensible defaults and environment-derived settings.

The returned state initializes runtime fields (variables, exported, aliases, positional params, function/local scopes, FD table, traps, and control flags) and derives display preferences from environment:

  • colors_enabled is determined by NO_COLOR, RUSH_COLORS, and whether stdout is a terminal.
  • condensed_cwd is determined by RUSH_CONDENSED (defaults to true).
§Examples
use rush_sh::ShellState;
let state = ShellState::new();
// basic invariants
assert_eq!(state.last_exit_code, 0);
assert!(state.shell_pid != 0);
Source

pub fn get_var(&self, name: &str) -> Option<String>

Get a variable value, checking local scopes first, then shell variables, then environment

Source

pub fn set_var(&mut self, name: &str, value: String)

Set a shell variable (updates local scope if variable exists there, otherwise sets globally)

Source

pub fn unset_var(&mut self, name: &str)

Remove a shell variable

Source

pub fn export_var(&mut self, name: &str)

Mark a variable as exported

Source

pub fn set_exported_var(&mut self, name: &str, value: String)

Set and export a variable

Source

pub fn get_env_for_child(&self) -> HashMap<String, String>

Get all environment variables for child processes (exported + inherited)

Source

pub fn set_last_exit_code(&mut self, code: i32)

Update the last exit code

Source

pub fn set_script_name(&mut self, name: &str)

Set the script name ($0)

Source

pub fn get_condensed_cwd(&self) -> String

Get the condensed current working directory for the prompt

Source

pub fn get_full_cwd(&self) -> String

Get the full current working directory for the prompt

Source

pub fn get_user_hostname(&self) -> String

Get the user@hostname string for the prompt

Source

pub fn get_prompt(&self) -> String

Get the full prompt string

Source

pub fn set_alias(&mut self, name: &str, value: String)

Set an alias

Source

pub fn get_alias(&self, name: &str) -> Option<&String>

Get an alias value

Source

pub fn remove_alias(&mut self, name: &str)

Remove an alias

Source

pub fn get_all_aliases(&self) -> &HashMap<String, String>

Get all aliases

Source

pub fn set_positional_params(&mut self, params: Vec<String>)

Set positional parameters

Source

pub fn get_positional_params(&self) -> &[String]

Get positional parameters

Source

pub fn shift_positional_params(&mut self, count: usize)

Shift positional parameters (remove first n parameters)

Source

pub fn push_positional_param(&mut self, param: String)

Add a positional parameter at the end

Source

pub fn define_function(&mut self, name: String, body: Ast)

Define a function

Source

pub fn get_function(&self, name: &str) -> Option<&Ast>

Get a function definition

Source

pub fn remove_function(&mut self, name: &str)

Remove a function definition

Source

pub fn get_function_names(&self) -> Vec<&String>

Get all function names

Source

pub fn push_local_scope(&mut self)

Push a new local variable scope

Source

pub fn pop_local_scope(&mut self)

Pop the current local variable scope

Source

pub fn set_local_var(&mut self, name: &str, value: String)

Set a local variable in the current scope

Source

pub fn enter_function(&mut self)

Enter a function context (push local scope if needed)

Source

pub fn exit_function(&mut self)

Exit a function context (pop local scope if needed)

Source

pub fn set_return(&mut self, value: i32)

Set return state for function returns

Source

pub fn clear_return(&mut self)

Clear return state

Source

pub fn is_returning(&self) -> bool

Check if currently returning

Source

pub fn get_return_value(&self) -> Option<i32>

Get return value if returning

Source

pub fn enter_loop(&mut self)

Enter a loop context (increment loop depth)

Source

pub fn exit_loop(&mut self)

Exit a loop context (decrement loop depth)

Source

pub fn set_break(&mut self, level: usize)

Set break state for loop control

Source

pub fn clear_break(&mut self)

Clear break state

Source

pub fn is_breaking(&self) -> bool

Check if currently breaking

Source

pub fn get_break_level(&self) -> usize

Get break level

Source

pub fn decrement_break_level(&mut self)

Decrement break level (when exiting a loop level)

Source

pub fn set_continue(&mut self, level: usize)

Set continue state for loop control

Source

pub fn clear_continue(&mut self)

Clear continue state

Source

pub fn is_continuing(&self) -> bool

Check if currently continuing

Source

pub fn get_continue_level(&self) -> usize

Get continue level

Source

pub fn decrement_continue_level(&mut self)

Decrement continue level (when exiting a loop level)

Source

pub fn set_trap(&mut self, signal: &str, command: String)

Set a trap handler for a signal

Source

pub fn get_trap(&self, signal: &str) -> Option<String>

Get a trap handler for a signal

Source

pub fn remove_trap(&mut self, signal: &str)

Remove a trap handler for a signal

Source

pub fn get_all_traps(&self) -> HashMap<String, String>

Get all trap handlers

Source

pub fn clear_traps(&mut self)

Clear all trap handlers

Trait Implementations§

Source§

impl Clone for ShellState

Source§

fn clone(&self) -> ShellState

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

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ShellState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ShellState

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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, 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> 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.