Created
October 17, 2018 13:01
-
-
Save j-brn/b7c547727a4c4f9797404019bc77c29a to your computer and use it in GitHub Desktop.
Simple wrapper script around xrandr to easily change the display brightness
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # set the screen brightness on all outputs | |
| ### variables ### | |
| # You can configure the script by editing these vars, it should not be necessary to change anything else | |
| max="2" # max allowed brightness | |
| min="0.2" # min allowed brightness (0 = blackscreen) | |
| prefix="$(echo ${0} | rev | cut -d "/" -f 1 | rev)" # this name is used as prefix for all output in this script | |
| ## end ## | |
| # help text | |
| read -r -d '' help_text <<EOF | |
| ${prefix} | |
| a simple tool for setting the display brightness using xrandr | |
| usage: | |
| ${prefix} <brightness: number/float> ARGS | |
| args: | |
| -s --silent don't output anything except errors | |
| -f --force bypass checking if the submitted brightness value is allowed (validation is done anyway) | |
| -h --help show this help | |
| author: Jonas Braun <jonas.braun@jbrn.eu> | |
| EOF | |
| # prints the given messages with prefix | |
| function print_messages() { | |
| for msg in "${@}"; do | |
| echo "${prefix}: ${msg}" | |
| done | |
| } | |
| # prints message(es) with prefix and exits with code 1 | |
| function print_errors_and_exit() { | |
| for msg in "${@}"; do | |
| >&2 echo "${prefix}: ${msg}" | |
| done | |
| exit 1 | |
| } | |
| # flag defaults | |
| force=0 | |
| silent=0 | |
| # fetch flags | |
| for arg in ${@}; do | |
| [ ${arg} == "--force" ] || [ ${arg} == "-f" ] && force=1 && continue | |
| [ ${arg} == "--silent" ] || [ ${arg} == "-s" ] && silent=1 && continue | |
| [ ${arg} == "--help" ] || [ ${arg} == "-h" ] && >&2 echo "${help_text}" && exit 0 | |
| [[ "${arg}" =~ ^[0-9]+\.?[0-9]*$ ]] && brightness=${arg} && continue | |
| print_errors_and_exit "Unknown argument '${arg}'. Use --help or -h for for more information." | |
| done | |
| # check if brightness is set | |
| [ -z ${brightness+x} ] && print_errors_and_exit "Brightness is not specified. Use --help or -h for more information." | |
| # don't check if brightness is too high or low if the force flag is set | |
| if [ ${force} == 0 ]; then | |
| ! [ $(echo "${brightness} >= ${min}" | bc -l) == 1 ] && \ | |
| print_errors_and_exit "The specified brightness is to low. The lowest allowed value is ${min}" | |
| ! [ $(echo "${max} >= ${brightness}" | bc -l) == 1 ] && \ | |
| print_errors_and_exit "The specified brightness is to high. The highest allowed value is ${max}" | |
| fi | |
| #set the specified brightness for all output devices using xrandr | |
| for display in $(xrandr -q | grep " connected" | cut -d " " -f 1); do | |
| xrandr --output ${display} --brightness ${brightness} && \ | |
| [ ${silent} == 0 ] && print_messages "Brightness was set to ${brightness} for ${display}" | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment