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
602
use crate::{Tmux, TmuxCommands};
use std::borrow::Cow;
use std::fmt;
use std::process::Command;

// XXX: cmd, command, tmux command all proper names in methods, fields
// XXX: mb enum for command?
// XXX: rename (.cmd to .name)?
// XXX: environment
/// Standard tmux command line arguments syntax:
///
/// ```text
/// tmux [-2CluvV] [-c shell-command] [-f file] [-L socket-name] [-S socket-path] [command [flags]]
/// ```
///
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct TmuxCommand<'a> {
    /// environment variables
    pub envs: Option<Vec<(Cow<'a, str>, Cow<'a, str>)>>,

    /// command name
    pub name: Option<Cow<'a, str>>,

    /// command alias
    pub alias: Option<Cow<'a, str>>,

    // XXX: remove
    /// flags (`[-a] [-b] [-c]`)
    pub flags: Option<Vec<Cow<'a, str>>>,

    /// short flags (`[-a] [-b] [-c]`)
    pub flags_short: Option<String>,

    /// arguments: long flags, options, parameters (`[--longflag] [-o opt] [param]`)
    pub args: Option<Vec<Cow<'a, str>>>,

    /// subcommands list
    pub subcommands: Option<TmuxCommands<'a>>,

    // XXX: Cow<'a, str> or &'a str?
    /// separator between command and it's flags, args, subcommand (" ")
    pub separator: Option<&'a str>,

    // XXX: Cow<'a, str> or &'a str?
    /// flags, args separator (usually double dash `--`)
    pub flags_args_separator: Option<&'a str>,

    /// combine multiple single flags into flags line (`-f -a` = `-fa`)
    pub combine_short_flags: bool,

    /// use command alias instead of name (`new-session` = `new`)
    pub use_alias: bool,
}

// XXX: reason?
//macro_rules! tmux_command!("env", "cmd", "-a", "-b", "-arg 0", "param")

pub const EMPTY_CMD: &str = "";
pub const TMUX_COMMAND_ARG_SEPARATOR: &str = " ";

impl<'a> Default for TmuxCommand<'a> {
    fn default() -> Self {
        Self {
            envs: None,
            name: None,
            alias: None,
            flags: None,
            flags_short: None,
            args: None,
            subcommands: None,
            separator: None,
            flags_args_separator: None,
            combine_short_flags: true,
            use_alias: true,
        }
    }
}

// XXX: reason?
// s. clap
//macro_rules! tmux_command!("env", "cmd", "-a", "-b", "-arg 0", "param")

impl<'a> fmt::Display for TmuxCommand<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let output = self
            .to_vec()
            .join(self.separator.unwrap_or(TMUX_COMMAND_ARG_SEPARATOR));
        write!(f, "{}", output)
    }
}

impl<'a> TmuxCommand<'a> {
    /// Create new `Cmd` structure (using `default()` method)
    pub fn new() -> Self {
        Default::default()
    }

