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
180
181
182
183
184
185
186
187
#![allow(non_upper_case_globals)]
extern crate regex;
use std::process::{Command, Output};
use std::str;
use regex::Regex;
use super::tmux_interface_error::TmuxInterfaceError;
pub const _1_KEY: &str = "-1";
pub const _2_KEY: &str = "-2";
pub const a_KEY: &str = "-a";
pub const b_KEY: &str = "-b";
pub const c_KEY: &str = "-c";
pub const d_KEY: &str = "-d";
pub const e_KEY: &str = "-e";
pub const f_KEY: &str = "-f";
pub const g_KEY: &str = "-g";
pub const h_KEY: &str = "-h";
pub const i_KEY: &str = "-i";
pub const k_KEY: &str = "-k";
pub const l_KEY: &str = "-l";
pub const m_KEY: &str = "-m";
pub const n_KEY: &str = "-n";
pub const o_KEY: &str = "-o";
pub const p_KEY: &str = "-p";
pub const q_KEY: &str = "-q";
pub const r_KEY: &str = "-r";
pub const s_KEY: &str = "-s";
pub const t_KEY: &str = "-t";
pub const u_KEY: &str = "-u";
pub const v_KEY: &str = "-v";
pub const w_KEY: &str = "-w";
pub const x_KEY: &str = "-x";
pub const y_KEY: &str = "-y";
#[allow(non_upper_case_globals)]
pub const A_KEY: &str = "-A";
pub const C_KEY: &str = "-C";
pub const D_KEY: &str = "-D";
pub const E_KEY: &str = "-E";
pub const F_KEY: &str = "-F";
pub const G_KEY: &str = "-G";
pub const H_KEY: &str = "-H";
pub const I_KEY: &str = "-I";
pub const J_KEY: &str = "-J";
pub const L_KEY: &str = "-L";
pub const M_KEY: &str = "-M";
pub const N_KEY: &str = "-N";
pub const O_KEY: &str = "-O";
pub const P_KEY: &str = "-P";
pub const R_KEY: &str = "-R";
pub const S_KEY: &str = "-S";
pub const T_KEY: &str = "-T";
pub const U_KEY: &str = "-U";
pub const V_KEY: &str = "-V";
pub const X_KEY: &str = "-X";
pub const Z_KEY: &str = "-Z";
pub const CC_KEY: &str = "-C";
#[derive(Default)]
pub struct TmuxInterface<'a> {
pub environment: Option<&'a str>,
pub tmux: Option<&'a str>,
pub colours256: Option<bool>,
pub control_mode: Option<bool>,
pub disable_echo: Option<bool>,
pub shell_cmd: Option<&'a str>,
pub file: Option<&'a str>,
pub socket_name: Option<&'a str>,
pub login_shell: Option<bool>,
pub socket_path: Option<&'a str>,
pub force_utf8: Option<bool>,
pub verbose_logging: Option<bool>,
pub version: Option<bool>
}
impl<'a> TmuxInterface<'a> {
const TMUX: &'static str = "tmux";
const VERSION_STR_REGEX: &'static str = r"^tmux\s(\d+).(\d+)\n$";
pub fn new() -> Self {
Default::default()
}
pub fn subcommand(&self, subcmd: &str, args: &[&str]) -> Result<Output, TmuxInterfaceError> {
let mut options: Vec<&str> = Vec::new();
options.push(subcmd);
options.extend_from_slice(args);
self.exec(&options)
}
pub fn exec(&self, args: &[&str]) -> Result<Output, TmuxInterfaceError> {
let mut options: Vec<&str> = Vec::new();
let mut cmd = Command::new(self.tmux.unwrap_or(TmuxInterface::TMUX));
if self.colours256.unwrap_or(false) { options.push(_2_KEY); };
if self.control_mode.unwrap_or(false) { options.push(C_KEY); };
if self.disable_echo.unwrap_or(false) { options.push(CC_KEY); };
if self.login_shell.unwrap_or(false) { options.push(l_KEY) };
if self.force_utf8.unwrap_or(false) { options.push(u_KEY) }
if self.verbose_logging.unwrap_or(false) { options.push(v_KEY) }
self.shell_cmd.and_then(|s| Some(options.extend_from_slice(&[c_KEY, &s])));
self.file.and_then(|s| Some(options.extend_from_slice(&[f_KEY, &s])));
self.socket_name.and_then(|s| Some(options.extend_from_slice(&[L_KEY, &s])));
self.socket_path.and_then(|s| Some(options.extend_from_slice(&[S_KEY, &s])));
cmd.args(options);
let output = cmd.args(args).output()?;
Ok(output)
}
pub fn version(&self) -> Result<(usize, usize), TmuxInterfaceError> {
let mut tmux = Command::new(self.tmux.unwrap_or(TmuxInterface::TMUX));
let output = tmux.arg(V_KEY).output()?;
let version_str = String::from_utf8_lossy(&output.stdout).to_string();
let regex = Regex::new(TmuxInterface::VERSION_STR_REGEX)?;
if let Some(cap) = regex.captures(&version_str) {
let major = cap[1].parse::<usize>()?;
let minor = cap[2].parse::<usize>()?;
Ok((major, minor))
} else {
Err(TmuxInterfaceError::new("cap"))
}
}
}