tmux_interface/commands/clients_and_sessions/
source_file.rs

1use crate::commands::constants::*;
2use crate::TmuxCommand;
3use std::borrow::Cow;
4
5pub type Source<'a> = SourceFile<'a>;
6
7/// Execute commands from path
8///
9/// # Manual
10///
11/// tmux ^3.4:
12/// ```text
13/// source-file [-Fnqv] [-t target-pane] path ...
14/// (alias: source)
15/// ```
16///
17/// tmux ^3.2:
18/// ```text
19/// source-file [-Fnqv] path ...
20/// (alias: source)
21/// ```
22///
23/// tmux ^3.0:
24/// ```text
25/// source-file [-nqv] path
26/// (alias: source)
27/// ```
28///
29/// tmux ^2.3:
30/// ```text
31/// source-file path
32/// (alias: source)
33///
34/// ```
35/// tmux ^0.8:
36/// ```text
37/// source-file path
38/// (alias: source)
39/// ```
40#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
41pub struct SourceFile<'a> {
42    /// `[-F]` - value is expanded as a format
43    #[cfg(feature = "tmux_3_2")]
44    pub expand: bool,
45
46    /// `[-n]`
47    #[cfg(feature = "tmux_3_0")]
48    pub not_execute: bool,
49
50    /// `[-q]`
51    #[cfg(feature = "tmux_3_0")]
52    pub quiet: bool,
53
54    /// `[-v]`
55    #[cfg(feature = "tmux_3_0")]
56    pub verbose: bool,
57
58    /// `[-t target-pane]`
59    #[cfg(feature = "tmux_3_4")]
60    pub target_pane: Option<Cow<'a, str>>,
61
62    /// `path`
63    #[cfg(feature = "tmux_0_8")]
64    pub path: Option<Cow<'a, str>>,
65}
66
67impl<'a> SourceFile<'a> {
68    pub fn new() -> Self {
69        Default::default()
70    }
71
72    /// `[-F]` - value is expanded as a format
73    #[cfg(feature = "tmux_3_2")]
74    pub fn expand(mut self) -> Self {
75        self.expand = true;
76        self
77    }
78
79    /// `[-n]`
80    #[cfg(feature = "tmux_3_0")]
81    pub fn not_execute(mut self) -> Self {
82        self.not_execute = true;
83        self
84    }
85
86    /// `[-q]`
87    #[cfg(feature = "tmux_3_0")]
88    pub fn quiet(mut self) -> Self {
89        self.quiet = true;
90        self
91    }
92
93    /// `[-v]`
94    #[cfg(feature = "tmux_3_0")]
95    pub fn verbose(mut self) -> Self {
96        self.verbose = true;
97        self
98    }
99
100    /// `[-t target-pane]`
101    #[cfg(feature = "tmux_3_4")]
102    pub fn target_pane<S: Into<Cow<'a, str>>>(mut self, target_pane: S) -> Self {
103        self.target_pane = Some(target_pane.into());
104        self
105    }
106
107    /// `path`
108    #[cfg(feature = "tmux_0_8")]
109    pub fn path<S: Into<Cow<'a, str>>>(mut self, path: S) -> Self {
110        self.path = Some(path.into());
111        self
112    }
113
114    pub fn build(self) -> TmuxCommand<'a> {
115        let mut cmd = TmuxCommand::new();
116
117        cmd.name(SOURCE_FILE);
118
119        // `[-F]` - value is expanded as a format
120        #[cfg(feature = "tmux_3_2")]
121        if self.expand {
122            cmd.push_flag(F_UPPERCASE_KEY);
123        }
124
125        // `[-n]`
126        #[cfg(feature = "tmux_3_0")]
127        if self.not_execute {
128            cmd.push_flag(N_LOWERCASE_KEY);
129        }
130
131        // `[-q]`
132        #[cfg(feature = "tmux_3_0")]
133        if self.quiet {
134            cmd.push_flag(Q_LOWERCASE_KEY);
135        }
136
137        // `[-v]`
138        #[cfg(feature = "tmux_3_0")]
139        if self.verbose {
140            cmd.push_flag(V_LOWERCASE_KEY);
141        }
142
143        // `[-t target-pane]`
144        #[cfg(feature = "tmux_3_4")]
145        if let Some(target_pane) = self.target_pane {
146            cmd.push_option(T_LOWERCASE_KEY, target_pane);
147        }
148
149        // `path`
150        #[cfg(feature = "tmux_0_8")]
151        if let Some(path) = self.path {
152            cmd.push_param(path);
153        }
154
155        cmd
156    }
157}