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
use std::ffi::{OsStr, OsString};
use std::io::Result;
use std::path::Path;
use std::process::Command as StdCommand;
use std::process::{Child, ExitStatus, Output, Stdio};

/// Like [`std::process::Command`] but args are iterable in old versions of Rust.
pub struct Command {
    inner: StdCommand,
    cached_args: Vec<OsString>,
}

impl Command {
    pub fn new<S: AsRef<OsStr>>(program: S) -> Command {
        Command {
            inner: StdCommand::new(program),
            cached_args: vec![],
        }
    }

    pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Command {
        self.cached_args.push(OsString::from(arg.as_ref()));
        self.inner.arg(arg);
        self
    }

    pub fn args<I, S>(&mut self, args: I) -> &mut Command
    where
        I: IntoIterator<Item = S>,
        S: AsRef<OsStr>,
    {
        let args: Vec<_> = args.into_iter().collect();
        self.cached_args
            .extend(args.iter().map(|arg| OsString::from(arg)));
        self.inner.args(args);
        self
    }

    pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Command
    where
        K: AsRef<OsStr>,
        V: AsRef<OsStr>,
    {
        self.inner.env(key, val);
        self
    }

    pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Command
    where
        I: IntoIterator<Item = (K, V)>,
        K: AsRef<OsStr>,
        V: AsRef<OsStr>,
    {
        self.inner.envs(vars);
        self
    }

    pub fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Command {
        self.inner.env_remove(key);
        self
    }

    pub fn env_clear(&mut self) -> &mut Command {
        self.inner.env_clear();
        self
    }

    pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Command {
        self.inner.current_dir(dir);
        self
    }

    pub fn stdin<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
        self.inner.stdin(cfg);
        self
    }

    pub fn stdout<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
        self.inner.stdout(cfg);
        self
    }

    pub fn stderr<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
        self.inner.stderr(cfg);
        self
    }

    pub fn spawn(&mut self) -> Result<Child> {
        self.inner.spawn()
    }

    pub fn output(&mut self) -> Result<Output> {
        self.inner.output()
    }

    pub fn status(&mut self) -> Result<ExitStatus> {
        self.inner.status()
    }

    pub fn get_args(&self) -> CommandArgs<'_> {
        CommandArgs {
            inner: self
                .cached_args
                .iter()
                .map(&|arg: &OsString| arg.as_os_str()),
        }
    }
}

pub struct CommandArgs<'cmd> {
    inner: std::iter::Map<
        std::slice::Iter<'cmd, OsString>,
        &'cmd dyn for<'r> Fn(&'r OsString) -> &'r OsStr,
    >, // omg
}

impl<'cmd> Iterator for CommandArgs<'cmd> {
    type Item = &'cmd OsStr;

    fn next(&mut self) -> Option<&'cmd OsStr> {
        self.inner.next()
    }
}

impl std::fmt::Debug for Command {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.inner.fmt(f)
    }
}