    /// Create new `Cmd` structure, initialize both `not_combine_short_flags` and `not_use_alias`
    /// fields with `true`. Command will not combine flags (separate flags will be used instead),
    /// and not use alias (command name will be used instead)
    pub fn new_full<S: Into<Cow<'a, str>>>(name: S) -> Self {
        Self {
            name: Some(name.into()),
            combine_short_flags: false,
            use_alias: false,
            ..Default::default()
        }
    }

    /// Create and set `name` field
    pub fn with_name<S: Into<Cow<'a, str>>>(name: S) -> Self {
        Self {
            name: Some(name.into()),
            ..Default::default()
        }
    }

    /// Create and set `alias` field
    pub fn with_alias<S: Into<Cow<'a, str>>>(alias: S) -> Self {
        Self {
            alias: Some(alias.into()),
            ..Default::default()
        }
    }

    pub fn with_cmds(cmd_list: TmuxCommands<'a>) -> Self {
        Self {
            subcommands: Some(cmd_list),
            ..Default::default()
        }
    }

    /// Set `Cmd.name` field
    pub fn name<S: Into<Cow<'a, str>>>(&mut self, cmd: S) -> &mut Self {
        self.name = Some(cmd.into());
        self
    }

    /// Set `Cmd.alias` field
    pub fn alias<S: Into<Cow<'a, str>>>(&mut self, alias: S) -> &mut Self {
        self.alias = Some(alias.into());
        self
    }

    /// Add an environment variable to `Cmd.env`
    pub fn env<T, U>(&mut self, key: T, value: U) -> &mut Self
    where
        T: Into<Cow<'a, str>>,
        U: Into<Cow<'a, str>>,
    {
        self.envs
            .get_or_insert(Vec::new())
            .push((key.into(), value.into()));
        self
    }

    // XXX: hard bound to cmd_args
    // if vec doesn't exist, creates it and appends with given arguments
    /// push a single flag (`-x`)
    pub fn push_flag<S: Into<Cow<'a, str>>>(&mut self, flag: S) -> &mut Self {
        self.args.get_or_insert(Vec::new()).push(flag.into());
        self
    }

    pub fn push_flag_short(&mut self, flag: char) -> &mut Self {
        self.flags_short.get_or_insert(String::new()).push(flag);
        self
    }

    // if vec doesn't exist, creates it and appends with given arguments
    /// push an option, flag and value (`-x  <VALUE>`)
    pub fn push_option<U, V>(&mut self, key: U, option: V) -> &mut Self
    where
        U: Into<Cow<'a, str>>,
        V: Into<Cow<'a, str>>,
    {
        self.args
            .get_or_insert(Vec::new())
            .extend_from_slice(&[key.into(), option.into()]);
        self
    }

    // if vec doesn't exist, creates it and appends with given arguments
    /// push a single parameter (`<VALUE>`)
    pub fn push_param<S: Into<Cow<'a, str>>>(&mut self, param: S) -> &mut Self {
        self.args.get_or_insert(Vec::new()).push(param.into());
        self
    }

    // XXX: rename subcmd?
    pub fn push_cmd(&mut self, cmd: TmuxCommand<'a>) -> &mut Self {
        self.subcommands
            .get_or_insert(TmuxCommands::new())
            .push(cmd);
        self
    }

    // XXX: rename subcmd?
    pub fn push_cmds(&mut self, cmd_list: TmuxCommands<'a>) -> &mut Self {
        self.subcommands = Some(cmd_list);
        self
    }

    pub fn arg<T, U>(&mut self, flag: T, opt: U) -> &mut Self
    where
        T: Into<Cow<'a, str>>,
        U: Into<Cow<'a, str>>,
    {
        let v = self.args.get_or_insert(Vec::new());
        v.push(flag.into());
        v.push(opt.into());
        self
    }

    // XXX: -> &mut Self, or Self
    pub fn opt<T, U>(&mut self, short: T, opt: U) -> &mut Self
    where
        T: Into<Cow<'a, str>>,
        U: Into<Cow<'a, str>>,
    {
        let v = self.args.get_or_insert(Vec::new());
        v.push(short.into());
        v.push(opt.into());
        self
    }

    pub fn param<T: Into<Cow<'a, str>>>(&mut self, param: T) -> &mut Self {
        self.args.get_or_insert(Vec::new()).push(param.into());
        self
    }

    /// Set `Cmd.combine_short_flags` to `true`
    pub fn combine_short_flags(&mut self) -> &mut Self {
        self.combine_short_flags = true;
        self
    }

    pub fn not_combine_short_flags(&mut self) -> &mut Self {
        self.combine_short_flags = false;
        self
    }

    pub fn combine_short_flags_ext(&mut self, state: bool) -> &mut Self {
        self.combine_short_flags = state;
        self
    }

    /// Set `Cmd.use_alias` to `true`
    // XXX: Remove, replace?
    pub fn use_alias(&mut self) -> &mut Self {
        self.use_alias = true;
        self
    }

    pub fn not_use_alias(&mut self) -> &mut Self {
        self.use_alias = false;
        self
    }

    pub fn use_alias_ext(&mut self, state: bool) -> &mut Self {
        self.use_alias = state;
        self
    }

    //pub fn arg(&mut self, arg: &'a str) -> &mut Self {
    //self.args.get_or_insert(Vec::new()).push(arg);
    //self
    //}

    // TODO: custom command
    //pub fn custom<S: Into<Cow<'a, str>>>(&self, ) -> &mut Self {
    //}

    //// create `std::process::Command` from `Self` (consuming `Self`)
    //pub fn to_command(&self) -> Command {
    //Command::from(self)
    //}

    // NOTE: can't be consuming `to_vec(self)`, borrowing used in `fmt(&self)`
    /// Transform `Cmd` to `Vec<Cow<'a, str>>`
    pub fn to_vec(&self) -> Vec<Cow<'a, str>> {
        let mut v: Vec<Cow<'a, str>> = Vec::new();

        if let Some(envs) = &self.envs {
            for (key, value) in envs {
                v.push(Cow::Owned(format!("{}={}", key, value)));
            }
        }

        // XXX: ugly
        if self.use_alias {
            if let Some(alias) = &self.alias {
                v.push(alias.clone());
            } else if let Some(name) = &self.name {
                v.push(name.clone());
            }
        } else if let Some(name) = &self.name {
            v.push(name.clone());
        }

        if let Some(flags_short) = &self.flags_short {
            if self.combine_short_flags {
                v.push(Cow::Owned(format!("-{}", flags_short)));
            } else {
                for c in flags_short.chars() {
                    v.push(Cow::Owned(format!("-{}", c)));
                }
            }
        }

        if let Some(args) = &self.args {
            v.extend(args.to_vec());
        }

        if let Some(cmds) = &self.subcommands {
            v.extend(cmds.to_vec());
        }

        v
    }

    /// Transform `Cmd` into [`std::process::Command`]
    pub fn to_command(self) -> Command {
        let name = self.name.as_ref().unwrap_or(&Cow::Borrowed(""));
        let mut command = Command::new(name.as_ref());

        if let Some(envs) = self.envs {
            command.envs(
                envs.iter()
                    .map(|(key, value)| (key.as_ref(), value.as_ref())),
            );
        }

        if let Some(args) = self.args {
            command.args(args.iter().map(|arg| arg.as_ref()));
        }

        // additional subcommands
        if let Some(cmds) = self.subcommands {
            command.args(cmds.to_vec().iter().map(|arg| arg.as_ref()));
        }

        //if let Some(stdin) = self.stdin {
        //command.stdin(stdin);
        //}

        //if let Some(stdout) = self.stdout {
        //command.stdout(stdout);
        //}

        //if let Some(stderr) = self.stderr {
        //command.stderr(stderr);
        //}

        command
    }

    //pub fn into_tmux_command(self) -> TmuxCommand<'a> {
    //TmuxCommand::default()
    //}

    //pub fn into_tmux_bin_command_ext(self, tmux: TmuxBin<'a>) -> TmuxBinCommand<'a> {
    //TmuxBinCommand {
    //tmux: tmux,
    //command: self,
    //}
    //}

    //pub fn append_to(self, cmds: &mut TmuxCommands<'a>) {
    //cmds.push(self);
    //}

    //pub fn writeln(self, stdin: &mut ChildStdin) -> Result<(), std::io::Error> {
    //writeln!(stdin, "{}", self.to_string())
    //}
    //pub fn into_vec(self) -> Vec<&'a str> {
    //let mut v = Vec::new();

    //v.push(self.name);

    //for cmd in self.cmds.cmds {
    //v.append(&mut cmd.into_vec());
    //}

    //for arg in self.args.args {
    ////v.append(&mut args.into_vec());
    //match arg {
    //Arg::Flag(flag) => {
    //if let Some(short) = flag.get_short() {
    //v.push(short);
    //}
    //}
    //Arg::Opt(opt) => {}
    //Arg::Param(param) => {}
    //_ => {}
    //};
    //}

    //v
    //}
}

