-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathhelpers.sh
65 lines (60 loc) · 1.39 KB
/
helpers.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
62
63
64
65
get_tmux_option() {
local option="$1"
local default_value="$2"
local option_value="$(tmux show-option -gqv "$option")"
if [ -z "$option_value" ]; then
echo "$default_value"
else
echo "$option_value"
fi
}
is_osx() {
[ $(uname) == "Darwin" ]
}
is_chrome() {
chrome="/sys/class/chromeos/cros_ec"
if [ -d "$chrome" ]; then
return 0
else
return 1
fi
}
is_wsl() {
version=$(</proc/version)
if [[ "$version" == *"Microsoft"* || "$version" == *"microsoft"* ]]; then
return 0
else
return 1
fi
}
command_exists() {
local command
for command; do
type "$command" >/dev/null 2>&1 || return $?
done
}
battery_status() {
if command_exists "termux-battery-status" "jq"; then
termux-battery-status | jq -er '.status | ascii_downcase'
elif is_wsl; then
local battery
battery=$(find /sys/class/power_supply/*/status | tail -n1)
awk '{print tolower($0);}' "$battery"
elif command_exists "pmset"; then
pmset -g batt | awk -F '; *' 'NR==2 { print $2 }'
elif command_exists "acpi"; then
acpi -b | awk '{gsub(/,/, ""); print tolower($3); exit}'
elif command_exists "upower"; then
local battery
battery=$(upower -e | grep -E 'battery|DisplayDevice'| tail -n1)
upower -i $battery | awk '/state/ {print $2}'
elif command_exists "apm"; then
local battery
battery=$(apm -a)
if [ $battery -eq 0 ]; then
echo "discharging"
elif [ $battery -eq 1 ]; then
echo "charging"
fi
fi
}