statusbar.sh - statusbar - Shell script to set/print a status bar
(HTM) hg clone https://bitbucket.org/iamleot/statusbar
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
statusbar.sh
---
1 #!/bin/sh
2
3 #
4 # Copyright (c) 2018 Leonardo Taccari
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # 1. Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
21 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
28 #
29
30
31 #
32 # Customizable variables used by the various functions.
33 #
34 acad_sensor="${STATUSBAR_ACAD_SENSOR}"
35 battery_sensor="${STATUSBAR_BATTERY_SENSOR}"
36 battery_charging_sensor="${STATUSBAR_BATTERY_CHARGING_SENSOR}"
37 clock_format="${STATUSBAR_CLOCK_FORMAT}"
38 date_format="${STATUSBAR_DATE_FORMAT}"
39 mixer_mute="${STATUSBAR_MIXER_MUTE}"
40 mixer_output="${STATUSBAR_MIXER_OUTPUT}"
41 network_interfaces="${STATUSBAR_NETWORK_INTERFACES}"
42 temperature_sensor="${STATUSBAR_TEMPERATURE_SENSOR}"
43 unread_emails="${STATUSBAR_UNREAD_EMAILS}"
44 unread_news="${STATUSBAR_UNREAD_NEWS}"
45
46 #
47 # Customizable status command dynamically evaluated every $sleep_time.
48 #
49 status_cmd='status="'
50 if [ -n "${network_interfaces}" ]; then
51 status_cmd=$status_cmd' `network_status` $separator'
52 fi
53 if [ -n "${mixer_mute}" ] && [ -n "${mixer_output}" ]; then
54 status_cmd=$status_cmd' `volume` $separator'
55 fi
56 if [ -n "${temperature_sensor}" ]; then
57 status_cmd=$status_cmd' `cpu_temperature` $separator'
58 fi
59 if [ -n "${acad_sensor}" ] && [ -n "${battery_sensor}" ] && [ -n "${battery_charging_sensor}" ]; then
60 status_cmd=$status_cmd' `battery_status` $separator'
61 fi
62 if [ -n "${unread_emails}" ]; then
63 status_cmd=$status_cmd' `unread_mail` $separator'
64 fi
65 if [ -n "${unread_news}" ]; then
66 status_cmd=$status_cmd' `unread_feeds` $separator'
67 fi
68 if [ -n "${date_format}" ]; then
69 status_cmd=$status_cmd' `what_day_is_it` $separator'
70 fi
71 if [ -n "${clock_format}" ]; then
72 status_cmd=$status_cmd' `what_time_is_it`'
73 fi
74 status_cmd=$status_cmd'"'
75
76 # Sleep time (in seconds) used to refresh the status bar
77 sleep_time=60
78
79 #
80 # Symbols (mostly Unicode characters) used by the various functions.
81 #
82 acad_symbol="🔌"
83 charging_symbol="⚡"
84 battery_symbol="🔋"
85 clock_symbol="⏰"
86 date_symbol="📅"
87 mail_symbol="🖂"
88 network_symbol="🖧"
89 feeds_symbol="📰"
90 temperature_symbol="🌡"
91 separator="|"
92
93 mute_volume_symbol="🔇"
94 low_volume_symbol="🔈"
95 medium_volume_symbol=" 🔉"
96 high_volume_symbol="🔊"
97
98
99 #
100 # Print current date via date(1) honoring $date_format.
101 #
102 what_day_is_it()
103 {
104 printf "%s " $date_symbol
105 date "$date_format"
106 }
107
108 #
109 # Print current time via date(1) honoring $clock_format.
110 #
111 what_time_is_it()
112 {
113 printf "%s " $clock_symbol
114 date "$clock_format"
115 }
116
117 #
118 # Print the number of unread emails.
119 # FIXME: nmh logic is actually hardcoded inside the function.
120 #
121 unread_mail()
122 {
123 printf "%s " $mail_symbol
124 new | sed -nE '/total/ s/^ total +([0-9]+)\.$/\1/p'
125 }
126
127 #
128 # Print the number of unread feeds.
129 # FIXME: nmh logic is actually hardcoded inside the function.
130 #
131 unread_feeds()
132 {
133 printf "%s " $feeds_symbol
134 MH=${HOME}/.mh_rss_profile new | sed -nE '/total/ s/^ total +([0-9]+)\.$/\1/p'
135 }
136
137 #
138 # Print the percentage of battery left using envstat(8) and $battery_sensor.
139 # If an AC adapter is present set the AC adapter symbol accordingly.
140 #
141 battery_status()
142 {
143 acad_connected=`envstat -s "$acad_sensor" | awk '/(connected|present):/ { print $NF }'`
144 battery_charging=`envstat -s "$battery_charging_sensor" | awk '/charging:/ { print $NF }'`
145
146 if [ "$battery_charging" = "TRUE" ]; then
147 printf "%s " $charging_symbol
148 elif [ "$acad_connected" = "TRUE" ]; then
149 printf "%s " $acad_symbol
150 else
151 printf "%s " $battery_symbol
152 fi
153 { envstat -s "$battery_sensor" || echo "charge: (N/A)" ; } |
154 awk '/battery percent:/ { printf("%d%%", int($3)) }
155 /charge:/ { printf("%s", ($NF == "(N/A)") ? "(N/A)" : int(substr($NF, 2, length($NF))) "%") }'
156 }
157
158 #
159 # Print all the network interfaces currently up defined in
160 # $network_interfaces list.
161 #
162 network_status()
163 {
164 connected=""
165
166 printf "%s " ${network_symbol}
167 for i in $network_interfaces; do
168 if [ -n "`ifconfig -u $i 2>/dev/null`" ]; then
169 if [ ! "$connected" ]; then
170 connected="yes"
171 fi
172 printf " ${i}"
173 fi
174 done
175
176 if [ ! "$connected" ]; then
177 printf " -"
178 fi
179 }
180
181 #
182 # Print the CPU temperature using envstat(8) and $temperature_sensor.
183 #
184 cpu_temperature()
185 {
186 printf "%s " $temperature_symbol
187 { envstat -s "$temperature_sensor" || echo "temperature: N/A" ; } | sed -nE '/temperature:/ s/^.*: *([0-9]+|N\/A) *.*$/\1/p'
188 }
189
190 #
191 # Print current volume level graphically via a symbol using mixerctl(1) and
192 # $mixer_output.
193 #
194 volume()
195 {
196 low_volume=0
197 medium_volume=80
198 high_volume=160
199
200 if [ "$mixer_mute" -a "`mixerctl -n $mixer_mute 2>/dev/null`" = "on" ]; then
201 echo "$mute_volume_symbol"
202 return
203 fi
204
205 volume=`mixerctl -n "$mixer_output" | awk -F ',' '{ print ($1 + $2) / 2 }'`
206
207 if [ $volume -le $low_volume ]; then
208 echo "$low_volume_symbol"
209 elif [ $volume -le $medium_volume ]; then
210 echo "$medium_volume_symbol"
211 else
212 echo "$high_volume_symbol"
213 fi
214 }
215
216 onetime=false
217 output=false
218
219 args=`getopt 1os: $*`
220 if [ $? -ne 0 ]; then
221 progname=`basename "$0"`
222 echo "usage: $progname [-1o] [-s seconds]"
223 exit 2
224 fi
225 set -- $args
226 while [ $# -gt 0 ]; do
227 case "$1" in
228 -1)
229 onetime=true
230 ;;
231 -o)
232 output=true
233 ;;
234 -s)
235 sleep_time=$2; shift
236 ;;
237 --)
238 shift; break
239 ;;
240 esac
241 shift
242 done
243
244 while true; do
245 eval $status_cmd
246 if $output; then
247 printf "\r"
248 printf "%s" "$status"
249 else
250 xsetroot -name "$status"
251 fi
252 if $onetime; then
253 exit 0
254 fi
255 sleep $sleep_time
256 done