Skip to content

Commit 924c328

Browse files
committed
shfmt
1 parent bdd9271 commit 924c328

File tree

1 file changed

+152
-152
lines changed

1 file changed

+152
-152
lines changed

battstat

Lines changed: 152 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -1,213 +1,213 @@
11
#!/usr/bin/env sh
22

3-
charging_icon="" # U+26A1 - Thunderbolt
3+
charging_icon="" # U+26A1 - Thunderbolt
44
discharging_icon="🔋" # U+1F50B - Battery
55

66
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."
2121
}
2222

2323
exit_no_battery() {
24-
echo "battstat: no battery found"
25-
exit 1
24+
echo "battstat: no battery found"
25+
exit 1
2626
}
2727

2828
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]*"%)
4141
}
4242

4343
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
5757
"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+
;;
6262
"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
7878
}
7979

8080
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
110110
}
111111

112112
hide_percent_until_charged() {
113-
if [ -z "$charged" ]; then
114-
percent=""
115-
fi
116-
}
113+
if [ -z "$charged" ]; then
114+
percent=""
115+
fi
116+
}
117117

118118
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
124124

125-
printf " %s " $icon
125+
printf " %s " $icon
126126
}
127127

128128
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
142142

143143
}
144144

145145
print_percent() {
146-
if [ ! -z "$percent" ]; then
147-
printf " %s " $percent
148-
fi
146+
if [ ! -z "$percent" ]; then
147+
printf " %s " $percent
148+
fi
149149
}
150150

151151
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
152-
print_help
153-
exit 0
152+
print_help
153+
exit 0
154154
fi
155155

156156
case $(uname) in
157-
"Darwin")
157+
"Darwin")
158158
get_darwin_details
159159
;;
160-
"Linux")
160+
"Linux")
161161
get_linux_details
162162
;;
163-
"OpenBSD")
163+
"OpenBSD")
164164
get_openbsd_details
165165
;;
166-
*)
166+
*)
167167
echo "battstat: operating system not supported"
168168
exit 1
169169
;;
170170
esac
171171

172172
if [ $# -eq 0 ]; then
173-
print_time
174-
print_percent
175-
print_icon
173+
print_time
174+
print_percent
175+
print_icon
176176
fi
177177

178178
while test $# -gt 0; do
179-
case "$1" in
179+
case "$1" in
180180
--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+
;;
194194
{i})
195-
print_icon
196-
shift
197-
;;
195+
print_icon
196+
shift
197+
;;
198198
{t})
199-
print_time
200-
shift
201-
;;
199+
print_time
200+
shift
201+
;;
202202
{p})
203-
print_percent
204-
shift
205-
;;
203+
print_percent
204+
shift
205+
;;
206206
*)
207-
print_help
208-
break
209-
;;
210-
esac
207+
print_help
208+
break
209+
;;
210+
esac
211211
done
212212

213213
printf "\n"

0 commit comments

Comments
 (0)