From 08aef016cdb1dbf53fc50b6e31ab0add2f40856b Mon Sep 17 00:00:00 2001 From: TsubasaKawajiri <39114857+TsubasaKawajiri@users.noreply.github.com> Date: Mon, 26 Aug 2019 10:52:44 +0900 Subject: [PATCH] fix: battery percentage character on Zsh. #226 (#237) on Zsh, battery percentage character would print % this PR fixes print %{ -> % --- src/init.rs | 5 +++-- src/modules/battery.rs | 9 ++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/init.rs b/src/init.rs index 104dd709..398538f7 100644 --- a/src/init.rs +++ b/src/init.rs @@ -167,8 +167,8 @@ if [[ $preexec_functions ]]; then preexec_functions+=(starship_preexec) precmd_functions+=(starship_precmd) else -# We want to avoid destroying an existing DEBUG hook. If we detect one, create -# a new function that runs both the existing function AND our function, then +# We want to avoid destroying an existing DEBUG hook. If we detect one, create +# a new function that runs both the existing function AND our function, then # re-trap DEBUG to use this new function. This prevents a trap clobber. dbg_trap="$(trap -p DEBUG | cut -d' ' -f3 | tr -d \')" if [[ -z "$dbg_trap" ]]; then @@ -255,4 +255,5 @@ function fish_prompt set -l starship_duration (math --scale=0 "$CMD_DURATION / 1000") ## STARSHIP ## prompt --status=$exit_code --cmd-duration=$starship_duration --jobs=(count (jobs -p)) end +export STARSHIP_SHELL="fish" "##; diff --git a/src/modules/battery.rs b/src/modules/battery.rs index 78ea1813..800fb6d2 100644 --- a/src/modules/battery.rs +++ b/src/modules/battery.rs @@ -8,6 +8,13 @@ pub fn module<'a>(context: &'a Context) -> Option> { const BATTERY_CHARGING: &str = "⇡"; const BATTERY_DISCHARGING: &str = "⇣"; const BATTERY_THRESHOLD: f32 = 10.0; + // TODO: Update when v1.0 printing refactor is implemented to only + // print escapes in a prompt context. + let shell = std::env::var("STARSHIP_SHELL").unwrap_or_default(); + let percentage_char = match shell.as_str() { + "zsh" => "%%", // % is an escape in zsh, see PROMPT in `man zshmisc` + _ => "%", + }; let battery_status = get_battery_status()?; let BatteryStatus { state, percentage } = battery_status; @@ -42,7 +49,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { let mut percent_string = Vec::::with_capacity(2); // Round the percentage to a whole number percent_string.push(percentage.round().to_string()); - percent_string.push("%".to_string()); + percent_string.push(percentage_char.to_string()); module.new_segment("percentage", percent_string.join("").as_ref()); Some(module)