~rycwo/lemons

ref: 0.6.0 lemons/lemons -rwxr-xr-x 9.2 KiB
7bdfbddcRyan Chan Bump minor version 0.5.0 -> 0.6.0 10 months ago
                                                                                
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env bash
# Tasty bar lemons.
#
# This script is adapted from electro7's dotfiles and the bspwm example:
# - https://github.com/electro7/dotfiles/tree/master/.i3/lemonbar
# - https://github.com/baskerville/bspwm/tree/master/examples/panel
#
# TODO:
# - Add mouse button/click commands.
# - Expose color configuration.

# Sane and safe way to begin a Bash script:
# https://github.com/anordal/shellharden/blob/master/how_to_do_things_safely_in_bash.md.
set -o errexit
set -o nounset
set -o pipefail
shopt -s nullglob globstar

###############################################################################
# Constants/defaults.
###############################################################################
readonly NAME="${0##*/}"
readonly VERSION="0.6.0"

# This is helpful for padding 3-digit values.
# We can pad values by subtracting the length of the
# value string from $PADDING, e.g. "${PADDING:${#my_value}}".
readonly PADDING="   "

# Colors.
readonly BLACK="#4e4e4e"
readonly GRAY="#999999"
readonly WHITE="#dadada"

# Glyphs.
readonly LARGE_CROSS="\\uf00d"
readonly SMALL_CROSS="\\u00d7"
readonly LARGE_DOT="\\u2022"
readonly SMALL_DOT="\\u00b7"
readonly SEPARATOR="   ""$SMALL_DOT""   "

readonly BATTERY_FULL="\\uf240"
readonly BATTERY_THREEQUAT="\\uf241"
readonly BATTERY_HALF="\\uf242"
readonly BATTERY_QUAT="\\uf243"
readonly BATTERY_NONE="\\uf244"
readonly BATTERY_CHARGING="\\uf0e7"

readonly VOLUME_HIGH="\\uf028"
readonly VOLUME_LOW="\\uf027"
readonly VOLUME_NONE="\\uf026"

readonly NETWORK_WIFI="\\uf1eb"

# Update frequency for non-event driven items.
readonly DATETIME_UPDATE_FREQ=1
readonly BATTERY_UPDATE_FREQ=10
readonly NETWORK_UPDATE_FREQ=10

# Default pipe.
: "${LEMONBAR_PIPE:=/tmp/lemonbar_pipe}"

###############################################################################
# Helpers.
###############################################################################
exit_() {
	# Clean-up named pipe.
	rm -f "$LEMONBAR_PIPE"

	# Before signaling all child processes, we have to reset the trap
	# for SIGTERM so that we don't enter an exit loop.
	trap - TERM

	# Signal all processes in the **current** process group.
	# This is necessary to correctly clean-up the processes, including
	# this one.
	kill 0
}

###############################################################################
# Lemons.
###############################################################################
datetime() {
	date "+%a %d""-""%m""-""%y""$SEPARATOR""%H:%M:%S"
}

battery() {
	battery_info="$(acpi --battery)"

	[[ "$battery_info" == *Discharging* ]] && charging=0 || charging=1

	battery_=""
	if [[ "$battery_info" =~ [[:digit:]]+% ]]; then
		battery_="${BASH_REMATCH[0]//%}"
	else
		echo "Failed to retrieve battery information" >&2
		return 1
	fi

	if [[ "$charging" -eq "1" ]]; then
		echo "$BATTERY_CHARGING"" ""$battery_"
	elif [[ "$battery_" -le "5" ]]; then
		echo "$BATTERY_NONE"" ""$battery_"
	elif [[ "$battery_" -le "25" ]]; then
		echo "$BATTERY_QUAT"" ""$battery_"
	elif [[ "$battery_" -le "50" ]]; then
		echo "$BATTERY_HALF"" ""$battery_"
	elif [[ "$battery_" -le "75" ]]; then
		echo "$BATTERY_THREEQUAT"" ""$battery_"
	else
		echo "$BATTERY_FULL""  ""$battery_"
	fi
}

