summaryrefslogtreecommitdiffstats
path: root/src/verb/internal_execution.rs
blob: 740d2b549a229d000f054c77aacdd64f244cdea7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use {
    super::*,
    crate::errors::ConfError,
    std::fmt,
};

/// A verb execution definition based on an internal
#[derive(Debug, Clone)]
pub struct InternalExecution {

    /// the internal to use
    pub internal: Internal,

    /// whether to open the resulting state in a new panel
    /// instead of the current ones
    pub bang: bool,

    /// arguments
    /// (for example `"~"` when a verb execution is `:!focus ~`)
    pub arg: Option<String>,
}

impl InternalExecution {
    pub fn from_internal(internal: Internal) -> Self {
        Self {
            internal,
            bang: false,
            arg: None,
        }
    }
    pub fn from_internal_bang(internal: Internal, bang: bool) -> Self {
        Self {
            internal,
            bang,
            arg: None,
        }
    }
    pub fn try_from(invocation_str: &str) -> Result<Self, ConfError> {
        let invocation = VerbInvocation::from(invocation_str);
        let internal = Internal::try_from(&invocation.name)?;
        Ok(Self {
            internal,
            bang: invocation.bang,
            arg: invocation.args,
        })
    }
    pub fn needs_selection(&self) -> bool {
        self.internal.needs_selection(&self.arg)
    }
}
impl fmt::Display for InternalExecution {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, ":{}", self.internal.name())?;
        if self.bang {
            write!(f, "!")?;
        }
        if let Some(arg) = &self.arg {
            write!(f, " {}", arg)?;
        }
        Ok(())
    }
}