Skip to content

Commit 2685d5e

Browse files
committed
Use echo and Append Percentage Symbol
Instead of straightforwardly calling `cat`, I've encased the command in `echo` to enable appending strings. This allows me to add a percentage symbol after calling `cat "$battery"`! This also suppresses any `echo`s for machines without battery (i.e. a desktop environment)
1 parent 5c52d4f commit 2685d5e

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

scripts/battery_percentage.sh

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,43 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
55
source "$CURRENT_DIR/helpers.sh"
66

77
print_battery_percentage() {
8-
# percentage displayed in the 2nd field of the 2nd row
9-
if is_wsl; then
10-
local battery
11-
battery=$(find /sys/class/power_supply/*/capacity | tail -n1)
12-
cat "$battery"
13-
elif command_exists "pmset"; then
14-
pmset -g batt | grep -o "[0-9]\{1,3\}%"
15-
elif command_exists "acpi"; then
16-
acpi -b | grep -m 1 -Eo "[0-9]+%"
17-
elif command_exists "upower"; then
8+
# percentage displayed in the 2nd field of the 2nd row
9+
if is_wsl; then
10+
local battery=$(find /sys/class/power_supply/*/capacity | tail -n1)
11+
if [ -n "$battery" ]; then
12+
echo $(cat "$battery")%
13+
return
14+
fi
15+
elif command_exists "pmset"; then
16+
pmset -g batt | grep -o "[0-9]\{1,3\}%"
17+
elif command_exists "acpi"; then
18+
acpi -b | grep -m 1 -Eo "[0-9]+%"
19+
elif command_exists "upower"; then
1820
# use DisplayDevice if available otherwise battery
19-
local battery=$(upower -e | grep -E 'battery|DisplayDevice'| tail -n1)
20-
if [ -z "$battery" ]; then
21-
return
22-
fi
23-
local percentage=$(upower -i $battery | awk '/percentage:/ {print $2}')
24-
if [ "$percentage" ]; then
25-
echo ${percentage%.*%}
26-
return
27-
fi
28-
local energy
29-
local energy_full
30-
energy=$(upower -i $battery | awk -v nrg="$energy" '/energy:/ {print nrg+$2}')
31-
energy_full=$(upower -i $battery | awk -v nrgfull="$energy_full" '/energy-full:/ {print nrgfull+$2}')
32-
if [ -n "$energy" ] && [ -n "$energy_full" ]; then
33-
echo $energy $energy_full | awk '{printf("%d%%", ($1/$2)*100)}'
34-
fi
35-
elif command_exists "termux-battery-status"; then
36-
termux-battery-status | jq -r '.percentage' | awk '{printf("%d%%", $1)}'
37-
elif command_exists "apm"; then
38-
apm -l
39-
fi
21+
local battery=$(upower -e | grep -E 'battery|DisplayDevice'| tail -n1)
22+
if [ -z "$battery" ]; then
23+
return
24+
fi
25+
local percentage=$(upower -i $battery | awk '/percentage:/ {print $2}')
26+
if [ "$percentage" ]; then
27+
echo ${percentage%.*%}
28+
return
29+
fi
30+
local energy
31+
local energy_full
32+
energy=$(upower -i $battery | awk -v nrg="$energy" '/energy:/ {print nrg+$2}')
33+
energy_full=$(upower -i $battery | awk -v nrgfull="$energy_full" '/energy-full:/ {print nrgfull+$2}')
34+
if [ -n "$energy" ] && [ -n "$energy_full" ]; then
35+
echo $energy $energy_full | awk '{printf("%d%%", ($1/$2)*100)}'
36+
fi
37+
elif command_exists "termux-battery-status"; then
38+
termux-battery-status | jq -r '.percentage' | awk '{printf("%d%%", $1)}'
39+
elif command_exists "apm"; then
40+
apm -l
41+
fi
4042
}
4143

4244
main() {
43-
print_battery_percentage
45+
print_battery_percentage
4446
}
4547
main

0 commit comments

Comments
 (0)