volume() {
	volume_info="$(amixer get "Master")"

	[[ "$volume_info" == *off* ]] && muted=1 || muted=0

	# We use the regex matching operator here to capture the first volume
	# before then stripping the "[]" brackets from it on echo.
	volume_=""
	if [[ "$volume_info" =~ \[[[:digit:]]+%\] ]]; then
		volume_="${BASH_REMATCH[0]//[\[\]%]}"
	else
		echo "Failed to retrieve volume information" >&2
		return 1
	fi

	if [[ "$muted" -eq "1" ]]; then
		echo "%{F""$GRAY""}""$VOLUME_NONE""  ""$LARGE_CROSS""%{F-}"
	elif [[ "$volume_" -le "0" ]]; then
		echo "$VOLUME_NONE""  ""$volume_"
	elif [[ "$volume_" -le "50" ]]; then
		echo "$VOLUME_LOW""  ""$volume_"
	else
		echo "$VOLUME_HIGH""  ""$volume_"
	fi
}

network() {
	# At the moment only the wifi interface is supported. We find the first
	# one that iwconfig accepts and use that.
	network_id=""
	while read -r line; do
		if [[ "$line" =~ ^[[:digit:]]:\ ([[:alnum:]]+): ]]; then
			interface="${BASH_REMATCH[1]}"

			# Check the interface is up.
			[[ "$(ip link show "$interface")" =~ \<.+?,UP.+?\> ]] || continue

			# Check the interface is wireless, we assume that if the
			# regex fails, it isn't wireless.
			if [[ "$(iw dev "$interface" link 2>&1)" =~ SSID:\ (.{3}) ]]; then
				network_id="${BASH_REMATCH[1]//"\\x00"}"  # Trim null bytes.
				break
			fi
			fi
			# We use process substitution here to save using a pipe. This allows us to
			# set the network_id variable correctly. Found here:
			# https://stackoverflow.com/a/124349
		done < <(ip -oneline link)

	# As suggested in: http://blog.z3bra.org/2014/04/meeting-at-the-bar.html,
	# we use Google's DNS 8.8.8.8, and use an IP to save DNS resolution.
	# This is only done if we are connected to a network.
	ping -c 1 8.8.8.8 > /dev/null 2>&1 && active=1 || active=0

	if [[ "$active" -eq "1" ]] && [[ -n "$network_id" ]]; then
		echo "$NETWORK_WIFI""  ""$network_id"
	elif [[ "$active" -eq "1" ]] && [[ -z "$network_id" ]]; then
		echo "$NETWORK_WIFI""  ""$LARGE_CROSS"
	else
		echo "%{F""$GRAY""}""$NETWORK_WIFI""  ""$LARGE_CROSS""%{F-}"
	fi
}

workspace() {
	monitors=() # TODO: Currently unused.
		desktops=()

	# Parse items into a Bash array using a here-string to pass
	# the input as stdin to read.
	IFS=':' read -ra items <<< "$1"

	for item in "${items[@]}"; do
		# Capital items are focused.
		case "$item" in
			# Monitors.
			[mM]*)
			[[ "$item" =~ ^[mM](.+)$ ]] && monitors+=("${BASH_REMATCH[1]}")
			;;
			# Desktops.
			[fFoOuU]*)
			# Occupied.
			if [[ "$item" =~ ^o(.+)$ ]]; then
				desktops+=(" ""$LARGE_DOT"" ")
				# Occupied focused.
			elif [[ "$item" =~ ^O(.+)$ ]]; then
				desktops+=("%{T3} ""$SMALL_CROSS"" %{T-}")
				# Free.
			elif [[ "$item" =~ ^f(.+)$ ]]; then
				desktops+=("%{F""$GRAY""} ""$LARGE_DOT"" %{F-}")
				# Free focused.
			elif [[ "$item" =~ ^F(.+)$ ]]; then
				desktops+=("%{T3}%{F""$GRAY""} ""$SMALL_CROSS"" %{F-}%{T-}")
				# Urgent.
			elif [[ "$item" =~ ^u(.+)$ ]]; then
				desktops+=("%{+u} ""$LARGE_DOT"" %{-u}")
				# Urgent focused.
			elif [[ "$item" =~ ^U(.+)$ ]]; then
				desktops+=("%{T3}%{+u} ""$SMALL_CROSS"" %{-u}%{T-}")
			fi
			;;
			# Layout/state/flags.
			[LTG]*)
			# TODO.
			;;
	esac
