tmux_interface 0.4.0

Rust language library for communication with TMUX via CLI
Documentation
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// auto-generated file
//

use crate::commands::constants::*;
use crate::{Error, TmuxCommand, TmuxCommands, TmuxOutput};
use std::borrow::Cow;
use std::process::{Child, Command, ExitStatus, Stdio};

/// enum for setting stdin, stdout, stderr, used in [`Tmux`] struct,
/// wrapper for [`Stdio`][`std::process::Stdio`]
// NOTE: std::process::Stdio has no Clone, Copy, there is no option to hold them
// in some wrapper struct, instead they will be initialized in-place before
// returning std::process::Command back from builder / converter
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub enum StdIO {
    /// inherit from invoker process
    #[default]
    Inherit,
    /// analogous to `/dev/null`
    Null,
    /// pipe, used later as child process handle will be returned
    Piped,
}

impl From<StdIO> for Stdio {
    fn from(item: StdIO) -> Self {
        match item {
            StdIO::Inherit => Stdio::inherit(),
            StdIO::Piped => Stdio::piped(),
            StdIO::Null => Stdio::null(),
        }
    }
}

// XXX: using environment vars
// XXX: set_cmds_separator
// NOTE: [-N] missing in man
/// [man tmux](http://man7.org/linux/man-pages/man1/tmux.1.html#DESCRIPTION)
///
/// # Manual
///
/// tmux >=3.6:
/// ```text
/// tmux [-2CDhlNuVv] [-c shell-command] [-f file] [-L socket-name] [-S socket-path] [-T features] [command [flags]]
/// ```
///
/// tmux >=3.4:
/// ```text
/// tmux [-2CDlNuVv] [-c shell-command] [-f file] [-L socket-name] [-S socket-path] [-T features] [command [flags]]
/// ```
///
/// tmux >=3.2:
/// ```text
/// tmux [-2CDluvV] [-c shell-command] [-f file] [-L socket-name] [-S socket-path] [-T features] [command [flags]]
/// ```
///
/// tmux >=2.1:
/// ```text
/// tmux [-2CluvV] [-c shell-command] [-f file] [-L socket-name] [-S socket-path] [command [flags]]
/// ```
///
/// tmux >=1.8:
/// ```text
/// tmux [-28lCquvV] [-c shell-command] [-f file] [-L socket-name] [-S socket-path] [command [flags]
/// ```
///
/// tmux >=1.5:
/// ```text
/// tmux [-28lquvV] [-c shell-command] [-f file] [-L socket-name] [-S socket-path] [command [flags]]
/// ```
///
/// tmux >=0.8:
/// ```text
/// tmux [-28dqUuVv] [-f file] [-L socket-name] [-S socket-path] [command [flags]]
/// ```
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub struct Tmux<'a> {
    /// `[-2]` - Force tmux to assume the terminal supports 256 colours
    #[cfg(feature = "tmux_0_8")]
    pub colours256: bool,

    /// `[-8]` - indicates that tmux supports 88 colours
    #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_1_9")))]
    pub colours88: bool,

    /// `[-d]` - indicates that tmux supports defaults colours
    #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_1_5")))]
    pub default_colours: bool,

    /// `[-q]` - prevent the server sending various information messages
    #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_2_1")))]
    pub prevent_msg: bool,

    /// `[-C]` - Start in control mode
    #[cfg(feature = "tmux_1_8")]
    pub control_mode: bool,

    /// `[-CC]` - Disable echo
    #[cfg(feature = "tmux_1_8")]
    pub disable_echo: bool,

    /// `[-D]` - Do not start the tmux server as a daemon
    #[cfg(feature = "tmux_3_2")]
    pub no_daemon: bool,

    /// `[-h]` - Print usage information and exit
    #[cfg(feature = "tmux_3_6")]
    pub help: bool,

    /// `[-l]` - Behave as a login shell
    #[cfg(feature = "tmux_1_5")]
    pub login_shell: bool,

    /// `[-N]` - Do not start the server even if the command would normally do so (for example new-session or start-server)
    #[cfg(feature = "tmux_3_4")]
    pub no_start: bool,

    /// `[-U]` - Unlock the server
    #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_1_5")))]
    pub unlock: bool,

    /// `[-u]` - Write UTF-8 output to the terminal
    #[cfg(feature = "tmux_0_8")]
    pub force_utf8: bool,

    /// `[-v]` - Request verbose logging
    #[cfg(feature = "tmux_0_8")]
    pub verbose_logging: bool,

    /// `[-V]` - Report the tmux version
    #[cfg(feature = "tmux_0_8")]
    pub version: bool,

    /// `[-c shell-command]` - Execute shell-command using the default shell
    #[cfg(feature = "tmux_1_5")]
    pub shell_command: Option<Cow<'a, str>>,

    /// `[-f file]` - Specify an alternative configuration file
    #[cfg(feature = "tmux_0_8")]
    pub file: Option<Cow<'a, str>>,

    /// `[-L socket-name]` - Allow a different socket name to be specified
    #[cfg(feature = "tmux_0_8")]
    pub socket_name: Option<Cow<'a, str>>,

    /// `[-S socket-path]` - Specify a full alternative path to the server socket
    #[cfg(feature = "tmux_0_8")]
    pub socket_path: Option<Cow<'a, str>>,

    /// `[-T features]` - Set terminal features for the client
    #[cfg(feature = "tmux_3_2")]
    pub features: Option<Cow<'a, str>>,

    /// `[command]`
    pub command: Option<TmuxCommands<'a>>,

    /// (1)
    pub stdin: Option<StdIO>,

    /// (2)
    pub stdout: Option<StdIO>,

    /// (3)
    pub stderr: Option<StdIO>,
}

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

    /// `[-2]` - Force tmux to assume the terminal supports 256 colours
    #[cfg(feature = "tmux_0_8")]
    pub fn colours256(mut self) -> Self {
        self.colours256 = true;
        self
    }

    /// `[-8]` - indicates that tmux supports 88 colours
    #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_1_9")))]
    pub fn colours88(mut self) -> Self {
        self.colours88 = true;
        self
    }

    /// `[-d]` - indicates that tmux supports defaults colours
    #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_1_5")))]
    pub fn default_colours(mut self) -> Self {
        self.default_colours = true;
        self
    }

    /// `[-q]` - prevent the server sending various information messages
    #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_2_1")))]
    pub fn prevent_msg(mut self) -> Self {
        self.prevent_msg = true;
        self
    }

    /// `[-C]` - Start in control mode
    #[cfg(feature = "tmux_1_8")]
    pub fn control_mode(mut self) -> Self {
        self.control_mode = true;
        self
    }

    /// `[-CC]` - Disable echo
    #[cfg(feature = "tmux_1_8")]
    pub fn disable_echo(mut self) -> Self {
        self.disable_echo = true;
        self
    }

    /// `[-D]` - Do not start the tmux server as a daemon
    #[cfg(feature = "tmux_3_2")]
    pub fn no_daemon(mut self) -> Self {
        self.no_daemon = true;
        self
    }

    /// `[-h]` - Print usage information and exit
    #[cfg(feature = "tmux_3_6")]
    pub fn help(mut self) -> Self {
        self.help = true;
        self
    }

    /// `[-l]` - Behave as a login shell
    #[cfg(feature = "tmux_1_5")]
    pub fn login_shell(mut self) -> Self {
        self.login_shell = true;
        self
    }

    /// `[-N]` - Do not start the server even if the command would normally do so (for example new-session or start-server)
    #[cfg(feature = "tmux_3_4")]
    pub fn no_start(mut self) -> Self {
        self.no_start = true;
        self
    }

    /// `[-U]` - Unlock the server
    #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_1_5")))]
    pub fn unlock(mut self) -> Self {
        self.unlock = true;
        self
    }

    /// `[-u]` - Write UTF-8 output to the terminal
    #[cfg(feature = "tmux_0_8")]
    pub fn force_utf8(mut self) -> Self {
        self.force_utf8 = true;
        self
    }

    /// `[-v]` - Request verbose logging
    #[cfg(feature = "tmux_0_8")]
    pub fn verbose_logging(mut self) -> Self {
        self.verbose_logging = true;
        self
    }

    /// `[-V]` - Report the tmux version
    #[cfg(feature = "tmux_0_8")]
    pub fn version(mut self) -> Self {
        self.version = true;
        self
    }

    /// `[-c shell-command]` - Execute shell-command using the default shell
    #[cfg(feature = "tmux_1_5")]
    pub fn shell_command<S: Into<Cow<'a, str>>>(mut self, shell_command: S) -> Self {
        self.shell_command = Some(shell_command.into());
        self
    }

    /// `[-f file]` - Specify an alternative configuration file
    #[cfg(feature = "tmux_0_8")]
    pub fn file<S: Into<Cow<'a, str>>>(mut self, file: S) -> Self {
        self.file = Some(file.into());
        self
    }

    /// `[-L socket-name]` - Allow a different socket name to be specified
    #[cfg(feature = "tmux_0_8")]
    pub fn socket_name<S: Into<Cow<'a, str>>>(mut self, socket_name: S) -> Self {
        self.socket_name = Some(socket_name.into());
        self
    }

    /// `[-S socket-path]` - Specify a full alternative path to the server socket
    #[cfg(feature = "tmux_0_8")]
    pub fn socket_path<S: Into<Cow<'a, str>>>(mut self, socket_path: S) -> Self {
        self.socket_path = Some(socket_path.into());
        self
    }

    /// `[-T features]` - Set terminal features for the client
    #[cfg(feature = "tmux_3_2")]
    pub fn features<S: Into<Cow<'a, str>>>(mut self, features: S) -> Self {
        self.features = Some(features.into());
        self
    }

    /// `[command]` -
    #[cfg(feature = "tmux_0_8")]
    pub fn command<T: Into<TmuxCommand<'a>>>(mut self, command: T) -> Self {
        self.command
            .get_or_insert(TmuxCommands::new())
            .push(command.into());
        self
    }

    /// build command with arguments in right order
    pub fn build(self) -> TmuxCommand<'a> {
        let mut cmd = TmuxCommand::new();

        cmd.name(TMUX);

        // `[-2]` - Force tmux to assume the terminal supports 256 colours
        #[cfg(feature = "tmux_0_8")]
        if self.colours256 {
            cmd.push_flag(_2_KEY);
        }

        // `[-8]` - indicates that tmux supports 88 colours
        #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_1_9")))]
        if self.colours88 {
            cmd.push_flag(_8_KEY);
        }

        // `[-d]` - indicates that tmux supports defaults colours
        #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_1_5")))]
        if self.default_colours {
            cmd.push_flag(D_LOWERCASE_KEY);
        }

        // `[-q]` - prevent the server sending various information messages
        #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_2_1")))]
        if self.prevent_msg {
            cmd.push_flag(Q_LOWERCASE_KEY);
        }

        // `[-C]` - Start in control mode
        #[cfg(feature = "tmux_1_8")]
        if self.control_mode {
            cmd.push_flag(C_UPPERCASE_KEY);
        }

        // `[-CC]` - Disable echo
        #[cfg(feature = "tmux_1_8")]
        if self.disable_echo {
            cmd.push_flag(CC_UPPERCASE_KEY);
        }

        // `[-D]` - Do not start the tmux server as a daemon
        #[cfg(feature = "tmux_3_2")]
        if self.no_daemon {
            cmd.push_flag(D_UPPERCASE_KEY);
        }

        // `[-h]` - Print usage information and exit
        #[cfg(feature = "tmux_3_6")]
        if self.help {
            cmd.push_flag(H_LOWERCASE_KEY);
        }

        // `[-l]` - Behave as a login shell
        #[cfg(feature = "tmux_1_5")]
        if self.login_shell {
            cmd.push_flag(L_LOWERCASE_KEY);
        }

        // `[-N]` - Do not start the server even if the command would normally do so (for example new-session or start-server)
        #[cfg(feature = "tmux_3_4")]
        if self.no_start {
            cmd.push_flag(N_UPPERCASE_KEY);
        }

        // `[-U]` - Unlock the server
        #[cfg(all(feature = "tmux_0_8", not(feature = "tmux_1_5")))]
        if self.unlock {
            cmd.push_flag(U_UPPERCASE_KEY);
        }

        // `[-u]` - Write UTF-8 output to the terminal
        #[cfg(feature = "tmux_0_8")]
        if self.force_utf8 {
            cmd.push_flag(U_LOWERCASE_KEY);
        }

        // `[-v]` - Request verbose logging
        #[cfg(feature = "tmux_0_8")]
        if self.verbose_logging {
            cmd.push_flag(V_LOWERCASE_KEY);
        }

        // `[-V]` - Report the tmux version
        #[cfg(feature = "tmux_0_8")]
        if self.version {
            cmd.push_flag(V_UPPERCASE_KEY);
        }

        // `[-c shell-command]` - Execute shell-command using the default shell
        #[cfg(feature = "tmux_1_5")]
        if let Some(shell_command) = self.shell_command {
            cmd.push_option(C_LOWERCASE_KEY, shell_command);
        }

        // `[-f file]` - Specify an alternative configuration file
        #[cfg(feature = "tmux_0_8")]
        if let Some(file) = self.file {
            cmd.push_option(F_LOWERCASE_KEY, file);
        }

        // `[-L socket-name]` - Allow a different socket name to be specified
        #[cfg(feature = "tmux_0_8")]
        if let Some(socket_name) = self.socket_name {
            cmd.push_option(L_UPPERCASE_KEY, socket_name);
        }

        // `[-S socket-path]` - Specify a full alternative path to the server socket
        #[cfg(feature = "tmux_0_8")]
        if let Some(socket_path) = self.socket_path {
            cmd.push_option(S_UPPERCASE_KEY, socket_path);
        }

        // `[-T features]` - Set terminal features for the client
        #[cfg(feature = "tmux_3_2")]
        if let Some(features) = self.features {
            cmd.push_option(T_UPPERCASE_KEY, features);
        }

        // `[command]` -
        #[cfg(feature = "tmux_0_8")]
        if let Some(command) = self.command {
            cmd.push_cmds(command);
        }

        cmd
    }

    //pub fn to_command(&self) -> Command {
    //Command::from(self)
    //}

    // XXX: ?
    pub fn into_command(self) -> Command {
        Command::from(self)
    }

    // XXX: command or subcommand better name?
    pub fn with_command<T: Into<TmuxCommand<'a>>>(command: T) -> Self {
        Tmux::new().command(command.into())
    }

    pub fn with_commands(commands: TmuxCommands<'a>) -> Self {
        Tmux::new().commands(commands)
    }

    // XXX: ?
    pub fn add_command<T: Into<TmuxCommand<'a>>>(mut self, command: T) -> Self {
        self.command
            .get_or_insert(TmuxCommands::new())
            .push(command.into());
        self
    }

    pub fn commands(mut self, commands: TmuxCommands<'a>) -> Self {
        self.command = Some(commands);
        self
    }
}

