Skip to main content

brush_core/shell/
expansion.rs

1//! Expansion support for shell instances.
2
3use std::borrow::Cow;
4
5use crate::{error, expansion, extensions, interp::ExecutionParameters};
6
7impl<SE: extensions::ShellExtensions> crate::Shell<SE> {
8    /// Returns the current value of the IFS variable, or the default value if it is not set.
9    pub fn ifs(&self) -> Cow<'_, str> {
10        self.env_str("IFS").unwrap_or_else(|| " \t\n".into())
11    }
12
13    /// Returns the first character of the IFS variable, or a space if it is not set.
14    pub(crate) fn get_ifs_first_char(&self) -> char {
15        self.ifs().chars().next().unwrap_or(' ')
16    }
17
18    /// Applies basic shell expansion to the provided string.
19    ///
20    /// # Arguments
21    ///
22    /// * `s` - The string to expand.
23    pub async fn basic_expand_string<S: AsRef<str>>(
24        &mut self,
25        params: &ExecutionParameters,
26        s: S,
27    ) -> Result<String, error::Error> {
28        let result = expansion::basic_expand_word(self, params, s.as_ref()).await?;
29        Ok(result)
30    }
31
32    /// Applies full shell expansion and field splitting to the provided string; returns
33    /// a sequence of fields.
34    ///
35    /// # Arguments
36    ///
37    /// * `s` - The string to expand and split.
38    pub async fn full_expand_and_split_string<S: AsRef<str>>(
39        &mut self,
40        params: &ExecutionParameters,
41        s: S,
42    ) -> Result<Vec<String>, error::Error> {
43        let result = expansion::full_expand_and_split_word(self, params, s.as_ref()).await?;
44        Ok(result)
45    }
46}