-
Notifications
You must be signed in to change notification settings - Fork 0
/
brightness
executable file
·112 lines (82 loc) · 2.05 KB
/
brightness
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
#!/bin/bash
set -e
set -o pipefail
################################################################################
function error {
echo "ERROR: ${*}" >/dev/stderr
}
function cmd-exist {
builtin type -P "${1}" &>/dev/null
}
################################################################################
function read-value {
local type value
type="${1}"
value="${2}"
{ grep -oP "${type} value = +\K\w+" | tr -d '\n'; } <<< "${value}"
}
function get-brightness {
local value
value="$(sudo ddcutil getvcp 10)"
echo -n "Current brightness is "
read-value current "${value}"
echo -n '/'
read-value max "${value}"
echo
}
function set-brightness {
sudo ddcutil setvcp 10 "${1}"
}
################################################################################
function parse-args {
local opts flags script
script="$(basename "${0}")"
flags="help"
opts="$(getopt -o h --long "${flags}" -n "${script}" -- "${@}")"
eval set -- "$opts"
while true; do
case "${1}" in
-h | --help)
usage
exit
;;
-- ) shift; break ;;
* ) break ;;
esac
done
case "${1}" in
''|*[!0-9]*)
error "Bad brighness value. Should be 0-10!"
return 3
;;
*)
if ! (( 0 < $1 || $1 <= 100 )) ; then
error "Bad brighness value. Should be 0-10!"
return 3
fi
ARG_BRIGHTNESS="${1}"
;;
esac
export ARG_BRIGHTNESS
}
function usage {
cat <<EOM
Usage: $(basename "${0}") <BRIGHTNESS>
Set brightness of external display to 0-10 (in steps of 10%).
EOM
}
function main {
parse-args "${@}"
if ! cmd-exist ddcutil; then
error "Missing ddcutil. Install before proceeding!"
return 4
fi
if [ -v DEBUG ]; then
get-brightness
fi
set-brightness "${ARG_BRIGHTNESS}"
if [ -v DEBUG ]; then
get-brightness
fi
}
main "${@}"; exit