diff --git a/Cargo.lock b/Cargo.lock index 2254b60971..b4d0da0aee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1634,6 +1634,7 @@ dependencies = [ "serde_yaml 0.8.11 (registry+https://github.com/rust-lang/crates.io-index)", "shellexpand 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "starship 0.26.4 (registry+https://github.com/rust-lang/crates.io-index)", + "strip-ansi-escapes 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "sublime_fuzzy 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "subprocess 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", "surf 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2463,6 +2464,14 @@ name = "static_assertions" version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "strip-ansi-escapes" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "vte 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "strsim" version = "0.8.0" @@ -2788,6 +2797,14 @@ name = "void" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "vte" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "utf8parse 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "walkdir" version = "2.2.9" @@ -3281,6 +3298,7 @@ dependencies = [ "checksum starship 0.26.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d31191331ef70afd5c8f88515850ce50ae4a7ad5a9d7d1046eba6ceb8e9707d8" "checksum starship_module_config_derive 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "36d147e2f158842551535289789d4f3ef5a37d4043f6dc96f3a461bb253e69a1" "checksum static_assertions 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7f3eb36b47e512f8f1c9e3d10c2c1965bc992bd9cdb024fa581e2194501c83d3" +"checksum strip-ansi-escapes 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9d63676e2abafa709460982ddc02a3bb586b6d15a49b75c212e06edd3933acee" "checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" "checksum sublime_fuzzy 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bdac3d983d073c19487ba1f5e16eda43e9c6e50aa895d87110d0febe389b66b9" "checksum subprocess 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = "28fc0f40f0c0da73339d347aa7d6d2b90341a95683a47722bc4eebed71ff3c00" @@ -3319,6 +3337,7 @@ dependencies = [ "checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" "checksum version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" +"checksum vte 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4f42f536e22f7fcbb407639765c8fd78707a33109301f834a594758bedd6e8cf" "checksum walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)" = "9658c94fa8b940eab2250bd5a457f9c48b748420d71293b165c8cdbe2f55f71e" "checksum wasi 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b89c3ce4ce14bdc6fb6beaf9ec7928ca331de5df7e5ea278375642a2f478570d" "checksum wasm-bindgen 0.2.54 (registry+https://github.com/rust-lang/crates.io-index)" = "c4568ae1b4e07ca907b1a4de41174eaa3e5be4066c024475586b7842725f69a9" diff --git a/Cargo.toml b/Cargo.toml index 57110f815c..9c64c77c17 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -76,6 +76,7 @@ sublime_fuzzy = "0.6" trash = "1.0.0" regex = "1" cfg-if = "0.1" +strip-ansi-escapes = "0.1.0" neso = { version = "0.5.0", optional = true } crossterm = { version = "0.10.2", optional = true } diff --git a/src/cli.rs b/src/cli.rs index dc83baf7b8..f5488a8e70 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -398,15 +398,20 @@ pub async fn cli() -> Result<(), Box> { let prompt = { #[cfg(feature = "starship-prompt")] { - &starship::print::get_prompt(starship::context::Context::new_with_dir( - clap::ArgMatches::default(), - cwd, + let bytes = strip_ansi_escapes::strip(&starship::print::get_prompt( + starship::context::Context::new_with_dir( + clap::ArgMatches::default(), + cwd.clone(), + ), )) + .unwrap(); + + String::from_utf8_lossy(&bytes).to_string() } #[cfg(not(feature = "starship-prompt"))] { &format!( - "{}{}\x1b[m> ", + "{}{}> ", cwd, match current_branch() { Some(s) => format!("({})", s), @@ -415,10 +420,33 @@ pub async fn cli() -> Result<(), Box> { ) } }; + + let colored_prompt = { + #[cfg(feature = "starship-prompt")] + { + starship::print::get_prompt(starship::context::Context::new_with_dir( + clap::ArgMatches::default(), + cwd, + )) + } + #[cfg(not(feature = "starship-prompt"))] + { + format!( + "{}{}\x1b[m]> ", + cwd, + match current_branch() { + Some(s) => format!("({})", s), + None => "".to_string(), + } + ) + } + }; + + rl.helper_mut().expect("No helper").colored_prompt = colored_prompt; let mut initial_command = Some(String::new()); let mut readline = Err(ReadlineError::Eof); while let Some(ref cmd) = initial_command { - readline = rl.readline_with_initial(prompt, (&cmd, "")); + readline = rl.readline_with_initial(&prompt, (&cmd, "")); if let Err(ReadlineError::Eof) = &readline { // Fuzzy search in history let lines = rl.history().iter().rev().map(|s| s.as_str()).collect(); diff --git a/src/shell/helper.rs b/src/shell/helper.rs index b12c176afb..7e662a7dd5 100644 --- a/src/shell/helper.rs +++ b/src/shell/helper.rs @@ -14,11 +14,15 @@ use std::borrow::Cow::{self, Owned}; pub(crate) struct Helper { context: Context, + pub colored_prompt: String, } impl Helper { pub(crate) fn new(context: Context) -> Helper { - Helper { context } + Helper { + context, + colored_prompt: String::new(), + } } } @@ -41,8 +45,18 @@ impl Hinter for Helper { } impl Highlighter for Helper { - fn highlight_prompt<'b, 's: 'b, 'p: 'b>(&'s self, prompt: &'p str, _: bool) -> Cow<'b, str> { - Owned("\x1b[32m".to_owned() + &prompt + "\x1b[m") + fn highlight_prompt<'b, 's: 'b, 'p: 'b>( + &'s self, + prompt: &'p str, + default: bool, + ) -> Cow<'b, str> { + use std::borrow::Cow::Borrowed; + + if default { + Borrowed(&self.colored_prompt) + } else { + Borrowed(prompt) + } } fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> {