Skip to main content

brush_core/shell/
env.rs

1//! Environment support for shell.
2
3use std::borrow::Cow;
4
5use crate::{ShellVariable, error};
6
7impl<SE: crate::extensions::ShellExtensions> crate::Shell<SE> {
8    /// Tries to retrieve a variable from the shell's environment, converting it into its
9    /// string form.
10    ///
11    /// # Arguments
12    ///
13    /// * `name` - The name of the variable to retrieve.
14    pub fn env_str(&self, name: &str) -> Option<Cow<'_, str>> {
15        self.env.get_str(name, self)
16    }
17
18    /// Tries to retrieve a variable from the shell's environment.
19    ///
20    /// # Arguments
21    ///
22    /// * `name` - The name of the variable to retrieve.
23    pub fn env_var(&self, name: &str) -> Option<&ShellVariable> {
24        self.env.get(name).map(|(_, var)| var)
25    }
26
27    /// Tries to set a global variable in the shell's environment.
28    ///
29    /// # Arguments
30    ///
31    /// * `name` - The name of the variable to add.
32    /// * `var` - The variable contents to add.
33    pub fn set_env_global(&mut self, name: &str, var: ShellVariable) -> Result<(), error::Error> {
34        self.env.set_global(name, var)
35    }
36}