done

echo "${desktops[@]}"
}

###############################################################################
# Handlers.
###############################################################################
volume_handler() {
	while read -r line; do
		if [[ "$line" =~ ^Event\ \'change\'\ on\ sink\ \#[[:digit:]]+$ ]]; then
			echo "VOLM""$(volume)"
		fi
	done
}

bspwm_handler() {
	while read -r line; do
		echo "BSPC""$(workspace "${line##W}")"
	done
}

handler() {
	datetime_=""
	battery_=""
	network_=""
	volume_=""
	workspace_=""

	# Read lines from standard input and format for lemonbar.
	while read -r line; do
		# Note the asterisks (*) are necessary in the case statements as we are
		# matching against the first few characters of the line.
		case "$line" in
			TIME*)
				datetime_="${line#????}"
				;;
			BATT*)
				battery_="${line#????}"
				;;
			NETW*)
				network_="${line#????}"
				;;
			VOLM*)
				volume_="${line#????}"
				;;
			BSPC*)
				workspace_="${line#????}"
				;;
		esac

		# Output for lemonbar.
		echo -e \
			"%{l}""  ""$workspace_"\
			"%{c}""$battery_"\
			"%{r}""$network_""$SEPARATOR""$volume_""$SEPARATOR""$datetime_""  "
		done
	}

###############################################################################
# Main.
###############################################################################
# Set trap for numerous signals.
trap exit_ EXIT INT TERM QUIT

if [[ ! -p "$LEMONBAR_PIPE" ]]; then
	mkfifo "$LEMONBAR_PIPE"
fi

# Subscribe to a number of events/callbacks.

# bspwm events.
bspc subscribe report | bspwm_handler > "$LEMONBAR_PIPE" &

# PulseAudio volume-change events. We need to query the volume
# once to initialize the volume.
echo "VOLM""$(volume)" > "$LEMONBAR_PIPE" &
pactl subscribe | volume_handler > "$LEMONBAR_PIPE" &

# Here we define a number of counters for stats we have to
# continuously poll.
datetime_secs_since_update="$DATETIME_UPDATE_FREQ"
battery_secs_since_update="$BATTERY_UPDATE_FREQ"
network_secs_since_update="$NETWORK_UPDATE_FREQ"

# Manually update a number of components.
# Note the "&" after the "done", this forks the loop into
# the background.
while true; do
	if [[ "$((datetime_secs_since_update++))" -ge "$((DATETIME_UPDATE_FREQ - 1))" ]]; then
		echo "TIME""$(datetime)" > "$LEMONBAR_PIPE"
		datetime_secs_since_update=0
	fi

	if [[ "$((battery_secs_since_update++))" -ge "$((BATTERY_UPDATE_FREQ - 1))" ]]; then
		echo "BATT""$(battery)" > "$LEMONBAR_PIPE"
		battery_secs_since_update=0
	fi

	if [[ "$((network_secs_since_update++))" -ge "$((NETWORK_UPDATE_FREQ - 1))" ]]; then
		echo "NETW""$(network)" > "$LEMONBAR_PIPE"
		network_secs_since_update=0
	fi

	sleep 0.5
done &

# Here we read from the named pipe to handler.
# The output of handler is then piped to lemonbar.
# Additional arguments for lemonbar can be specified as arguments to
# lemons, which passes them on.
handler < "$LEMONBAR_PIPE" | lemonbar -bp -B "$WHITE" -F "$BLACK" -f "FontAwesome" "$@" &

# Wait for lemonbar completion.
wait