impl<'a> Tmux<'a> {
    /// execute tmux process, wait for output, return output
    ///
    /// by default:
    ///  * stdin  is inherited (differs from [`std::process::Command`], where it
    ///    is not inherited by default) prevents from immediately closing tmux
    ///    after attempt to read from stdin
    ///  * stdout is inherited
    ///  * stderr is inherited
    pub fn output(self) -> Result<TmuxOutput, Error> {
        let mut command = Command::from(self);
        let output = command.output()?;
        Ok(TmuxOutput(output))
    }

    /// spawn tmux process, return process handle as child
    /// by default:
    ///  * stdin  is inherited
    ///  * stdout is inherited
    ///  * stderr is inherited
    pub fn spawn(self) -> Result<Child, Error> {
        let mut command = Command::from(self);
        let child = command.spawn()?;
        Ok(child)
    }

    /// spawn tmux process, return status result of the command execution
    /// by default:
    ///  * stdin  is inherited
    ///  * stdout is inherited
    ///  * stderr is inherited
    pub fn status(self) -> Result<ExitStatus, Error> {
        let mut command = Command::from(self);
        let status = command.status()?;
        Ok(status)
    }

    /// set stdin
    pub fn stdin(mut self, stdin: Option<StdIO>) -> Self {
        self.stdin = stdin;
        self
    }

