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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crate::commands::constants::*;
use crate::TmuxCommand;
use std::borrow::Cow;

pub type SetEnv<'a> = SetEnvironment<'a>;

/// Structure for setting or unsetting an environment variable
///
/// # Manual
///
/// tmux ^3.2:
/// ```text
/// set-environment [-Fhgru] [-t target-session] name [value]
/// (alias: setenv)
/// ```
///
/// tmux ^1.0:
/// ```text
/// set-environment [-gru] [-t target-session] name [value]
/// (alias: setenv)
/// ```
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub struct SetEnvironment<'a> {
    /// `[-F]` - value is expanded as a format
    #[cfg(feature = "tmux_3_2")]
    pub expand: bool,

    /// `[-h]` - marks the variable as hidden
    #[cfg(feature = "tmux_3_2")]
    pub hidden: bool,

    /// `[-g]` - make change in the global environment
    #[cfg(feature = "tmux_1_0")]
    pub global: bool,

    /// `[-r]` - remove the variable from the environment before starting a new process
    #[cfg(feature = "tmux_1_0")]
    pub remove: bool,

    /// `[-u]` - unset a variable
    #[cfg(feature = "tmux_1_0")]
    pub unset: bool,

    /// `[-t target-session]` - target-session
    #[cfg(feature = "tmux_1_0")]
    pub target_session: Option<Cow<'a, str>>,

    /// `name`
    #[cfg(feature = "tmux_1_0")]
    pub name: Option<Cow<'a, str>>,

    /// `[value]` - specify the value
    #[cfg(feature = "tmux_1_0")]
    pub value: Option<Cow<'a, str>>,
}

impl<'a> SetEnvironment<'a> {
    pub fn new() -> Self {
        Default::default()
    }

    /// `[-F]` - value is expanded as a format
    #[cfg(feature = "tmux_3_2")]
    pub fn expand(mut self) -> Self {
        self.expand = true;
        self
    }

    /// `[-h]` - marks the variable as hidden
    #[cfg(feature = "tmux_3_2")]
    pub fn hidden(mut self) -> Self {
        self.hidden = true;
        self
    }

    /// `[-g]` - make change in the global environment
    #[cfg(feature = "tmux_1_0")]
    pub fn global(mut self) -> Self {
        self.global = true;
        self
    }

    /// `[-r]` - remove the variable from the environment before starting a new process
    #[cfg(feature = "tmux_1_0")]
    pub fn remove(mut self) -> Self {
        self.remove = true;
        self
    }

    /// `[-u]` - unset a variable
    #[cfg(feature = "tmux_1_0")]
    pub fn unset(mut self) -> Self {
        self.unset = true;
        self
    }

    /// `[-t target-session]` - target-session
    #[cfg(feature = "tmux_1_0")]
    pub fn target_session<S: Into<Cow<'a, str>>>(mut self, target_session: S) -> Self {
        self.target_session = Some(target_session.into());
        self
    }

    /// `name`
    #[cfg(feature = "tmux_1_0")]
    pub fn name<S: Into<Cow<'a, str>>>(mut self, name: S) -> Self {
        self.name = Some(name.into());
        self
    }

    /// `[value]` - specify the value
    #[cfg(feature = "tmux_1_0")]
    pub fn value<S: Into<Cow<'a, str>>>(mut self, value: S) -> Self {
        self.value = Some(value.into());
        self
    }

    pub fn build(self) -> TmuxCommand<'a> {
        let mut cmd = TmuxCommand::new();

        cmd.name(SET_ENVIRONMENT);

        // `[-F]` - value is expanded as a format
        #[cfg(feature = "tmux_3_2")]
        if self.expand {
            cmd.push_flag(F_UPPERCASE_KEY);
        }

        // `[-h]` - marks the variable as hidden
        #[cfg(feature = "tmux_3_2")]
        if self.hidden {
            cmd.push_flag(H_LOWERCASE_KEY);
        }

        // `[-g]` - make change in the global environment
        #[cfg(feature = "tmux_1_0")]
        if self.global {
            cmd.push_flag(G_LOWERCASE_KEY);
        }

        // `[-r]` - remove the variable from the environment before starting a new process
        #[cfg(feature = "tmux_1_0")]
        if self.remove {
            cmd.push_flag(R_LOWERCASE_KEY);
        }

        // `[-u]` - unset a variable
        #[cfg(feature = "tmux_1_0")]
        if self.unset {
            cmd.push_flag(U_LOWERCASE_KEY);
        }

        // `[-t target-session]` - target-session
        #[cfg(feature = "tmux_1_0")]
        if let Some(target_session) = self.target_session {
            cmd.push_option(T_LOWERCASE_KEY, target_session);
        }

        // `name`
        #[cfg(feature = "tmux_1_0")]
        if let Some(name) = self.name {
            cmd.push_param(name);
        }

        // `[value]` - specify the value
        #[cfg(feature = "tmux_1_0")]
        if let Some(value) = self.value {
            cmd.push_param(value);
        }

        cmd
    }
}