// create ready to exec [`std::process::Command`]
// * create [`std::process::Command`]
// * push environment variables
// * push binary arguments
// * push subcommand
impl<'a> From<&TmuxCommand<'a>> for Command {
    fn from(cmd: &TmuxCommand) -> Self {
        // user given command or blank command
        let name = cmd.name.as_ref().unwrap_or(&Cow::Borrowed(EMPTY_CMD));
        let mut command = Command::new(name.as_ref());

        // environment variables
        if let Some(envs) = &cmd.envs {
            command.envs(
                envs.iter()
                    .map(|(key, value)| (key.as_ref(), value.as_ref())),
            );
        }

        // arguments
        if let Some(args) = &cmd.args {
            command.args(args.iter().map(|arg| arg.as_ref()));
        }

        // subcommands
        if let Some(cmds) = &cmd.subcommands {
            command.args(cmds.to_vec().iter().map(|arg| arg.as_ref()));
        }

        command
    }
}

// create ready to exec [`std::process::Command`]
// * create [`std::process::Command`]
// * push environment variables
// * push binary arguments
// * push subcommand
impl<'a> From<TmuxCommand<'a>> for Command {
    fn from(cmd: TmuxCommand) -> Self {
        Command::from(&cmd)
    }
}

/* NOTE: from bin
    /// Returns mutable reference to tmux executable name `Cow<'a, str>`
    ///
    /// # Examples
    /// ```
    /// use std::borrow::Cow;
    /// use tmux_interface::commands::tmux_bin::TmuxBin;
    ///
    /// let mut tmux = TmuxBin::default();
    /// let bin = tmux.bin();
    /// assert_eq!(bin, &Cow::Borrowed("tmux"));
    /// ```
    pub fn bin(&self) -> &Cow<'a, str> {
        &self.bin
    }

    /// Returns mutable reference to tmux executable name `Cow<'a, str>`
    ///
    /// # Examples
    /// ```
    /// use std::borrow::Cow;
    /// use tmux_interface::commands::tmux_bin::TmuxBin;
    ///
    /// let mut tmux = TmuxBin::default();
    /// *tmux.bin_mut() = Cow::Borrowed("/usr/bin/tmux");
    /// assert_eq!(tmux.bin, Cow::Borrowed("/usr/bin/tmux"));
    /// ```
    /// or
    /// ```
    /// use std::borrow::Cow;
    /// use tmux_interface::commands::tmux_bin::TmuxBin;
    ///
    /// let mut tmux = TmuxBin::default();
    /// *tmux.bin_mut() = "/usr/bin/tmux".into();
    /// assert_eq!(tmux.bin, Cow::Borrowed("/usr/bin/tmux"));
    /// ```
    pub fn bin_mut(&mut self) -> &mut Cow<'a, str> {
        &mut self.bin
    }

*/

