|
1 | 1 | #!/usr/bin/env sh
|
2 | 2 |
|
3 |
| -charging_icon="⚡" # U+26A1 - Thunderbolt |
| 3 | +charging_icon="⚡" # U+26A1 - Thunderbolt |
4 | 4 | discharging_icon="🔋" # U+1F50B - Battery
|
5 | 5 |
|
6 | 6 | print_help() {
|
7 |
| - echo "usage: battstat [options] format" |
8 |
| - echo "" |
9 |
| - echo "options:" |
10 |
| - echo " -h, --help display help information" |
11 |
| - echo " -c, --charging-icon string to display in icon's place when battery is charging" |
12 |
| - echo " -d, --discharging-icon string to display in icon's place when battery is discharging" |
13 |
| - echo " --percent-when-charged only display percent when charged" |
14 |
| - echo "" |
15 |
| - echo "format:" |
16 |
| - echo " {i} display icon" |
17 |
| - echo " {t} display time remaining" |
18 |
| - echo " {p} display percent" |
19 |
| - echo "" |
20 |
| - echo " Note: There must be a space between each format token." |
| 7 | + echo "usage: battstat [options] format" |
| 8 | + echo "" |
| 9 | + echo "options:" |
| 10 | + echo " -h, --help display help information" |
| 11 | + echo " -c, --charging-icon string to display in icon's place when battery is charging" |
| 12 | + echo " -d, --discharging-icon string to display in icon's place when battery is discharging" |
| 13 | + echo " --percent-when-charged only display percent when charged" |
| 14 | + echo "" |
| 15 | + echo "format:" |
| 16 | + echo " {i} display icon" |
| 17 | + echo " {t} display time remaining" |
| 18 | + echo " {p} display percent" |
| 19 | + echo "" |
| 20 | + echo " Note: There must be a space between each format token." |
21 | 21 | }
|
22 | 22 |
|
23 | 23 | exit_no_battery() {
|
24 |
| - echo "battstat: no battery found" |
25 |
| - exit 1 |
| 24 | + echo "battstat: no battery found" |
| 25 | + exit 1 |
26 | 26 | }
|
27 | 27 |
|
28 | 28 | get_darwin_details() {
|
29 |
| - battery_details=$(pmset -g batt) |
30 |
| - |
31 |
| - # Exit if no battery exists. |
32 |
| - if ! echo "$battery_details" | grep -q InternalBattery; then |
33 |
| - exit_no_battery |
34 |
| - fi |
35 |
| - |
36 |
| - charged=$(echo "$battery_details" | grep -w 'charged') |
37 |
| - charging=$(echo "$battery_details" | grep -w 'AC Power') |
38 |
| - discharging=$(echo "$battery_details" | grep -w 'Battery Power') |
39 |
| - time=$(echo "$battery_details" | grep -Eo '([0-9][0-9]|[0-9]):[0-5][0-9]') |
40 |
| - percent=$(echo "$battery_details" | grep -o "[0-9]*"%) |
| 29 | + battery_details=$(pmset -g batt) |
| 30 | + |
| 31 | + # Exit if no battery exists. |
| 32 | + if ! echo "$battery_details" | grep -q InternalBattery; then |
| 33 | + exit_no_battery |
| 34 | + fi |
| 35 | + |
| 36 | + charged=$(echo "$battery_details" | grep -w 'charged') |
| 37 | + charging=$(echo "$battery_details" | grep -w 'AC Power') |
| 38 | + discharging=$(echo "$battery_details" | grep -w 'Battery Power') |
| 39 | + time=$(echo "$battery_details" | grep -Eo '([0-9][0-9]|[0-9]):[0-5][0-9]') |
| 40 | + percent=$(echo "$battery_details" | grep -o "[0-9]*"%) |
41 | 41 | }
|
42 | 42 |
|
43 | 43 | get_linux_details() {
|
44 |
| - battery_details=$(LC_ALL=en_US.UTF-8 upower -i $(upower -e | grep 'BAT')) |
45 |
| - |
46 |
| - # Exit if no batery exists. |
47 |
| - if [ -z "$battery_details" ]; then |
48 |
| - exit_no_battery |
49 |
| - fi |
50 |
| - |
51 |
| - charged=$(echo "$battery_details" | grep 'state' | grep -w 'fully-charged') |
52 |
| - charging=$(echo "$battery_details" | grep 'state' | grep -w 'charging') |
53 |
| - discharging=$(echo "$battery_details" | grep 'state' | grep -w 'discharging') |
54 |
| - percent=$(echo "$battery_details"| grep 'percentage' | awk '{print $2}') |
55 |
| - |
56 |
| - case $(echo "$battery_details" | grep 'time' | awk '{print $5}') in |
| 44 | + battery_details=$(LC_ALL=en_US.UTF-8 upower -i $(upower -e | grep 'BAT')) |
| 45 | + |
| 46 | + # Exit if no batery exists. |
| 47 | + if [ -z "$battery_details" ]; then |
| 48 | + exit_no_battery |
| 49 | + fi |
| 50 | + |
| 51 | + charged=$(echo "$battery_details" | grep 'state' | grep -w 'fully-charged') |
| 52 | + charging=$(echo "$battery_details" | grep 'state' | grep -w 'charging') |
| 53 | + discharging=$(echo "$battery_details" | grep 'state' | grep -w 'discharging') |
| 54 | + percent=$(echo "$battery_details" | grep 'percentage' | awk '{print $2}') |
| 55 | + |
| 56 | + case $(echo "$battery_details" | grep 'time' | awk '{print $5}') in |
57 | 57 | "hours")
|
58 |
| - hours=$(echo "$battery_details" | grep 'time' | awk '{print $4}' | cut -d . -f1) |
59 |
| - minutes=$(echo "$battery_details" | grep 'time' | awk '{print $4}' | cut -d . -f2) |
60 |
| - minutes=$(echo .$minutes \* 60 | bc -l | cut -d. -f1) |
61 |
| - ;; |
| 58 | + hours=$(echo "$battery_details" | grep 'time' | awk '{print $4}' | cut -d . -f1) |
| 59 | + minutes=$(echo "$battery_details" | grep 'time' | awk '{print $4}' | cut -d . -f2) |
| 60 | + minutes=$(echo .$minutes \* 60 | bc -l | cut -d. -f1) |
| 61 | + ;; |
62 | 62 | "minutes")
|
63 |
| - minutes=$(echo "$battery_details" | grep 'time' | awk '{print $4}' | cut -d . -f1) |
64 |
| - ;; |
65 |
| - esac |
66 |
| - |
67 |
| - # Diplay 0 in the hours spot when only minutes remain. |
68 |
| - if [ -z "$hours" ]; then |
69 |
| - hours="0" |
70 |
| - fi |
71 |
| - |
72 |
| - # Prefix 0 when minutes drop below 10. |
73 |
| - if [ ${#minutes} -eq '1' ]; then |
74 |
| - minutes="0$minutes" |
75 |
| - fi |
76 |
| - |
77 |
| - time=$hours:$minutes |
| 63 | + minutes=$(echo "$battery_details" | grep 'time' | awk '{print $4}' | cut -d . -f1) |
| 64 | + ;; |
| 65 | + esac |
| 66 | + |
| 67 | + # Diplay 0 in the hours spot when only minutes remain. |
| 68 | + if [ -z "$hours" ]; then |
| 69 | + hours="0" |
| 70 | + fi |
| 71 | + |
| 72 | + # Prefix 0 when minutes drop below 10. |
| 73 | + if [ ${#minutes} -eq '1' ]; then |
| 74 | + minutes="0$minutes" |
| 75 | + fi |
| 76 | + |
| 77 | + time=$hours:$minutes |
78 | 78 | }
|
79 | 79 |
|
80 | 80 | get_openbsd_details() {
|
81 |
| - battery_details=$(apm) |
82 |
| - |
83 |
| - # Exit if no battery exists. |
84 |
| - if [ -z "$battery_details" ]; then |
85 |
| - exit_no_battery |
86 |
| - fi |
87 |
| - |
88 |
| - charging=$(echo $battery_details | grep -w 'state: connected') |
89 |
| - discharging=$(echo $battery_details | grep -w 'state: not connected') |
90 |
| - percent=$(echo $battery_details | grep -o '[0-9]*%') |
91 |
| - full_minutes=$(echo $battery_details | grep -o ' [0-9]* ') |
92 |
| - |
93 |
| - # Battery is considered charged when AC is connected and 100% |
94 |
| - if [ ! -z "$charging" ] && [ $percent = "100%" ]; then |
95 |
| - charged="charged" |
96 |
| - fi |
97 |
| - |
98 |
| - # Only compute time when available |
99 |
| - if [ ! -z "$full_minutes" ]; then |
100 |
| - hours=$(($full_minutes/60)) |
101 |
| - minutes=$(($full_minutes%60)) |
102 |
| - |
103 |
| - # Prefix 0 when minutes drop below 10. |
104 |
| - if [ ${#minutes} -eq '1' ]; then |
105 |
| - minutes="0$minutes" |
106 |
| - fi |
107 |
| - |
108 |
| - time=$hours:$minutes |
109 |
| - fi |
| 81 | + battery_details=$(apm) |
| 82 | + |
| 83 | + # Exit if no battery exists. |
| 84 | + if [ -z "$battery_details" ]; then |
| 85 | + exit_no_battery |
| 86 | + fi |
| 87 | + |
| 88 | + charging=$(echo $battery_details | grep -w 'state: connected') |
| 89 | + discharging=$(echo $battery_details | grep -w 'state: not connected') |
| 90 | + percent=$(echo $battery_details | grep -o '[0-9]*%') |
| 91 | + full_minutes=$(echo $battery_details | grep -o ' [0-9]* ') |
| 92 | + |
| 93 | + # Battery is considered charged when AC is connected and 100% |
| 94 | + if [ ! -z "$charging" ] && [ $percent = "100%" ]; then |
| 95 | + charged="charged" |
| 96 | + fi |
| 97 | + |
| 98 | + # Only compute time when available |
| 99 | + if [ ! -z "$full_minutes" ]; then |
| 100 | + hours=$(($full_minutes / 60)) |
| 101 | + minutes=$(($full_minutes % 60)) |
| 102 | + |
| 103 | + # Prefix 0 when minutes drop below 10. |
| 104 | + if [ ${#minutes} -eq '1' ]; then |
| 105 | + minutes="0$minutes" |
| 106 | + fi |
| 107 | + |
| 108 | + time=$hours:$minutes |
| 109 | + fi |
110 | 110 | }
|
111 | 111 |
|
112 | 112 | hide_percent_until_charged() {
|
113 |
| - if [ -z "$charged" ]; then |
114 |
| - percent="" |
115 |
| - fi |
116 |
| -} |
| 113 | + if [ -z "$charged" ]; then |
| 114 | + percent="" |
| 115 | + fi |
| 116 | +} |
117 | 117 |
|
118 | 118 | print_icon() {
|
119 |
| - if [ ! -z "$charging" ] || [ ! -z "$charged" ]; then |
120 |
| - icon=$charging_icon |
121 |
| - elif [ ! -z "$discharging" ]; then |
122 |
| - icon=$discharging_icon |
123 |
| - fi |
| 119 | + if [ ! -z "$charging" ] || [ ! -z "$charged" ]; then |
| 120 | + icon=$charging_icon |
| 121 | + elif [ ! -z "$discharging" ]; then |
| 122 | + icon=$discharging_icon |
| 123 | + fi |
124 | 124 |
|
125 |
| - printf " %s " $icon |
| 125 | + printf " %s " $icon |
126 | 126 | }
|
127 | 127 |
|
128 | 128 | print_time() {
|
129 |
| - # Display "calc..." when calculating time remaining. |
130 |
| - if [ -z "$time" ] || [ $time = "0:00" ]; then |
131 |
| - time="calc..." |
132 |
| - fi |
133 |
| - |
134 |
| - # Hide time when fully charged. |
135 |
| - if [ ! -z "$charged" ]; then |
136 |
| - time="" |
137 |
| - fi |
138 |
| - |
139 |
| - if [ ! -z "$time" ]; then |
140 |
| - printf " %s " $time |
141 |
| - fi |
| 129 | + # Display "calc..." when calculating time remaining. |
| 130 | + if [ -z "$time" ] || [ $time = "0:00" ]; then |
| 131 | + time="calc..." |
| 132 | + fi |
| 133 | + |
| 134 | + # Hide time when fully charged. |
| 135 | + if [ ! -z "$charged" ]; then |
| 136 | + time="" |
| 137 | + fi |
| 138 | + |
| 139 | + if [ ! -z "$time" ]; then |
| 140 | + printf " %s " $time |
| 141 | + fi |
142 | 142 |
|
143 | 143 | }
|
144 | 144 |
|
145 | 145 | print_percent() {
|
146 |
| - if [ ! -z "$percent" ]; then |
147 |
| - printf " %s " $percent |
148 |
| - fi |
| 146 | + if [ ! -z "$percent" ]; then |
| 147 | + printf " %s " $percent |
| 148 | + fi |
149 | 149 | }
|
150 | 150 |
|
151 | 151 | if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
|
152 |
| - print_help |
153 |
| - exit 0 |
| 152 | + print_help |
| 153 | + exit 0 |
154 | 154 | fi
|
155 | 155 |
|
156 | 156 | case $(uname) in
|
157 |
| - "Darwin") |
| 157 | +"Darwin") |
158 | 158 | get_darwin_details
|
159 | 159 | ;;
|
160 |
| - "Linux") |
| 160 | +"Linux") |
161 | 161 | get_linux_details
|
162 | 162 | ;;
|
163 |
| - "OpenBSD") |
| 163 | +"OpenBSD") |
164 | 164 | get_openbsd_details
|
165 | 165 | ;;
|
166 |
| - *) |
| 166 | +*) |
167 | 167 | echo "battstat: operating system not supported"
|
168 | 168 | exit 1
|
169 | 169 | ;;
|
170 | 170 | esac
|
171 | 171 |
|
172 | 172 | if [ $# -eq 0 ]; then
|
173 |
| - print_time |
174 |
| - print_percent |
175 |
| - print_icon |
| 173 | + print_time |
| 174 | + print_percent |
| 175 | + print_icon |
176 | 176 | fi
|
177 | 177 |
|
178 | 178 | while test $# -gt 0; do
|
179 |
| - case "$1" in |
| 179 | + case "$1" in |
180 | 180 | --percent-when-charged)
|
181 |
| - hide_percent_until_charged |
182 |
| - shift |
183 |
| - ;; |
184 |
| - -c|--charging-icon) |
185 |
| - charging_icon="$2" |
186 |
| - shift |
187 |
| - shift |
188 |
| - ;; |
189 |
| - -d|--discharging-icon) |
190 |
| - discharging_icon="$2" |
191 |
| - shift |
192 |
| - shift |
193 |
| - ;; |
| 181 | + hide_percent_until_charged |
| 182 | + shift |
| 183 | + ;; |
| 184 | + -c | --charging-icon) |
| 185 | + charging_icon="$2" |
| 186 | + shift |
| 187 | + shift |
| 188 | + ;; |
| 189 | + -d | --discharging-icon) |
| 190 | + discharging_icon="$2" |
| 191 | + shift |
| 192 | + shift |
| 193 | + ;; |
194 | 194 | {i})
|
195 |
| - print_icon |
196 |
| - shift |
197 |
| - ;; |
| 195 | + print_icon |
| 196 | + shift |
| 197 | + ;; |
198 | 198 | {t})
|
199 |
| - print_time |
200 |
| - shift |
201 |
| - ;; |
| 199 | + print_time |
| 200 | + shift |
| 201 | + ;; |
202 | 202 | {p})
|
203 |
| - print_percent |
204 |
| - shift |
205 |
| - ;; |
| 203 | + print_percent |
| 204 | + shift |
| 205 | + ;; |
206 | 206 | *)
|
207 |
| - print_help |
208 |
| - break |
209 |
| - ;; |
210 |
| - esac |
| 207 | + print_help |
| 208 | + break |
| 209 | + ;; |
| 210 | + esac |
211 | 211 | done
|
212 | 212 |
|
213 | 213 | printf "\n"
|
0 commit comments