1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-09-27 20:59:02 +00:00

fix(zsh): always escape percent character (#3058)

This commit is contained in:
David Knaack 2021-09-13 01:55:46 +02:00 committed by GitHub
parent e64a99262e
commit 5ac7ad741f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 17 deletions

View File

@ -1,4 +1,4 @@
use super::{Context, Module, RootModuleConfig, Shell};
use super::{Context, Module, RootModuleConfig};
use crate::configs::battery::BatteryConfig;
#[cfg(test)]
use mockall::automock;
@ -7,13 +7,6 @@ use crate::formatter::StringFormatter;
/// Creates a module for the battery percentage and charging state
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
// TODO: Update when v1.0 printing refactor is implemented to only
// print escapes in a prompt context.
let percentage_char = match context.shell {
Shell::Zsh => "%%", // % is an escape in zsh, see PROMPT in `man zshmisc`
_ => "%",
};
let battery_status = get_battery_status(context)?;
let BatteryStatus { state, percentage } = battery_status;
@ -55,7 +48,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
_ => None,
})
.map(|variable| match variable {
"percentage" => Some(Ok(format!("{}{}", percentage.round(), percentage_char))),
"percentage" => Some(Ok(format!("{}%", percentage.round()))),
_ => None,
});

View File

@ -314,15 +314,24 @@ CMake suite maintained and supported by Kitware (kitware.com/cmake).\n",
}
}
/// Wraps ANSI color escape sequences in the shell-appropriate wrappers.
/// Wraps ANSI color escape sequences and other interpretable characters
/// that need to be escaped in the shell-appropriate wrappers.
pub fn wrap_colorseq_for_shell(mut ansi: String, shell: Shell) -> String {
// Bash might interepret baskslashes, backticks and $
// see #658 for more details
if shell == Shell::Bash {
ansi = ansi.replace('\\', r"\\");
ansi = ansi.replace('$', r"\$");
ansi = ansi.replace('`', r"\`");
}
// Handle other interpretable characters
match shell {
// Bash might interepret baskslashes, backticks and $
// see #658 for more details
Shell::Bash => {
ansi = ansi.replace('\\', r"\\");
ansi = ansi.replace('$', r"\$");
ansi = ansi.replace('`', r"\`");
}
Shell::Zsh => {
// % is an escape in zsh, see PROMPT in `man zshmisc`
ansi = ansi.replace('%', "%%");
}
_ => {}
};
const ESCAPE_BEGIN: char = '\u{1b}';
const ESCAPE_END: char = 'm';
@ -679,6 +688,15 @@ mod tests {
);
}
#[test]
fn test_zsh_escape() {
let test = "10%";
assert_eq!(wrap_colorseq_for_shell(test.to_owned(), Shell::Zsh), "10%%");
assert_eq!(
wrap_colorseq_for_shell(test.to_owned(), Shell::PowerShell),
test
);
}
#[test]
fn test_get_command_string_output() {
let case1 = CommandOutput {
stdout: String::from("stdout"),