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
174
175
176
177
178
179
use crate::commands::constants::*;
use crate::TmuxCommand;
use std::borrow::Cow;

/// Enter copy mode
///
/// # Manual
///
/// tmux ^3.2:
/// ```text
/// copy-mode [-eHMqu] [-s src-pane] [-t target-pane]
/// ```
///
/// tmux ^2.1:
/// ```text
/// copy-mode [-Meu] [-t target-pane]
/// ```
///
/// tmux ^1.0:
/// ```text
/// copy-mode [-u] [-t target-pane]
/// ```
///
/// tmux ^0.8:
/// ```text
/// copy-mode [-u] [-t target-window]
/// ```
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub struct CopyMode<'a> {
    /// `[-e]`
    #[cfg(feature = "tmux_2_1")]
    pub bottom_exit: bool,

    /// `[-H]` - hides the position indicator in the top right
    #[cfg(feature = "tmux_3_2")]
    pub hide_position: bool,

    /// `[-M]`
    #[cfg(feature = "tmux_2_1")]
    pub mouse_drag: bool,

    /// `[-q]` - cancels copy mode and any other modes
    #[cfg(feature = "tmux_3_2")]
    pub cancel: bool,

    /// `[-u]`
    #[cfg(feature = "tmux_0_8")]
    pub page_up: bool,

    /// `[-s src-pane]`
    #[cfg(feature = "tmux_1_0")]
    pub src_pane: Option<Cow<'a, str>>,

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

    /// `[-t target-window]`
    #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_1_0")))]
    pub target_window: Option<Cow<'a, str>>,
}

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

    /// `[-e]`
    #[cfg(feature = "tmux_2_1")]
    pub fn bottom_exit(mut self) -> Self {
        self.bottom_exit = true;
        self
    }

    /// `[-H]` - hides the position indicator in the top right
    #[cfg(feature = "tmux_3_2")]
    pub fn hide_position(mut self) -> Self {
        self.hide_position = true;
        self
    }

    /// `[-M]`
    #[cfg(feature = "tmux_2_1")]
    pub fn mouse_drag(mut self) -> Self {
        self.mouse_drag = true;
        self
    }

    /// `[-q]` - cancels copy mode and any other modes
    #[cfg(feature = "tmux_3_2")]
    pub fn cancel(mut self) -> Self {
        self.cancel = true;
        self
    }

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

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

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

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

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

        cmd.name(COPY_MODE);

        // `[-e]`
        #[cfg(feature = "tmux_2_1")]
        if self.bottom_exit {
            cmd.push_flag(E_LOWERCASE_KEY);
        }

        // `[-H]` - hides the position indicator in the top right
        #[cfg(feature = "tmux_3_2")]
        if self.hide_position {
            cmd.push_flag(H_UPPERCASE_KEY);
        }

        // `[-M]`
        #[cfg(feature = "tmux_2_1")]
        if self.mouse_drag {
            cmd.push_flag(M_UPPERCASE_KEY);
        }

        // `[-q]` - cancels copy mode and any other modes
        #[cfg(feature = "tmux_3_2")]
        if self.cancel {
            cmd.push_flag(Q_LOWERCASE_KEY);
        }

        // `[-u]`
        #[cfg(feature = "tmux_0_8")]
        if self.page_up {
            cmd.push_flag(U_LOWERCASE_KEY);
        }

        // `[-s src-pane]`
        #[cfg(feature = "tmux_1_0")]
        if let Some(src_pane) = self.src_pane {
            cmd.push_option(S_LOWERCASE_KEY, src_pane);
        }

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

        // `[-t target-window]`
        #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_1_0")))]
        if let Some(target_window) = self.target_window {
            cmd.push_option(T_LOWERCASE_KEY, target_window);
        }

        cmd
    }
}