Skip to content

Commit 33fa6eb

Browse files
committed
Finished project, everything working as expected
1 parent 4726fc0 commit 33fa6eb

File tree

1 file changed

+91
-13
lines changed

1 file changed

+91
-13
lines changed

src/llight.sh

+91-13
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,108 @@
11
#! /bin/bash
22

3-
echo "llight 1.0 2020"
3+
# main.sh
4+
# Josue Teodoro Moreira <[email protected]> <[email protected]>
5+
# December 13, 2020
46

5-
target_folder=/sys/class/backlight/intel_backlight/
7+
target_folder=/sys/class/backlight/intel_backlight
68
current_brightness=$(cat ${target_folder}/brightness)
79
max_brightness=$(cat ${target_folder}/max_brightness)
810

9-
# functions
11+
# print usage / help
1012
usage()
1113
{
12-
echo "$(basename $0) -[h | v | g | s | i | d]"
13-
echo " -h shows this help and exit"
14-
echo " -v shows version and exit"
15-
echo " -g shows current brightness and exit"
16-
echo " -s changes brightness by given value. Must be between 1 and ${max_brightness}"
17-
echo " -i increases brightness by given value"
18-
echo " -d decreases brightness by given value"
14+
echo "$(basename $0) -[h | v | g | s | i | d]."
15+
echo
16+
echo "Make sure to run llight with administrator rights. e. g. 'sudo llight -h'."
17+
echo
18+
echo " -h shows this help and exit."
19+
echo " -v shows version and exit."
20+
echo " -g shows current brightness and exit."
21+
echo " -s changes brightness by given value. Must be between 1 and 100."
22+
echo " -i increases brightness by given value."
23+
echo " -d decreases brightness by given value."
1924

2025
exit 1
2126
}
2227

23-
if [ $# -eq 0 ]; then
28+
set_by_value()
29+
{
30+
if [ ${1} -lt 1 ] || [ ${1} -gt 100 ]; then
31+
echo "Value must be between 1 and 100."
32+
33+
usage
34+
fi
35+
36+
let new_brightness=$((${max_brightness} / 100 * ${1}))
37+
echo "${new_brightness}" | sudo tee ${target_folder}/brightness > /dev/null
38+
}
39+
40+
increase_by_value()
41+
{
42+
if [ $((${current_brightness} + ${1})) -gt ${max_brightness} ]; then
43+
echo "Already on maximum brightness. Try a smaller number or -s instead."
44+
45+
usage
46+
fi
47+
48+
let new_brightness=$((${current_brightness} + ${1}))
49+
echo "${new_brightness}" | sudo tee ${target_folder}/brightness > /dev/null
50+
}
51+
52+
decrease_by_value()
53+
{
54+
if [ $((${current_brightness} - ${1})) -lt 1 ]; then
55+
echo "Already on minimum brightness. Try a smaller number or -s instead."
56+
57+
usage
58+
fi
59+
60+
let new_brightness=$((${current_brightness} - ${1}))
61+
echo "${new_brightness}" | sudo tee ${target_folder}/brightness > /dev/null
62+
}
63+
64+
if [ $# -gt 2 ] || [ $# -eq 0 ]
65+
then
2466
usage
2567
fi
2668

27-
# truncate -s 0 ${target_folder}/brightness
28-
# echo ${current_brightness} > ${target_folder}/brightness
69+
# get options
70+
options=":hvgsid"
71+
getopts ${options} args
72+
73+
case ${args} in
74+
h)
75+
usage
76+
77+
;;
78+
v)
79+
echo "llight 1.0"
80+
echo "Developed by Josue Teodoro Moreira"
81+
82+
;;
83+
g)
84+
echo ${current_brightness}
85+
86+
;;
87+
s)
88+
set_by_value ${2}
89+
90+
;;
91+
i)
92+
increase_by_value ${2}
93+
94+
;;
95+
d)
96+
decrease_by_value ${2}
97+
98+
;;
99+
*)
100+
echo "Invalid argument -${OPTARG}"
101+
echo
102+
usage
103+
104+
;;
105+
esac
106+
29107

30108
exit 0

0 commit comments

Comments
 (0)