tmux_interface/commands/hooks/
set_hook.rs

1use crate::commands::constants::*;
2use crate::TmuxCommand;
3use std::borrow::Cow;
4
5/// Structure for setting or unsetting hook `hook-name` to command.
6///
7/// # Manual
8///
9/// tmux ^3.0:
10/// ```text
11/// set-hook [-agRu] [-t target-session] hook-name command
12/// ```
13///
14/// tmux ^2.8:
15/// ```text
16/// set-hook [-gRu] [-t target-session] hook-name command
17/// ```
18///
19/// tmux ^2.4:
20/// ```text
21/// set-hook [-gu] [-t target-session] hook-name command
22/// ```
23///
24/// tmux ^2.2:
25/// ```text
26/// set-hook [-g] [-t target-session] hook-name command
27/// ```
28#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
29pub struct SetHook<'a> {
30    /// `[-a]`
31    #[cfg(feature = "tmux_3_0")]
32    pub append: bool,
33
34    /// `[-g]`
35    #[cfg(feature = "tmux_2_2")]
36    pub global: bool,
37
38    /// `[-R]`
39    #[cfg(feature = "tmux_2_8")]
40    pub run: bool,
41
42    /// `[-u]`
43    #[cfg(feature = "tmux_2_4")]
44    pub unset: bool,
45
46    /// `[-t target-session]`
47    #[cfg(feature = "tmux_2_2")]
48    pub target_session: Option<Cow<'a, str>>,
49
50    /// `hook-name`
51    #[cfg(feature = "tmux_2_2")]
52    pub hook_name: Option<Cow<'a, str>>,
53
54    /// `command`
55    #[cfg(feature = "tmux_2_2")]
56    pub command: Option<Cow<'a, str>>,
57}
58
59impl<'a> SetHook<'a> {
60    pub fn new() -> Self {
61        Default::default()
62    }
63
64    /// `[-a]`
65    #[cfg(feature = "tmux_3_0")]
66    pub fn append(mut self) -> Self {
67        self.append = true;
68        self
69    }
70
71    /// `[-g]`
72    #[cfg(feature = "tmux_2_2")]
73    pub fn global(mut self) -> Self {
74        self.global = true;
75        self
76    }
77
78    /// `[-R]`
79    #[cfg(feature = "tmux_2_8")]
80    pub fn run(mut self) -> Self {
81        self.run = true;
82        self
83    }
84
85    /// `[-u]`
86    #[cfg(feature = "tmux_2_4")]
87    pub fn unset(mut self) -> Self {
88        self.unset = true;
89        self
90    }
91
92    /// `[-t target-session]`
93    #[cfg(feature = "tmux_2_2")]
94    pub fn target_session<S: Into<Cow<'a, str>>>(mut self, target_session: S) -> Self {
95        self.target_session = Some(target_session.into());
96        self
97    }
98
99    /// `hook-name`
100    #[cfg(feature = "tmux_2_2")]
101    pub fn hook_name<S: Into<Cow<'a, str>>>(mut self, hook_name: S) -> Self {
102        self.hook_name = Some(hook_name.into());
103        self
104    }
105
106    // XXX: command?
107    /// `command`
108    #[cfg(feature = "tmux_2_2")]
109    pub fn command<S: Into<Cow<'a, str>>>(mut self, command: S) -> Self {
110        self.command = Some(command.into());
111        self
112    }
113
114    pub fn build(self) -> TmuxCommand<'a> {
115        let mut cmd = TmuxCommand::new();
116
117        cmd.name(SET_HOOK);
118
119        // `[-a]`
120        #[cfg(feature = "tmux_3_0")]
121        if self.append {
122            cmd.push_flag(A_LOWERCASE_KEY);
123        }
124
125        // `[-g]`
126        #[cfg(feature = "tmux_2_2")]
127        if self.global {
128            cmd.push_flag(G_LOWERCASE_KEY);
129        }
130
131        // `[-R]`
132        #[cfg(feature = "tmux_2_8")]
133        if self.run {
134            cmd.push_flag(R_UPPERCASE_KEY);
135        }
136
137        // `[-u]`
138        #[cfg(feature = "tmux_2_4")]
139        if self.unset {
140            cmd.push_flag(U_LOWERCASE_KEY);
141        }
142
143        // `[-t target-session]`
144        #[cfg(feature = "tmux_2_2")]
145        if let Some(target_session) = self.target_session {
146            cmd.push_option(T_LOWERCASE_KEY, target_session);
147        }
148
149        // `hook-name`
150        #[cfg(feature = "tmux_2_2")]
151        if let Some(hook_name) = self.hook_name {
152            cmd.push_param(hook_name);
153        }
154
155        // `command`
156        #[cfg(feature = "tmux_2_2")]
157        if let Some(command) = self.command {
158            cmd.push_param(command);
159        }
160
161        cmd
162    }
163}