summaryrefslogtreecommitdiffstats
path: root/src/init/mod.rs
blob: 2450d7ac6b16e7ef18645b7d0524b7cf66734a90 (plain)
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
use crate::utils::create_command;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::{env, io};

/* We use a two-phase init here: the first phase gives a simple command to the
shell. This command evaluates a more complicated script using `source` and
process substitution.

Directly using `eval` on a shell script causes it to be evaluated in
a single line, which sucks because things like comments will comment out the
rest of the script, and you have to spam semicolons everywhere. By using
source and process substitutions, we make it possible to comment and debug
the init scripts.

In the future, this may be changed to just directly evaluating the initscript
using whatever mechanism is available in the host shell--this two-phase solution
has been developed as a compatibility measure with `eval $(starship init X)`
*/

struct StarshipPath {
    native_path: PathBuf,
}
impl StarshipPath {
    fn init() -> io::Result<Self> {
        Ok(Self {
            native_path: env::current_exe()?,
        })
    }
    fn str_path(&self) -> io::Result<&str> {
        let current_exe = self
            .native_path
            .to_str()
            .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "can't convert to str"))?;
        Ok(current_exe)
    }

    /// Returns POSIX quoted path to starship binary
    fn sprint(&self) -> io::Result<String> {
        self.str_path().map(|p| shell_words::quote(p).into_owned())
    }

    /// PowerShell specific path escaping
    fn sprint_pwsh(&self) -> io::Result<String> {
        self.str_path()
            .map(|s| s.replace("'", "''"))
            .map(|s| format!("'{}'", s))
    }
    fn sprint_posix(&self) -> io::Result<String> {
        // On non-Windows platform, return directly.
        if cfg!(not(target_os = "windows")) {
            return self.sprint();
        }
        let str_path = self.str_path()?;
        let res = create_command("cygpath").and_then(|mut cmd| cmd.arg(str_path).output());
        let output = match res {
            Ok(output) => output,
            Err(e) => {
                if e.kind() != io::ErrorKind::NotFound {
                    log::warn!("Failed to convert \"{}\" to unix path:\n{:?}", str_path, e);
                }
                // Failed to execute cygpath.exe means there're not inside cygwin evironment,return directly.
                return self.sprint();
            }
        };
        let res = String::from_utf8(output.stdout);
        let posix_path = match res {
            Ok(ref cygpath_path) if output.status.success() => cygpath_path.trim(),
            Ok(_) => {
                log::warn!(
                    "Failed to convert \"{}\" to unix path:\n{}",
                    str_path,
                    String::from_utf8_lossy(&output.stderr),
                );
                str_path
            }
            Err(e) => {
                log::warn!("Failed to convert \"{}\" to unix path:\n{}", str_path, e);
                str_path
            }
        };
        Ok(shell_words::quote(posix_path).into_owned())
    }
}

/* This prints the setup stub, the short piece of code which sets up the main
init code. The stub produces the main init script, then evaluates it with
`source` and process substitution */
pub fn init_stub(shell_name: &str) -> io::Result<()> {
    log::debug!("Shell name: {}", shell_name);

    let shell_basename = Path::new(shell_name)
        .file_stem()
        .and_then(OsStr::to_str)
        .unwrap_or(shell_name);

    let starship = StarshipPath::init()?;

    match shell_basename {
        "bash" => print!(
            /*
             * The standard bash bootstrap is:
             *      `source <(starship init bash --print-full-init)`
             *
             * Unfortunately there is an issue with bash 3.2 (the MacOS
             * default) which prevents this from working. It does not support
             * `source` with process substitution.
             *
             * There are more details here: https://stackoverflow.com/a/32596626
             *
             * The workaround for MacOS is to use the `/dev/stdin` trick you
             * see below. However, there are some systems with emulated POSIX
             * environments which do not support `/dev/stdin`. For example,
             * `Git Bash` within `Git for Windows and `Termux` on Android.
             *
             * Fortunately, these apps ship with recent-ish versions of bash.
             * Git Bash is currently shipping bash 4.4 and Termux is shipping
             * bash 5.0.
             *
             * Some testing has suggested that bash 4.0 is also incompatible
             * with the standard bootstrap, whereas bash 4.1 appears to be
             * consistently compatible.
             *
             * The upshot of all of this, is that we will use the standard
             * bootstrap whenever the bash version is 4.1 or higher. Otherwise,
             * we fall back to the `/dev/stdin` solution.
             *
             * More background can be found in these pull requests:
             * https://github.com/starship/starship/pull/241
             * https://github.com/starship/starship/pull/278
             */
            r#"
            __main() {{
                local major="${{BASH_VERSINFO[0]}}"
                local minor="${{BASH_VERSINFO[1]}}"

                if ((major > 4)) || {{ ((major == 4)) && ((minor >= 1)); }}; then
                    source <("{0}" init bash --print-full-init)
                else
                    source /dev/stdin <<<"$("{0}" init bash --print-full-init)"
                fi
            }}
            __main
            unset -f __main
            "#,
            starship.sprint_posix()?
        ),
        "zsh" => print!(
            r#"source <({} init zsh --print-full-init)"#,
            starship.sprint_posix()?
        ),
        "fish" => print!(
            // Fish does process substitution with pipes and psub instead of bash syntax
            r#"source ({} init fish --print-full-init | psub)"#,
            starship.sprint_posix()?
        ),
        "powershell" => print!(
            r#"Invoke-Expression (& {} init powershell --print-full-init | Out-String)"#,
            starship.sprint_pwsh()?
        ),
        "ion" => print!("eval $({} init ion --print-full-init)", starship.sprint()?),
        "elvish" => print!(
            r#"eval ({} init elvish --print-full-init | slurp)"#,
            starship.sprint_posix()?
        ),
        "tcsh" => print!(
            r#"eval `({} init tcsh --print-full-init)`"#,
            starship.sprint_posix()?
        ),
        "nu" => print_script(NU_INIT, &StarshipPath::init()?.sprint_posix()?),
        "xonsh" => print!(
            r#"execx($({} init xonsh --print-full-init))"#,
            starship.sprint_posix()?
        ),
        _ => {
            let quoted_arg = shell_words::quote(shell_basename);
            println!(
                "printf \"\\n%s is not yet supported by starship.\\n\
                 For the time being, we support the following shells:\\n\
                 * bash\\n\
                 * elvish\\n\
                 * fish\\n\
                 * ion\\n\
                 * powershell\\n\
                 * tcsh\\n\
                 * zsh\\n\
                 * nu\\n\
                 * xonsh\\n\
                 \\n\
                 Please open an issue in the starship repo if you would like to \
                 see support for %s:\\nhttps://github.com/starship/starship/issues/new\\n\\n\" {0} {0}",
                quoted_arg
            )
        }
    };
    Ok(())
}

