From 14a038bbdaf444adead2aba0fdf9c6564624850f Mon Sep 17 00:00:00 2001 From: Li Haoyuan <1513624626@qq.com> Date: Wed, 10 Jul 2024 13:40:32 +0800 Subject: [PATCH 1/4] feat: Add battery prompt for Windows --- internal/p10k.zsh | 53 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/internal/p10k.zsh b/internal/p10k.zsh index d2d261b2e..fd030c5a9 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -1332,7 +1332,6 @@ _p9k_prompt_fvm_init() { } ################################################################ -# Segment that displays the battery status in levels and colors prompt_battery() { [[ $_p9k_os == (Linux|Android) ]] && _p9k_prompt_battery_set_args (( $#_p9k__battery_args )) && _p9k_prompt_segment "${_p9k__battery_args[@]}" @@ -1344,6 +1343,16 @@ _p9k_prompt_battery_init() { _p9k__async_segments_compute+='_p9k_worker_invoke battery _p9k_prompt_battery_compute' return fi + + if [[ $_p9k_os == Windows ]]; then + if command -v wmic > /dev/null 2>&1; then # Test "wimc" command + _p9k__async_segments_compute+='_p9k_worker_invoke battery _p9k_prompt_battery_compute' + return + else + typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${:-}' + fi + fi + if [[ $_p9k_os != (Linux|Android) || -z /sys/class/power_supply/(CMB*|BAT*|*battery)/(energy_full|charge_full|charge_counter)(#qN) ]]; then typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${:-}' @@ -1456,6 +1465,48 @@ _p9k_prompt_battery_set_args() { fi ;; + Windows) + # See Win32_Battery class members at https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-battery + battery_data=$(wmic path Win32_Battery get EstimatedChargeRemaining,BatteryStatus,EstimatedRunTime /format:list) + bat_percent=$(echo "$battery_data" | grep "EstimatedChargeRemaining" | awk -F'=' '{print $2}') + bat_status=$(echo "$battery_data" | grep "BatteryStatus" | awk -F'=' '{print $2}') + bat_remain_minutes=$(echo "$battery_data" | grep "EstimatedRunTime" | awk -F'=' '{print $2}') + + # Missing critical parameters + if [[ -z "$bat_percent" || -z "$bat_status" || -z "$bat_remain_minutes" ]]; then + return 0 + fi + + case $bat_status in + '1'|'4'|'5') + # When the battery is charging, system will show that bat_remain_minutes is 71582788 (min) + (( bat_remain_minutes < 71582788 )) && remain=$((bat_remain_minutes/60)):${(l#2##0#)$((bat_remain_minutes%60))} || remain='' + if (( bat_percent < _POWERLEVEL9K_BATTERY_LOW_THRESHOLD )); then + state=LOW + else + state=DISCONNECTED + fi + ;; + '2'|'6'|'7'|'8'|'9'|'11') + if (( bat_percent == 100 )); then + remain='' + state=CHARGED + else + (( bat_remain_minutes < 71582788 )) && remain=$((bat_remain_minutes/60)):${(l#2##0#)$((bat_remain_minutes%60))} || remain='' + state=CHARGING + fi + ;; + '3') + (( bat_remain_minutes < 71582788 )) && remain=$((bat_remain_minutes/60)):${(l#2##0#)$((bat_remain_minutes%60))} || remain='' + state=CHARGED + ;; + *) + state=CHARGED + remain='' + ;; + esac + ;; + *) return 0 ;; From 39e1c51ae302842fb32af9888e08a2eb96fc7c77 Mon Sep 17 00:00:00 2001 From: Li Haoyuan <1513624626@qq.com> Date: Wed, 10 Jul 2024 13:44:46 +0800 Subject: [PATCH 2/4] feat: Add segment comment --- internal/p10k.zsh | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/p10k.zsh b/internal/p10k.zsh index fd030c5a9..f2c252bcb 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -1332,6 +1332,7 @@ _p9k_prompt_fvm_init() { } ################################################################ +# Segment that displays the battery status in levels and colors prompt_battery() { [[ $_p9k_os == (Linux|Android) ]] && _p9k_prompt_battery_set_args (( $#_p9k__battery_args )) && _p9k_prompt_segment "${_p9k__battery_args[@]}" From bc0f9341da724cefe31ddced91ebb78cf4d6fb48 Mon Sep 17 00:00:00 2001 From: Li Haoyuan <1513624626@qq.com> Date: Fri, 12 Jul 2024 02:28:25 +0800 Subject: [PATCH 3/4] refactor: Change the method of parsing wmic output, improve performance. --- internal/p10k.zsh | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/internal/p10k.zsh b/internal/p10k.zsh index f2c252bcb..2b64543a1 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -1467,20 +1467,27 @@ _p9k_prompt_battery_set_args() { ;; Windows) - # See Win32_Battery class members at https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-battery - battery_data=$(wmic path Win32_Battery get EstimatedChargeRemaining,BatteryStatus,EstimatedRunTime /format:list) - bat_percent=$(echo "$battery_data" | grep "EstimatedChargeRemaining" | awk -F'=' '{print $2}') - bat_status=$(echo "$battery_data" | grep "BatteryStatus" | awk -F'=' '{print $2}') - bat_remain_minutes=$(echo "$battery_data" | grep "EstimatedRunTime" | awk -F'=' '{print $2}') + local raw_data + local -a info_values + # See definitions to Win32_Battery class members at https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-battery + { + read -r 2>/dev/null + read -r raw_data + } <<< "$(wmic path Win32_Battery get BatteryStatus,EstimatedChargeRemaining,EstimatedRunTime)" - # Missing critical parameters - if [[ -z "$bat_percent" || -z "$bat_status" || -z "$bat_remain_minutes" ]]; then - return 0 - fi + # info_values[1]: BatteryStatus + # info_values[2]: EstimatedChargeRemaining + # info_values[3]: EstimatedRunTime + info_values=(${(s: :)raw_data}) + + (( ${#info_values[@]} == 4 )) || return # Missing info_values (index [4] is for the following CRCR) + + bat_percent=${info_values[2]} + local -i bat_remain_minutes=${info_values[3]} - case $bat_status in + case ${info_values[1]} in '1'|'4'|'5') - # When the battery is charging, system will show that bat_remain_minutes is 71582788 (min) + # When the battery is charging or full, system will show that bat_remain_minutes (EstimatedRunTime) is 71582788 (min) (( bat_remain_minutes < 71582788 )) && remain=$((bat_remain_minutes/60)):${(l#2##0#)$((bat_remain_minutes%60))} || remain='' if (( bat_percent < _POWERLEVEL9K_BATTERY_LOW_THRESHOLD )); then state=LOW From 71d628cc8a4b157f7e6a8546a68f2c85efde9b39 Mon Sep 17 00:00:00 2001 From: Li Haoyuan <1513624626@qq.com> Date: Fri, 12 Jul 2024 02:37:25 +0800 Subject: [PATCH 4/4] fix: Fix the logic of displaying the remain time when battery is charged or charging. --- internal/p10k.zsh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/internal/p10k.zsh b/internal/p10k.zsh index 2b64543a1..f4c03fa37 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -1495,12 +1495,11 @@ _p9k_prompt_battery_set_args() { state=DISCONNECTED fi ;; - '2'|'6'|'7'|'8'|'9'|'11') + '2'|'6'|'7'|'8'|'9'|'11') # In this case, the battery is likely to be charged and disconnected, or still been charging. + remain='' if (( bat_percent == 100 )); then - remain='' state=CHARGED else - (( bat_remain_minutes < 71582788 )) && remain=$((bat_remain_minutes/60)):${(l#2##0#)$((bat_remain_minutes%60))} || remain='' state=CHARGING fi ;;