Skip to content

Commit

Permalink
Remove subshells in _lp_battery()
Browse files Browse the repository at this point in the history
_lp_battery() now returns the battery percentage in lp_battery.
It also returns 5 when disabled (still 4 when full or no data).
_lp_battery_color() now returns the formatted string in
lp_battery_color.
It also returns 2 when disabled (still 1 when no string returned).
Add a version of _lp_battery() for when OS is not Linux or Darwin that
only returns 5.
  • Loading branch information
Rycieos committed Nov 25, 2020
1 parent 40c4331 commit 3f57231
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions liquidprompt
Original file line number Diff line number Diff line change
Expand Up @@ -1362,58 +1362,61 @@ _lp_wifi() {
# returns 2 (and battery level) if battery is charging but under threshold
# returns 3 (and battery level) if battery is charging and above threshold
# returns 4 if no battery support
# returns 5 if disabled
case "$LP_OS" in
Linux)
_lp_battery() {
(( LP_ENABLE_BATT )) || return 4
(( LP_ENABLE_BATT )) || return 5
local acpi
acpi="$(acpi --battery 2>/dev/null)"
# Extract the battery load value in percent
# First, remove the beginning of the line...
local bat="${acpi#Battery *, }"
bat="${bat%%%*}" # remove everything starting at '%'
lp_battery="${acpi#Battery *, }"
lp_battery="${lp_battery%%%*}" # remove everything starting at '%'

if [[ -z "${bat}" ]]; then
if [[ -z "${lp_battery}" ]]; then
# no battery level found
return 4
fi
echo -nE "$bat"
# discharging
if [[ "$acpi" == *"Discharging"* ]]; then
# under => 0, above => 1
return $(( bat > LP_BATTERY_THRESHOLD ))
return $(( lp_battery > LP_BATTERY_THRESHOLD ))
# not charging
elif [[ "$acpi" == *"Not charging"* ]]; then
return 4
# charging
else
# under => 2, above => 3
return $(( 2 + ( bat > LP_BATTERY_THRESHOLD ) ))
return $(( 2 + ( lp_battery > LP_BATTERY_THRESHOLD ) ))
fi
}
;;
Darwin)
_lp_battery() {
(( LP_ENABLE_BATT )) || return 4
(( LP_ENABLE_BATT )) || return 5
local percent batt_status
eval "$(pmset -g batt | sed -n 's/^ -InternalBattery.*[[:space:]]\([0-9]*[0-9]\)%; \([^;]*\).*$/percent=\1 batt_status='\'\\2\'/p)"
eval "$(pmset -g batt | sed -n 's/^ -InternalBattery.*[[:space:]]\([0-9]*[0-9]\)%; \([^;]*\).*$/lp_battery=\1 batt_status='\'\\2\'/p)"
case "$batt_status" in
charged | "")
return 4
;;
discharging)
echo -nE "$percent"
# under => 0, above => 1
return $(( percent > LP_BATTERY_THRESHOLD ))
return $(( lp_battery > LP_BATTERY_THRESHOLD ))
;;
*) # "charging", "AC attached"
echo -nE "$percent"
# under => 2, above => 3
return $(( 2 + ( percent > LP_BATTERY_THRESHOLD ) ))
return $(( 2 + ( lp_battery > LP_BATTERY_THRESHOLD ) ))
;;
esac
}
;;
*)
_lp_battery() {
return 5
}
;;
esac

# Compute a gradient of background/foreground colors depending on the battery status
Expand All @@ -1423,33 +1426,33 @@ esac
# a yellow ⌁ if the battery is discharging but above threshold
# a red ⌁ if the battery is discharging and above threshold
_lp_battery_color() {
(( LP_ENABLE_BATT )) || return
(( LP_ENABLE_BATT )) || return 2

local mark=$LP_MARK_BATTERY
local chargingmark=$LP_MARK_ADAPTER
local -i bat ret
bat="$(_lp_battery)"
_lp_battery
ret=$?
bat=$lp_battery

if (( ret == 4 || bat == 100 )); then
if (( ret >= 4 || bat == 100 )); then
# no battery support or battery full: nothing displayed
:
return 1
elif (( ret == 3 && bat != 100 )); then
# charging and above threshold and not 100%
# green ⏚
echo -nE "${LP_COLOR_CHARGING_ABOVE}$chargingmark${NO_COL}"
lp_battery_color="${LP_COLOR_CHARGING_ABOVE}$chargingmark${NO_COL}"
elif (( ret == 2 )); then
# charging but under threshold
# yellow ⏚
echo -nE "${LP_COLOR_CHARGING_UNDER}$chargingmark${NO_COL}"
lp_battery_color="${LP_COLOR_CHARGING_UNDER}$chargingmark${NO_COL}"
elif (( ret == 1 )); then
# discharging but above threshold
# yellow ⌁
echo -nE "${LP_COLOR_DISCHARGING_ABOVE}$mark${NO_COL}"
lp_battery_color="${LP_COLOR_DISCHARGING_ABOVE}$mark${NO_COL}"
# discharging and under threshold
else
local res
res="${LP_COLOR_DISCHARGING_UNDER}${mark}${NO_COL}"
lp_battery_color="${LP_COLOR_DISCHARGING_UNDER}${mark}${NO_COL}"

if (( LP_PERCENTS_ALWAYS )); then
local -i idx
Expand Down Expand Up @@ -1477,9 +1480,8 @@ _lp_battery_color() {
idx=0
fi

res+="${LP_COLORMAP[idx+_LP_FIRST_INDEX]}${bat}$_LP_PERCENT${NO_COL}"
lp_battery_color+="${LP_COLORMAP[idx+_LP_FIRST_INDEX]}${bat}$_LP_PERCENT${NO_COL}"
fi # LP_PERCENTS_ALWAYS
echo -nE "${res}"
fi
}

Expand Down Expand Up @@ -1769,7 +1771,11 @@ _lp_theme_prompt() {
fi
LP_TEMP="$(_lp_sr "$(_lp_temperature)")"
LP_LOAD="$(_lp_sr "$(_lp_load_color)")"
LP_BATT="$(_lp_sr "$(_lp_battery_color)")"
if _lp_battery_color; then
LP_BATT="$lp_battery_color "
else
LP_BATT=
fi
_lp_time
_lp_sudo_check

Expand Down

0 comments on commit 3f57231

Please sign in to comment.