shrub_core/shell/state.rs
1//! Defines state traits for the shell.
2
3use std::borrow::Cow;
4use std::collections::HashMap;
5use std::path::{Path, PathBuf};
6
7use crate::env::ShellEnvironment;
8use crate::options::RuntimeOptions;
9use crate::shell::KeyBindingsHelper;
10use crate::{completion, jobs, openfiles, pathcache};
11
12/// A dyn-safe trait for constrained access to shell state.
13pub trait ShellState {
14 /// Returns whether or not this shell is a subshell.
15 fn is_subshell(&self) -> bool;
16
17 /// Returns the last "SECONDS" captured time.
18 fn last_stopwatch_time(&self) -> std::time::SystemTime;
19
20 /// Returns the last "SECONDS" offset requested.
21 fn last_stopwatch_offset(&self) -> u32;
22
23 /// Returns the shell environment containing variables.
24 fn env(&self) -> &ShellEnvironment;
25
26 /// Returns a mutable reference to the shell environment.
27 fn env_mut(&mut self) -> &mut ShellEnvironment;
28
29 /// Returns the shell's runtime options.
30 fn options(&self) -> &RuntimeOptions;
31
32 /// Returns a mutable reference to the shell's runtime options.
33 fn options_mut(&mut self) -> &mut RuntimeOptions;
34
35 /// Returns the shell's aliases.
36 fn aliases(&self) -> &HashMap<String, String>;
37
38 /// Returns a mutable reference to the shell's aliases.
39 fn aliases_mut(&mut self) -> &mut HashMap<String, String>;
40
41 /// Returns the shell's job manager.
42 fn jobs(&self) -> &jobs::JobManager;
43
44 /// Returns a mutable reference to the shell's job manager.
45 fn jobs_mut(&mut self) -> &mut jobs::JobManager;
46
47 /// Returns the shell's trap handler configuration.
48 fn traps(&self) -> &crate::traps::TrapHandlerConfig;
49
50 /// Returns a mutable reference to the shell's trap handler configuration.
51 fn traps_mut(&mut self) -> &mut crate::traps::TrapHandlerConfig;
52
53 /// Returns the shell's directory stack.
54 fn directory_stack(&self) -> &[PathBuf];
55
56 /// Returns a mutable reference to the shell's directory stack.
57 fn directory_stack_mut(&mut self) -> &mut Vec<PathBuf>;
58
59 /// Returns the statuses of commands in the last pipeline.
60 fn last_pipeline_statuses(&self) -> &[u8];
61
62 /// Returns a mutable reference to the statuses of commands in the last
63 /// pipeline.
64 fn last_pipeline_statuses_mut(&mut self) -> &mut Vec<u8>;
65
66 /// Returns the shell's program location cache.
67 fn program_location_cache(&self) -> &pathcache::PathCache;
68
69 /// Returns a mutable reference to the shell's program location cache.
70 fn program_location_cache_mut(&mut self) -> &mut pathcache::PathCache;
71
72 /// Returns the shell's completion configuration.
73 fn completion_config(&self) -> &completion::Config;
74
75 /// Returns a mutable reference to the shell's completion configuration.
76 fn completion_config_mut(&mut self) -> &mut completion::Config;
77
78 /// Returns the shell's open files.
79 fn open_files(&self) -> &openfiles::OpenFiles;
80
81 /// Returns a mutable reference to the shell's open files.
82 fn open_files_mut(&mut self) -> &mut openfiles::OpenFiles;
83
84 /// Returns the *current* name of the shell ($0).
85 fn current_shell_name(&self) -> Option<Cow<'_, str>>;
86
87 /// Returns the current subshell depth; 0 is returned if this shell is not a
88 /// 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}