    /// set stdout
    pub fn stdout(mut self, stdout: Option<StdIO>) -> Self {
        self.stdout = stdout;
        self
    }

    /// set stderr
    pub fn stderr(mut self, stderr: Option<StdIO>) -> Self {
        self.stderr = stderr;
        self
    }
}

//use crate::{HasSession, KillSession, NewSession};

//impl<'a> From<NewSession<'a>> for Tmux<'a> {
//fn from(item: NewSession<'a>) -> Self {
//item.build().into_tmux()
//}
//}

//impl<'a> From<HasSession<'a>> for Tmux<'a> {
//fn from(item: HasSession<'a>) -> Self {
//item.build().into_tmux()
//}
//}

//impl<'a> From<KillSession<'a>> for Tmux<'a> {
//fn from(item: KillSession<'a>) -> Self {
//item.build().into_tmux()
//}
//}

// convert from Tmux into Command (take Tmux -> build TmuxCommand -> into Command)
impl<'a> From<Tmux<'a>> for Command {
    fn from(item: Tmux<'a>) -> Self {
        let mut command: Command = item.clone().build().into();

        if let Some(stdio) = item.stdin {
            command.stdin::<Stdio>(stdio.into());
        } else {
            // NOTE: inherit stdin to prevent tmux fail with error `terminal failed: not a terminal`
            command.stdin(Stdio::inherit());
        }

        if let Some(stdio) = item.stdout {
            command.stdout::<Stdio>(stdio.into());
        }

        if let Some(stdio) = item.stderr {
            command.stderr::<Stdio>(stdio.into());
        }

        command
    }
}

//impl<'a> From<&Tmux<'a>> for Command {
//fn from(tmux: &Tmux) -> Self {
////let mut command = Command::new(TMUX);

//let cmd = match &tmux.0.name {
//Some(cmd) => cmd,
//None => &Cow::Borrowed(TMUX),
//};
//let mut command = Command::new(cmd.as_ref());

////// XXX: ugly?
////if let Some(v) = &tmux.bin_args {
////for a in v {
////command.arg(a.as_ref());
////}
////}

//// XXX: ugly?
//if let Some(v) = &tmux.0.args {
//for a in v {
//command.arg(a.as_ref());
//}
//}

//command
//}
//}