diff --git a/src/modules/battery.rs b/src/modules/battery.rs index 6a0396b1..95cd8aa3 100644 --- a/src/modules/battery.rs +++ b/src/modules/battery.rs @@ -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> { - // 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> { _ => None, }) .map(|variable| match variable { - "percentage" => Some(Ok(format!("{}{}", percentage.round(), percentage_char))), + "percentage" => Some(Ok(format!("{}%", percentage.round()))), _ => None, }); diff --git a/src/utils.rs b/src/utils.rs index a6c26ded..910bf9be 100644 --- a/src/utils.rs +++ b/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 { - // 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"),