-
Notifications
You must be signed in to change notification settings - Fork 1
/
gradient-temp
executable file
·461 lines (379 loc) · 10 KB
/
gradient-temp
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#!/usr/bin/env bash
#
# gradient-temp
#
# A temperature indicator for i3blocks, colored according to heat
#
# Requires:
# awk (POSIX compatible)
# lm_sensors
#
# Optional:
# fonts-font-awesome
#
# Copyright (c) 2018 Beau Hastings. All rights reserved
# License: GNU General Public License v2
#
# Author: Beau Hastings <[email protected]>
# URL: https://github.com/hastinbe/i3blocks-gradient-temp
FONT=${FONT:-"FontAwesome"}
SENSOR=${SENSOR:-$BLOCK_INSTANCE}
GRADIENT=${GRADIENT:-true}
FAHRENHEIT=${FAHRENHEIT:-false}
COLOR_TEMP=${COLOR_TEMP:-"white"}
COLOR_GRADIENT_START=${COLOR_GRADIENT_START:-"#0000FF"}
COLOR_GRADIENT_END=${COLOR_GRADIENT_END:-"#FF0000"}
SYMBOL_C=${SYMBOL_C:-"℃"}
SYMBOL_F=${SYMBOL_F:-"℉"}
define_helpers() {
empty() {
[[ -z $1 ]]
}
not_empty() {
[[ -n $1 ]]
}
isset() {
[[ -v $1 ]]
}
error() {
echo "$COLOR_RED$*$COLOR_RESET"
}
has_color() {
[ "$(tput colors)" -ge 8 ] &>/dev/null && [ -t 1 ]
}
# Returns the minimum of two numbers
min() {
if (($1 < $2)); then
echo "$1"
else
echo "$2"
fi
}
# Returns the maximum of two numbers
max() {
if (($1 > $2)); then
echo "$1"
else
echo "$2"
fi
}
# Clamps a value between a minimum and maximum
clamp() {
min "$(max "$1" "$2")" "$3"
}
deg_ctof() {
echo "$((($1 * 9) / 5 + 32))"
}
}
temp() {
local temp
temp=$(subfeature_f 'temp1_input')
if $FAHRENHEIT; then temp=$(deg_ctof "$temp"); fi
echo "$temp"
}
temp_max() {
local temp
temp=$(subfeature_f 'temp1_max')
if $FAHRENHEIT; then temp=$(deg_ctof "$temp"); fi
echo "$temp"
}
temp_crit() {
local temp
temp=$(subfeature_f 'temp1_crit')
if $FAHRENHEIT; then temp=$(deg_ctof "$temp"); fi
echo "$temp"
}
subfeature_f() {
awk -W posix "/$1/ {printf \"%.1f\", \$2; exit}" <<< "$SENSORS"
}
temp_symbol() {
if $FAHRENHEIT; then
echo "$SYMBOL_F"
else
echo "$SYMBOL_C"
fi
}
# Interpolate between 2 RGB colors
#
# Arguments:
# $1 - Color to interpolate from, as a hex triplet prefixed with #
# $2 - Color to interpolate to, as a hex triplet prefixed with #
# $3 - Amount of steps needed to get from start to end color
interpolate_rgb() {
local steps=$3 colors=() color1=$1 color2=$2
local startR=$(( 16#${color1:1:2} ))
local startG=$(( 16#${color1:3:2} ))
local startB=$(( 16#${color1:5:2} ))
local endR=$(( 16#${color2:1:2} ))
local endG=$(( 16#${color2:3:2} ))
local endB=$(( 16#${color2:5:2} ))
local stepR=$(( (endR - startR) / steps ))
local stepG=$(( (endG - startG) / steps ))
local stepB=$(( (endB - startB) / steps ))
for i in $(seq 0 "$steps"); do
local -i R=$(( startR + (stepR * i) ))
local -i G=$(( startG + (stepG * i) ))
local -i B=$(( startB + (stepB * i) ))
colors+=( "$(printf "#%02x%02x%02x\n" $R $G $B)" )
done
colors+=( "$(printf "#%02x%02x%02x\n" "$endR" "$endG" "$endB")" )
echo "${colors[@]}"
}
# Get color for the given temperature
#
# Arguments:
# $1 - The temperature
# $2 - Maximum temperature
temp_color() {
local colors
read -ra colors <<< "$(interpolate_rgb "$COLOR_GRADIENT_START" "$COLOR_GRADIENT_END" 10)"
if not_empty "$2"; then
plookup "$1" "$2" "${colors[@]}"
fi
}
# Retrieve an item from the provided lookup table based on the percentage of a number
#
# Arguments:
# $1 - A number
# $2 - Number to get a percentage of
# $3 - An array of potential values
plookup() {
if empty "$2" || empty "$3"; then
return
fi
local table=( "${@:3}" )
local temp
temp=$(min "${1%.*}" "${2%.*}")
local percent_max=$(( temp * ${2%.*} / 100 ))
local index=$(( percent_max * ${#table[@]} / 100 ))
index=$(clamp $index 0 $(( ${#table[@]} - 1 )) )
echo "${table[$index]}"
}
# Output text wrapped in a span element
#
# Options:
# -c <color> To specify a text color
#
# Arguments:
# $1 or $3 - String to encapsulate within a span
span() {
local -A attribs
local text="$*"
if not_empty "$FONT"; then attribs[font]="$FONT"; fi
if [ "$1" = "-c" ]; then
if not_empty "$2"; then
# shellcheck disable=SC2034
attribs[color]="$2"
fi
text="${*:3}"
fi
echo "<span$(build_attribs attribs)>$text</span>"
}
# Builds html element attributes
#
# Arguments:
# $1 - An associative array of attribute-value pairs
build_attribs() {
local -n attrs=$1
for key in "${!attrs[@]}"; do
echo -n " $key='${attrs[$key]}'"
done
echo
}
# Colors text using either the first or second color
#
# Arguments:
# $1 - Text
# $2 - Color to use if toggle is false
# $3 - Color to use if toggle is true
# $4 - A boolean used to toggle between colors
#
# Returns:
# The colored text
multicolor() {
local color="$2"
local toggle=$4
if $toggle && not_empty "$3"; then
color="$3"
fi
if not_empty "$color"; then
span -c "$color" "$1"
else
span "$1"
fi
}
define_commands() {
# Query a sensor.
#
# Arguments:
# Sensor (string) Sensor to query.
query_sensor() {
local sensor=${1:?$(error 'Sensor is required')}
SENSORS=$(sensors -u "$sensor")
temp=$(temp)
if isset OVERRIDE_MAX_TEMP; then
temp_max="$OVERRIDE_MAX_TEMP"
else
temp_max=$(temp_max)
fi
if isset OVERRIDE_CRIT_TEMP; then
temp_crit="$OVERRIDE_CRIT_TEMP"
else
temp_crit=$(temp_crit)
fi
temp_color=$(temp_color "$temp" "$temp_max")
colored_temp=$(multicolor "${temp}$(temp_symbol)" "$COLOR_TEMP" "$temp_color" $GRADIENT)
echo -e "$colored_temp\n$colored_temp\n"
if isset temp_crit && [[ $(temp_gt_eq "$temp" "$temp_crit") ]]; then
EXITCODE="$EX_URGENT"
elif isset temp_max && [[ $(temp_gt_eq "$temp" "$temp_max") ]]; then
EXITCODE="$EX_URGENT"
fi
}
}
# Display program usage
usage() {
cat <<-EOF 1>&2
Usage: $0 [<options>] <command> [<args>]
Display a temperature indicator using lm_sensors for i3blocks.
${COLOR_YELLOW}Commands:${COLOR_RESET}
${COLOR_GREEN}query <sensor>${COLOR_RESET} query sensor
${COLOR_GREEN}help${COLOR_RESET} display help
${COLOR_YELLOW}Options:${COLOR_RESET}
-f display temperatures in fahrenheit
-G disable color gradient
-c <temp> override critical temperature
-m <temp> override maximum temperature
-h display this help and exit
EOF
exit "$EX_USAGE"
}
temp_gt_eq() {
(( ${1%.*} >= ${2%.*} ))
}
setup_color() {
if has_color; then
COLOR_RESET=$'\033[0m'
COLOR_RED=$'\033[0;31m'
COLOR_GREEN=$'\033[0;32m'
COLOR_YELLOW=$'\033[0;33m'
COLOR_MAGENTA=$'\033[0;35m'
COLOR_CYAN=$'\033[0;36m'
fi
}
# Rearrange all options to place flags first
# Author: greycat
# URL: https://mywiki.wooledge.org/ComplexOptionParsing
arrange_opts() {
local flags args optstr=$1
shift
while (($#)); do
case $1 in
--)
args+=("$@")
break;
;;
-*)
flags+=("$1")
if [[ $optstr == *"${1: -1}:"* ]]; then
flags+=("$2")
shift
fi
;;
*)
args+=("$1")
;;
esac
shift
done
OPTARR=("${flags[@]}" "${args[@]}")
}
parse_opts() {
local optstring=c:C:E:f:F:GhHm:S:t:
arrange_opts "$optstring" "$@"
set -- "${OPTARR[@]}"
OPTIND=1
while getopts "$optstring" opt; do
case "$opt" in
c ) OVERRIDE_CRIT_TEMP=$OPTARG ;;
C ) SYMBOL_C=$OPTARG ;;
E ) COLOR_GRADIENT_END=$OPTARG ;;
f ) FONT=$OPTARG ;;
F ) SYMBOL_F=$OPTARG ;;
G ) GRADIENT=false ;;
H ) FAHRENHEIT=true ;;
m ) OVERRIDE_MAX_TEMP=$OPTARG ;;
S ) COLOR_GRADIENT_START=$OPTARG ;;
t ) COLOR_TEMP=$OPTARG ;;
h | *) usage ;;
esac
done
read -ra CMDARGS <<< "${OPTARR[@]:$((OPTIND-1))}"
}
exec_command() {
IFS=' ' read -ra ARGS <<< "$1"
set -- "${ARGS[@]}"
COMMAND=${1:?$(error 'A command is required')}
shift
case "$COMMAND" in
query)
case "$#" in
1)
SENSOR="$1"
;;
*)
if empty "$SENSOR"; then
usage
fi
;;
esac
query_sensor "$SENSOR"
;;
output)
case "$#" in 0) usage ;; esac
# output_volume "$*"
exit "${EXITCODE:-$EX_OK}"
;;
# outputs)
# list_output_formats
# ;;
*)
usage
;;
esac
}
###############################################################################
main() {
# Getopt parsing variables
declare OPTIND
declare -a OPTARR CMDARGS
###########################################################
# Non-command line option variables
###########################################################
# Exit codes
declare -ir \
EX_OK=0 \
EX_URGENT=33 \
EX_USAGE=64
# Main program exit code
declare -i EXITCODE=$EX_OK
declare \
COLOR_RESET \
COLOR_RED \
COLOR_GREEN \
COLOR_YELLOW \
COLOR_MAGENTA \
COLOR_CYAN
###########################################################
# Command line option variables
###########################################################
declare \
COMMAND
define_helpers
define_commands
setup_color
parse_opts "$@"
exec_command "${CMDARGS[*]}"
exit ${EXITCODE:-$EX_OK}
}
main "$@"