fix(zsh): always escape percent character (#3058)
This commit is contained in:
parent
e64a99262e
commit
5ac7ad741f
|
@ -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,
|
||||
});
|
||||
|
||||
|
|
22
src/utils.rs
22
src/utils.rs
|
@ -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 {
|
||||
// Handle other interpretable characters
|
||||
match shell {
|
||||
// Bash might interepret baskslashes, backticks and $
|
||||
// see #658 for more details
|
||||
if shell == Shell::Bash {
|
||||
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"),
|
||||
|
|
Loading…
Reference in New Issue