impl<'a> TmuxCommand<'a> {
    pub fn add_command(mut self, command: TmuxCommand<'a>) -> Self {
        self.push_cmd(command);
        self
    }

    // TODO: custom command
    //pub fn custom<S: Into<Cow<'a, str>>>(&self, ) -> &mut Self {
    //}

    //// create `std::process::Command` from `Self` (consuming `Self`)
    //pub fn to_command(&self) -> Command {
    //Command::from(self)
    //}

    /// create [`Tmux`] from [`TmuxCommand`]
    pub fn into_tmux(self) -> Tmux<'a> {
        Tmux::new().command(self)
    }

    //pub fn into_tmux_command(self) -> TmuxCommand<'a> {
    //TmuxCommand::default()
    //}

    //pub fn into_tmux_bin_command_ext(self, tmux: TmuxBin<'a>) -> TmuxBinCommand<'a> {
    //TmuxBinCommand {
    //tmux: tmux,
    //command: self,
    //}
    //}

    //pub fn append_to(self, cmds: &mut TmuxCommands<'a>) {
    //cmds.push(self);
    //}

    //pub fn writeln(self, stdin: &mut ChildStdin) -> Result<(), std::io::Error> {
    //writeln!(stdin, "{}", self.to_string())
    //}
}

//pub trait BuildCommand<'a> {
//fn build(self) -> TmuxCommand<'a>;
//}

// trait used for bin_args and cmd_args
//pub trait Args<'a> {
//fn push_param<S: AsRef<str>>(&mut self, param: S);
//fn push_option<S, U>(&mut self, key: S, option: U)
//where
//S: AsRef<str>,
//U: AsRef<str>;
//fn push_flag<S: AsRef<str>>(&mut self, flag: S);
//}

// trait used for bin_args and cmd_args
//impl<'a> Args<'a> for Option<Vec<Cow<'a, str>>> {
//fn push_param<S: Into<Cow<'a, str>>>(&mut self, param: S) {
//self.get_or_insert(Vec::new()).push(param.into());
//}

//fn push_option<S, U>(&mut self, key: S, option: U)
//where
//S: AsRef<Cow<'a, str>>,
//U: Into<Cow<'a, str>>,
//{
//self.get_or_insert(Vec::new())
//.extend_from_slice(&[key.into(), option.into()]);
//}

//fn push_flag<S: Into<Cow<'a, str>>>(&mut self, flag: S) {
//self.push_param(flag.into());
//}
//}
//    /// Returns mutable reference to tmux executable name `Cow<'a, str>`
/* NOTE: from bin
    ///
    /// # Examples
    /// ```
    /// use std::borrow::Cow;
    /// use tmux_interface::commands::tmux_bin::TmuxBin;
    ///
    /// let mut tmux = TmuxBin::default();
    /// let bin = tmux.bin();
    /// assert_eq!(bin, &Cow::Borrowed("tmux"));
    /// ```
    pub fn bin(&self) -> &Cow<'a, str> {
        &self.bin
    }

    /// Returns mutable reference to tmux executable name `Cow<'a, str>`
    ///
    /// # Examples
    /// ```
    /// use std::borrow::Cow;
    /// use tmux_interface::commands::tmux_bin::TmuxBin;
    ///
    /// let mut tmux = TmuxBin::default();
    /// *tmux.bin_mut() = Cow::Borrowed("/usr/bin/tmux");
    /// assert_eq!(tmux.bin, Cow::Borrowed("/usr/bin/tmux"));
    /// ```
    /// or
    /// ```
    /// use std::borrow::Cow;
    /// use tmux_interface::commands::tmux_bin::TmuxBin;
    ///
    /// let mut tmux = TmuxBin::default();
    /// *tmux.bin_mut() = "/usr/bin/tmux".into();
    /// assert_eq!(tmux.bin, Cow::Borrowed("/usr/bin/tmux"));
    /// ```
    pub fn bin_mut(&mut self) -> &mut Cow<'a, str> {
        &mut self.bin
    }

*/