-
Notifications
You must be signed in to change notification settings - Fork 23
/
lightctl
executable file
·136 lines (117 loc) · 3.07 KB
/
lightctl
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
#!/bin/sh
#---help---
# Usage:
# $0 [options] <command> [<value>%]
# $0 [options] (+|-)[<value>%]
# $0 [options] [=]<value>%
# $0 -h
#
# Control device brightness using 'brightnessctl' or 'light' and show
# an on-screen notification using 'avizo-client'.
#
# Commands:
# + | up Increase brightness by <value> percents (defaults to 5).
# - | down Decrease brightness by <value> percents (defaults to 5).
# = | set Set brightness to <value> percents.
#
# Options:
# -e <exponent> Change percentage curve to exponential (only for brightnessctl).
# -D <device> Specify the device name.
# -M <monitornr> Set monitor where notification will be shown.
# -d Show dark theme friendly variant of the icon
# -h Show this message and exit.
#
# This script is part of the avizo package.
#---help---
# This script is POSIX Shell Command Language compliant.
set -eu
PROGNAME='lightctl'
help() {
local tag='---help---'
sed -n "/^#$tag/,/^#$tag/{/^#$tag/d; s/^# \\?//; s|\$0|$0|; p}" "$0"
}
die() {
printf "$PROGNAME: %s\n" "$1" >&2
exit 1
}
is_integer() {
expr "$1" : '[0-9]\+$' >/dev/null
}
is_float() {
expr "$1" : '[0-9]\+\.[0-9]\+$' >/dev/null
}
exp=
dev=
optind=1
dark=0
monitor=-1
while getopts ':e:D:hM:d' OPT; do
case "$OPT" in
e) exp=$OPTARG;;
D) dev=$OPTARG;;
h) help; exit 0;;
M) monitor="$OPTARG";;
d) dark=1;;
\?) case "$OPTARG" in
[0-9]*) break;;
*) die "unknown option: -$OPTARG";;
esac
;;
esac
optind=$OPTIND # hack to make -<value>% work correctly
done
shift $((optind -1))
if [ $# -lt 1 ] || [ $# -gt 2 ]; then
die "invalid number of arguments (see '$0 -h')"
fi
case "$1" in
[=+-][0-9]*%) cmd=${1%%[0-9]*}; value=${1#[=+-]};;
[0-9]*%) cmd='='; value=$1;;
*) cmd=$1; value=${2:-5};;
esac
value=${value%\%}
if ! is_integer "$value"; then
die "invalid value: '$value'"
fi
# Note: 'raise' and 'lower' are only for backward compatibility with
# the previous version of this script.
case "$cmd" in
+ | up | raise) cmd='+';;
- | down | lower) cmd='-';;
= | set) cmd='=';;
*) die "invalid command: $cmd (see '$0 -h')";;
esac
if command -v brightnessctl >/dev/null; then
prog='brightnessctl'
opts="${dev:+-d $dev}"
out=$(brightnessctl $opts ${exp:+-e$exp} -m set "${value}%${cmd}")
light=$(printf '%s\n' "$out" | cut -d, -f4)
light=${light%\%}
elif command -v light >/dev/null; then
prog='light'
opts="${dev:+-s $dev}"
case "$cmd" in
+) light $opts -A "$value";;
-) light $opts -U "$value";;
=) light $opts -S "$value";;
esac
light="$(light $opts -G)"
light="${light%.*}" # strip decimal part
else
die 'command not found: brightnessctl or light'
fi
if ! is_integer "$light"; then
die "$prog returned invalid brigtness: '$light'"
fi
if [ "$light" -le 33 ]; then
image='brightness_low'
elif [ "$light" -le 66 ]; then
image='brightness_medium'
else
image='brightness_high'
fi
if [ "$dark" -eq 1 ]; then
image="${image}_dark"
fi
progress=$(echo "$light" | awk '{ printf "%.2f", $1 / 100 }')
avizo-client --image-resource="$image" --progress="$progress" --monitor="$monitor"