Skip to main content

brush_core/shell/
state.rs

1//! Defines state traits for the shell.
2
3use std::{
4    borrow::Cow,
5    collections::HashMap,
6    path::{Path, PathBuf},
7};
8
9use crate::{
10    completion, env::ShellEnvironment, jobs, openfiles, options::RuntimeOptions, pathcache,
11    shell::KeyBindingsHelper,
12};
13
14/// A dyn-safe trait for constrained access to shell state.
15pub trait ShellState {
16    /// Returns whether or not this shell is a subshell.
17    fn is_subshell(&self) -> bool;
18
19    /// Returns the last "SECONDS" captured time.
20    fn last_stopwatch_time(&self) -> std::time::SystemTime;
21
22    /// Returns the last "SECONDS" offset requested.
23    fn last_stopwatch_offset(&self) -> u32;
24
25    /// Returns the shell environment containing variables.
26    fn env(&self) -> &ShellEnvironment;
27
28    /// Returns a mutable reference to the shell environment.
29    fn env_mut(&mut self) -> &mut ShellEnvironment;
30
31    /// Returns the shell's runtime options.
32    fn options(&self) -> &RuntimeOptions;
33
34    /// Returns a mutable reference to the shell's runtime options.
35    fn options_mut(&mut self) -> &mut RuntimeOptions;
36
37    /// Returns the shell's aliases.
38    fn aliases(&self) -> &HashMap<String, String>;
39
40    /// Returns a mutable reference to the shell's aliases.
41    fn aliases_mut(&mut self) -> &mut HashMap<String, String>;
42
43    /// Returns the shell's job manager.
44    fn jobs(&self) -> &jobs::JobManager;
45
46    /// Returns a mutable reference to the shell's job manager.
47    fn jobs_mut(&mut self) -> &mut jobs::JobManager;
48
49    /// Returns the shell's trap handler configuration.
50    fn traps(&self) -> &crate::traps::TrapHandlerConfig;
51
52    /// Returns a mutable reference to the shell's trap handler configuration.
53    fn traps_mut(&mut self) -> &mut crate::traps::TrapHandlerConfig;
54
55    /// Returns the shell's directory stack.
56    fn directory_stack(&self) -> &[PathBuf];
57
58    /// Returns a mutable reference to the shell's directory stack.
59    fn directory_stack_mut(&mut self) -> &mut Vec<PathBuf>;
60
61    /// Returns the statuses of commands in the last pipeline.
62    fn last_pipeline_statuses(&self) -> &[u8];
63
64    /// Returns a mutable reference to the statuses of commands in the last pipeline.
65    fn last_pipeline_statuses_mut(&mut self) -> &mut Vec<u8>;
66
67    /// Returns the shell's program location cache.
68    fn program_location_cache(&self) -> &pathcache::PathCache;
69
70    /// Returns a mutable reference to the shell's program location cache.
71    fn program_location_cache_mut(&mut self) -> &mut pathcache::PathCache;
72
73    /// Returns the shell's completion configuration.
74    fn completion_config(&self) -> &completion::Config;
75
76    /// Returns a mutable reference to the shell's completion configuration.
77    fn completion_config_mut(&mut self) -> &mut completion::Config;
78
79    /// Returns the shell's open files.
80    fn open_files(&self) -> &openfiles::OpenFiles;
81
82    /// Returns a mutable reference to the shell's open files.
83    fn open_files_mut(&mut self) -> &mut openfiles::OpenFiles;
84
85    /// Returns the *current* name of the shell ($0).
86    fn current_shell_name(&self) -> Option<Cow<'_, str>>;
87
88    /// Returns the current subshell depth; 0 is returned if this shell is not a subshell.
89    fn depth(&self) -> usize;
90
91    /// Returns the call stack for the shell.
92    fn call_stack(&self) -> &crate::callstack::CallStack;
93
94    /// Returns the shell's history, if it exists.
95    fn history(&self) -> Option<&crate::history::History>;
96
97    /// Returns a mutable reference to the shell's history, if it exists.
98    fn history_mut(&mut self) -> Option<&mut crate::history::History>;
99
100    /// Returns the shell's official version string (if available).
101    fn version(&self) -> Option<&str>;
102
103    /// Returns the exit status of the last command executed in this shell.
104    fn last_exit_status(&self) -> u8;
105
106    /// Updates the last exit status.
107    fn set_last_exit_status(&mut self, status: u8);
108
109    /// Returns the key bindings helper for the shell.
110    fn key_bindings(&self) -> Option<&KeyBindingsHelper>;
111
112    /// Sets the key bindings helper for the shell.
113    fn set_key_bindings(&mut self, key_bindings: Option<KeyBindingsHelper>);
114
115    /// Returns the shell's current working directory.
116    fn working_dir(&self) -> &Path;
117
118    /// Returns a mutable reference to the shell's current working directory.
119    /// This is only accessible within the crate.
120    fn working_dir_mut(&mut self) -> &mut PathBuf;
121
122    /// Returns the product display name for this shell.
123    fn product_display_str(&self) -> Option<&str>;
124}