From 33fa6eba049c84e92169d9ff76cd57b08e611393 Mon Sep 17 00:00:00 2001 From: J0sueTM Date: Sun, 13 Dec 2020 23:13:17 -0300 Subject: [PATCH] Finished project, everything working as expected --- src/llight.sh | 104 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 91 insertions(+), 13 deletions(-) diff --git a/src/llight.sh b/src/llight.sh index 731fb73..0148d1b 100755 --- a/src/llight.sh +++ b/src/llight.sh @@ -1,30 +1,108 @@ #! /bin/bash -echo "llight 1.0 2020" +# main.sh +# Josue Teodoro Moreira +# December 13, 2020 -target_folder=/sys/class/backlight/intel_backlight/ +target_folder=/sys/class/backlight/intel_backlight current_brightness=$(cat ${target_folder}/brightness) max_brightness=$(cat ${target_folder}/max_brightness) -# functions +# print usage / help usage() { - echo "$(basename $0) -[h | v | g | s | i | d]" - echo " -h shows this help and exit" - echo " -v shows version and exit" - echo " -g shows current brightness and exit" - echo " -s changes brightness by given value. Must be between 1 and ${max_brightness}" - echo " -i increases brightness by given value" - echo " -d decreases brightness by given value" + echo "$(basename $0) -[h | v | g | s | i | d]." + echo + echo "Make sure to run llight with administrator rights. e. g. 'sudo llight -h'." + echo + echo " -h shows this help and exit." + echo " -v shows version and exit." + echo " -g shows current brightness and exit." + echo " -s changes brightness by given value. Must be between 1 and 100." + echo " -i increases brightness by given value." + echo " -d decreases brightness by given value." exit 1 } -if [ $# -eq 0 ]; then +set_by_value() +{ + if [ ${1} -lt 1 ] || [ ${1} -gt 100 ]; then + echo "Value must be between 1 and 100." + + usage + fi + + let new_brightness=$((${max_brightness} / 100 * ${1})) + echo "${new_brightness}" | sudo tee ${target_folder}/brightness > /dev/null +} + +increase_by_value() +{ + if [ $((${current_brightness} + ${1})) -gt ${max_brightness} ]; then + echo "Already on maximum brightness. Try a smaller number or -s instead." + + usage + fi + + let new_brightness=$((${current_brightness} + ${1})) + echo "${new_brightness}" | sudo tee ${target_folder}/brightness > /dev/null +} + +decrease_by_value() +{ + if [ $((${current_brightness} - ${1})) -lt 1 ]; then + echo "Already on minimum brightness. Try a smaller number or -s instead." + + usage + fi + + let new_brightness=$((${current_brightness} - ${1})) + echo "${new_brightness}" | sudo tee ${target_folder}/brightness > /dev/null +} + +if [ $# -gt 2 ] || [ $# -eq 0 ] +then usage fi -# truncate -s 0 ${target_folder}/brightness -# echo ${current_brightness} > ${target_folder}/brightness +# get options +options=":hvgsid" +getopts ${options} args + +case ${args} in + h) + usage + + ;; + v) + echo "llight 1.0" + echo "Developed by Josue Teodoro Moreira" + + ;; + g) + echo ${current_brightness} + + ;; + s) + set_by_value ${2} + + ;; + i) + increase_by_value ${2} + + ;; + d) + decrease_by_value ${2} + + ;; + *) + echo "Invalid argument -${OPTARG}" + echo + usage + + ;; +esac + exit 0