-
Notifications
You must be signed in to change notification settings - Fork 2
/
battery.sh
61 lines (59 loc) · 2.19 KB
/
battery.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
function battery_charge() {
local battery_path="/sys/class/power_supply/BAT1/"
local curcap
local maxcap
BATT_PCT=99
case $(uname -s) in
"Darwin")
if ((pmset_on)) && command -v pmset &>/dev/null; then
if [ "$(pmset -g batt | grep -o 'AC Power')" ]; then
BATT_CONNECTED=1
else
BATT_CONNECTED=0
fi
BATT_PCT=$(pmset -g batt | grep -o '[0-9]*%' | tr -d %)
else
while read key value; do
case $key in
"MaxCapacity")
maxcap=$value
;;
"CurrentCapacity")
curcap=$value
;;
"ExternalConnected")
if [ $value == "No" ]; then
BATT_CONNECTED=0
else
BATT_CONNECTED=1
fi
;;
esac
if [[ -n "$maxcap" && -n $curcap ]]; then
BATT_PCT=$[100 * $curcap / $maxcap]
fi
done < <(ioreg -n AppleSmartBattery -r | grep -o '"[^"]*" = [^ ]*' | sed -e 's/= //g' -e 's/"//g' | sort)
fi
;;
"Linux")
case $(cat /etc/*-release) in
*"Arch Linux"*|*"Ubuntu"*|*"openSUSE"*)
BATT_PCT=$(cat $battery_path/capacity 2>/dev/null || echo "100")
battery_state=$(cat $battery_path/status 2>/dev/null || echo "Charging")
;;
*)
BATT_PCT=$(cat $battery_path/capacity 2>/dev/null || echo "100")
battery_state=$(cat $battery_path/status 2>/dev/null || echo "Charging")
;;
esac
if [ $battery_state == 'Discharging' ]; then
BATT_CONNECTED=0
else
BATT_CONNECTED=1
fi
BATTERY_STATE=$battery_state
;;
esac
}
battery_charge