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
use super::tmux_interface::*;
use super::tmux_interface_error::TmuxInterfaceError;
use std::process::Output;


/// # Manual
///
/// ```text
/// tmux bind-key [-nr] [-T key-table] key command [arguments]
/// (alias: bind)
/// ```
#[derive(Default, Clone, Debug)]
pub struct BindKey<'a> {
    pub root: Option<bool>,                     // [-n]
    pub repeat: Option<bool>,                   // [-r]
    pub key_table: Option<&'a str>,             // [-T key-table]
    pub key: &'a str,                           // key
    pub command: &'a str,                       // command
    pub arguments: Option<&'a str>              // [arguments]
}

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


/// # Manual
///
/// ```text
/// tmux send-keys [-lMRX] [-N repeat-count] [-t target-pane] key ...
/// (alias: send)
/// ```
#[derive(Default, Clone, Debug)]
pub struct SendKeys<'a> {
    pub disable_lookup: Option<bool>,           // [-l]
    pub mouse_event: Option<bool>,              // [-M]
    pub copy_mode: Option<bool>,                // [-R]
    pub reset: Option<bool>,                    // [-X]
    pub repeat_count: Option<usize>,            // [-N repeat-count]
    pub target_pane: Option<&'a str>,           // [-t target-pane]
    pub key: Vec<&'a str>                       // key
}

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


/// Key bindings
impl<'a> TmuxInterface<'a> {

    const BIND_KEY: &'static str = "bind-key";
    const LIST_KEYS: &'static str = "list-keys";
    const SEND_KEYS: &'static str = "send-keys";
    const SEND_PREFIX: &'static str = "send-prefix";
    const UNBIND_KEY: &'static str = "unbind-key";


    /// # Manual
    ///
    /// ```text
    /// tmux bind-key [-nr] [-T key-table] key command [arguments]
    /// (alias: bind)
    /// ```
    pub fn bind_key(&self, bind_key: &BindKey) -> Result<Output, TmuxInterfaceError> {
        let mut args: Vec<&str> = Vec::new();
        if bind_key.root.unwrap_or(false) { args.push(n_KEY); }
        if bind_key.repeat.unwrap_or(false) { args.push(r_KEY); }
        bind_key.key_table.and_then(|s| Some(args.extend_from_slice(&[T_KEY, &s])));
        args.push(bind_key.key);
        args.push(bind_key.command);
        bind_key.arguments.and_then(|s| Some(args.push(&s)));
        let output = self.subcommand(TmuxInterface::BIND_KEY, &args)?;
        Ok(output)
    }


    /// # Manual
    ///
    /// ```text
    /// tmux list-keys [-T key-table]
    /// (alias: lsk)
    /// ```
    pub fn list_keys(&self, key_table: Option<&str>) -> Result<Output, TmuxInterfaceError> {
        let mut args: Vec<&str> = Vec::new();
        key_table.and_then(|s| Some(args.extend_from_slice(&[T_KEY, &s])));
        let output = self.subcommand(TmuxInterface::LIST_KEYS, &args)?;
        Ok(output)
    }


    // FIXME: repeat-count
    /// # Manual
    ///
    /// ```text
    /// tmux send-keys [-lMRX] [-N repeat-count] [-t target-pane] key ...
    /// (alias: send)
    /// ```
    pub fn send_keys(&self, send_keys: &SendKeys) -> Result<Output, TmuxInterfaceError> {
        let mut args: Vec<&str> = Vec::new();
        if send_keys.disable_lookup.unwrap_or(false) { args.push(l_KEY); }
        if send_keys.mouse_event.unwrap_or(false) { args.push(M_KEY); }
        if send_keys.copy_mode.unwrap_or(false) { args.push(R_KEY); }
        if send_keys.reset.unwrap_or(false) { args.push(X_KEY); }
        //send_keys.repeat_count.and_then(|s| Some(args.extend_from_slice(&[N_KEY, s])));
        let s;
        if let Some(repeat_count) = send_keys.repeat_count {
            s = repeat_count.to_string();
            args.extend_from_slice(&[N_KEY, &s]);
        }
        send_keys.target_pane.and_then(|s| Some(args.extend_from_slice(&[t_KEY, &s])));
        //args.extend_from_slice(send_keys.keys.as_slice());
        //args.extend_from_slice(send_keys.keys);
        args.append(&mut send_keys.key.clone());
        //args.push("C-m");
        let output = self.subcommand(TmuxInterface::SEND_KEYS, &args)?;
        Ok(output)
    }


    /// # Manual
    ///
    /// ```text
    /// tmux send-prefix [-2] [-t target-pane]
    /// ```
    pub fn send_prefix(&self,
                       secondary: Option<bool>,
                       target_pane: Option<&str>
                       ) -> Result<Output, TmuxInterfaceError> {
        let mut args: Vec<&str> = Vec::new();
        if secondary.unwrap_or(false) { args.push(_2_KEY); }
        target_pane.and_then(|s| Some(args.extend_from_slice(&[t_KEY, &s])));
        let output = self.subcommand(TmuxInterface::SEND_PREFIX, &args)?;
        Ok(output)
    }


    /// # Manual
    ///
    /// ```text
    /// tmux unbind-key [-an] [-T key-table] key
    /// (alias: unbind)
    /// ```
    pub fn unbind_key(&self,
                      all: Option<bool>,
                      root: Option<bool>,
                      key_table: Option<&str>,
                      key: &str
                      ) -> Result<Output, TmuxInterfaceError> {
        let mut args: Vec<&str> = Vec::new();
        if all.unwrap_or(false) { args.push(a_KEY); }
        if root.unwrap_or(false) { args.push(n_KEY); }
        key_table.and_then(|s| Some(args.extend_from_slice(&[T_KEY, &s])));
        args.push(key);
        let output = self.subcommand(TmuxInterface::UNBIND_KEY, &args)?;
        Ok(output)
    }


}