/* This function (called when `--print-full-init` is passed to `starship init`)
prints out the main initialization script */
pub fn init_main(shell_name: &str) -> io::Result<()> {
    let starship_path = StarshipPath::init()?;

    match shell_name {
        "bash" => print_script(BASH_INIT, &starship_path.sprint_posix()?),
        "zsh" => print_script(ZSH_INIT, &starship_path.sprint_posix()?),
        "fish" => print_script(FISH_INIT, &starship_path.sprint_posix()?),
        "powershell" => print_script(PWSH_INIT, &starship_path.sprint_pwsh()?),
        "ion" => print_script(ION_INIT, &starship_path.sprint()?),
        "elvish" => print_script(ELVISH_INIT, &starship_path.sprint_posix()?),
        "tcsh" => print_script(TCSH_INIT, &starship_path.sprint_posix()?),
        "xonsh" => print_script(XONSH_INIT, &starship_path.sprint_posix()?),
        _ => {
            println!(
                "printf \"Shell name detection failed on phase two init.\\n\
                 This probably indicates a bug within starship: please open\\n\
                 an issue at https://github.com/starship/starship/issues/new\\n\""
            );
        }
    }
    Ok(())
}

fn print_script(script: &str, path: &str) {
    let script = script.replace("::STARSHIP::", path);
    print!("{}", script);
}

/* GENERAL INIT SCRIPT NOTES

Each init script will be passed as-is. Global notes for init scripts are in this
comment, with additional per-script comments in the strings themselves.

JOBS: The argument to `--jobs` is quoted because MacOS's `wc` leaves whitespace
in the output. We pass it to starship and do the whitespace removal in Rust,
to avoid the cost of an additional shell fork every shell draw.

Note that the init scripts are not in their final form--they are processed by
`starship init` prior to emitting the final form. In this processing, some tokens
are replaced, e.g. `::STARSHIP::` is replaced by the full path to the
starship binary.
*/

const BASH_INIT: &str = include_str!("starship.bash");

const ZSH_INIT: &str = include_str!("starship.zsh");

const FISH_INIT: &str = include_str!("starship.fish");

const PWSH_INIT: &str = include_str!("starship.ps1");

const ION_INIT: &str = include_str!("starship.ion");

const ELVISH_INIT: &str = include_str!("starship.elv");

const TCSH_INIT: &str = include_str!("starship.tcsh");

const NU_INIT: &str = include_str!("starship.nu");

const XONSH_INIT: &str = include_str!("starship.xsh");

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn escape_pwsh() -> io::Result<()> {
        let starship_path = StarshipPath {
            native_path: PathBuf::from(r"C:\starship.exe"),
        };
        assert_eq!(starship_path.sprint_pwsh()?, r"'C:\starship.exe'");
        Ok(())
    }

    #[test]
    fn escape_tick_pwsh() -> io::Result<()> {
        let starship_path = StarshipPath {
            native_path: PathBuf::from(r"C:\'starship.exe"),
        };
        assert_eq!(starship_path.sprint_pwsh()?, r"'C:\''starship.exe'");
        Ok(())
    }
}