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
use crate::error::Error;
use crate::tmux_interface::*;
use std::process::Output;

/// Select the window at target-window.
///
/// # Manual
///
/// tmux ^1.8:
/// ```text
/// tmux select-window [-lnpT] [-t target-window]
/// (alias: selectw)
/// ```
///
/// tmux ^1.5:
/// ```text
/// tmux select-window [-lnp] [-t target-window]
/// (alias: selectw)
/// ```
///
/// tmux ^0.8:
/// ```text
/// tmux select-window [-t target-window]
/// (alias: selectw)
/// ```
#[derive(Default, Debug)]
pub struct SelectWindow<'a> {
    /// [-l] - equivalent to last-window
    #[cfg(feature = "tmux_1_5")]
    pub last: Option<bool>,
    /// [-n] - equivalent to next-window
    #[cfg(feature = "tmux_1_5")]
    pub next: Option<bool>,
    /// [-p] - equivalent to previous-window
    #[cfg(feature = "tmux_1_5")]
    pub previous: Option<bool>,
    /// [-T] - if the selected window is already the current window, behave like last-window
    #[cfg(feature = "tmux_1_8")]
    pub switch: Option<bool>,
    /// [-t target-window] - target-window
    #[cfg(feature = "tmux_0_8")]
    pub target_window: Option<&'a str>,
}

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

#[derive(Default, Debug)]
pub struct SelectWindowBuilder<'a> {
    #[cfg(feature = "tmux_1_5")]
    pub last: Option<bool>,
    #[cfg(feature = "tmux_1_5")]
    pub next: Option<bool>,
    #[cfg(feature = "tmux_1_5")]
    pub previous: Option<bool>,
    #[cfg(feature = "tmux_1_8")]
    pub switch: Option<bool>,
    #[cfg(feature = "tmux_0_8")]
    pub target_window: Option<&'a str>,
}

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

    #[cfg(feature = "tmux_1_5")]
    pub fn last(&mut self) -> &mut Self {
        self.last = Some(true);
        self
    }

    #[cfg(feature = "tmux_1_5")]
    pub fn next(&mut self) -> &mut Self {
        self.next = Some(true);
        self
    }

    #[cfg(feature = "tmux_1_5")]
    pub fn previous(&mut self) -> &mut Self {
        self.previous = Some(true);
        self
    }

    #[cfg(feature = "tmux_1_8")]
    pub fn switch(&mut self) -> &mut Self {
        self.switch = Some(true);
        self
    }

    #[cfg(feature = "tmux_0_8")]
    pub fn target_window(&mut self, target_window: &'a str) -> &mut Self {
        self.target_window = Some(target_window);
        self
    }

    pub fn build(&self) -> SelectWindow<'a> {
        SelectWindow {
            #[cfg(feature = "tmux_1_5")]
            last: self.last,
            #[cfg(feature = "tmux_1_5")]
            next: self.next,
            #[cfg(feature = "tmux_1_5")]
            previous: self.previous,
            #[cfg(feature = "tmux_1_8")]
            switch: self.switch,
            #[cfg(feature = "tmux_0_8")]
            target_window: self.target_window,
        }
    }
}

impl<'a> TmuxInterface<'a> {
    #[cfg(not(feature = "use_cmd_alias"))]
    const SELECT_WINDOW: &'static str = "select-window";
    #[cfg(feature = "use_cmd_alias")]
    const SELECT_WINDOW: &'static str = "selectw";

    /// Select the window at target-window
    ///
    /// # Manual
    ///
    /// tmux ^1.8:
    /// ```text
    /// tmux select-window [-lnpT] [-t target-window]
    /// (alias: selectw)
    /// ```
    ///
    /// tmux ^1.5:
    /// ```text
    /// tmux select-window [-lnp] [-t target-window]
    /// (alias: selectw)
    /// ```
    ///
    /// tmux ^0.8:
    /// ```text
    /// tmux select-window [-t target-window]
    /// (alias: selectw)
    /// ```
    pub fn select_window(&mut self, select_window: Option<&SelectWindow>) -> Result<Output, Error> {
        let mut args: Vec<&str> = Vec::new();
        if let Some(select_window) = select_window {
            #[cfg(feature = "tmux_1_5")]
            if select_window.last.unwrap_or(false) {
                args.push(l_KEY);
            }
            #[cfg(feature = "tmux_1_5")]
            if select_window.next.unwrap_or(false) {
                args.push(n_KEY);
            }
            #[cfg(feature = "tmux_1_5")]
            if select_window.previous.unwrap_or(false) {
                args.push(p_KEY);
            }
            #[cfg(feature = "tmux_1_8")]
            if select_window.switch.unwrap_or(false) {
                args.push(T_KEY);
            }
            #[cfg(feature = "tmux_0_8")]
            if let Some(target_window) = select_window.target_window {
                args.extend_from_slice(&[t_KEY, &target_window])
            }
        }
        let output = self.command(TmuxInterface::SELECT_WINDOW, &args)?;
        Ok(output)
    }
}