-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbattery-status
More file actions
executable file
·94 lines (82 loc) · 2.26 KB
/
battery-status
File metadata and controls
executable file
·94 lines (82 loc) · 2.26 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/sh
# shellcheck disable=SC2046
NOTIFICATION_MESSAGE='Battery low'
notify() {
# TODO this relies on a script available in Arch Linux' aur. Maybe it shouldn't?
notify-send.sh -h string:x-dunst-stack-tag:battery-status -p "$@"
}
clear_notifications() {
# TODO other notification daemons also ought to work
pkill dunst
}
darwin_plugged() {
if [ "$(pmset -g batt | head -1)" = "Now drawing from 'AC Power'" ]
then echo yes
else echo no
fi
}
linux_plugged() {
if upower -i /org/freedesktop/UPower/devices/line_power_ADP1 | grep 'online:.*yes' >/dev/null
then echo yes
else echo no
fi
}
linux_charge() {
if command -v upower >/dev/null && upower
then
upower -i /org/freedesktop/UPower/devices/battery_BAT0 \
| awk '/percentage/{sub(/%/, "", $2); printf "%i", $2}'
else
echo ""
fi
}
output() {
printf '#[fg=%s,bg=%s]%s' "$bg_color" "$prev_color" "$sep" # left separator
test -n "$charge" || exit
printf '#[fg=%s,bg=%s]%s%%' "$fg_color" "$bg_color" "$charge" # percentage
printf '#[fg=%s,bg=%s]%s' "$next_color" "$bg_color" "$sep" # right separator
}
prev_color="$1"
next_color="$2"
sep="$3"
fg_color=black
bg_color="$next_color"
case $(uname -a | awk '{print $1}') in
Darwin)
plugged=$(darwin_plugged)
charge=$(pmset -g batt \
| awk '/-InternalBattery-/ {sub(/%;/, "", $3); print $3}')
;;
Linux)
plugged=$(linux_plugged)
charge=$(linux_charge)
;;
esac
if [ "$plugged" = 'yes' ]
then
printf "#[fg=%s]%s" "${next_color}" "${sep}"
clear_notifications
exit
fi
case "$charge" in
"" ) ;;
[0-9] )
bg_color=colour196
# This notification never goes away in xfce
notify "$NOTIFICATION_MESSAGE" "$charge%" \
--icon='battery-caution' \
--urgency critical \
--expire-time=60
;;
1[0-9]) bg_color=colour202 ;;
2[0-9]) bg_color=colour214 ;;
3[0-9]) bg_color=colour220 ;;
4[0-9]) bg_color=colour45 ;;
5[0-9]) bg_color=colour63 ;;
6[0-9]) bg_color=colour20; fg_color=white ;;
7[0-9]) bg_color=colour25; fg_color=white ;;
8[0-9]) bg_color=colour35 ;;
9[0-9]) bg_color=colour85 ;;
100) bg_color=$next_color;;
esac
output