Added the ability to read the config file

This commit is contained in:
Karma Riuk 2021-03-07 22:19:14 +01:00
parent 8128935e30
commit d4faf04dbc

118
brightctl
View File

@ -1,33 +1,95 @@
#!/bin/bash
## Default controller values
BRIGHTNESS_CONTROLLER="light"
INCREASE_FLAG="-A"
DECREASE_FLAG="-U"
SET_BRIGHTNESS_FLAG="-S"
GET_BRIGHTNESS_FLAG="-G"
ADDITIONAL_FLAGS=()
DEFAULT_BRIGHTNESS_CONTROLLER="light"
DEFAULT_INCREASE_FLAG="-A"
DEFAULT_DECREASE_FLAG="-U"
DEFAULT_SET_BRIGHTNESS_FLAG="-S"
DEFAULT_GET_BRIGHTNESS_FLAG="-G"
DEFAULT_ADDITIONAL_FLAGS=()
## Default values
NOTIFICATION_ENABLED=1
NOTIFICATION_TIMEOUT=3000
DEFAULT_NOTIFICATION_ENABLED=1
DEFAULT_NOTIFICATION_TIMEOUT=3000
DEFAULT_NORMAL_DELTA=5
DEFAULT_BIG_DELTA=10
DEFAULT_BRIGTHNESS_MAX=100
DEFAULT_BRIGTHNESS_MIN=0.1
CONFIG_PATH="$XDG_CONFIG_HOME"
VERBOSE=0
NORMAL_DELTA=5
BIG_DELTA=10
BRIGTHNESS_MAX=100
BRIGTHNESS_MIN=0.1
CONFIG_PATH="${XDG_CONFIG_HOME:-$HOME/.config}/brightctl/config.ini"
READ_INI_POSSIBLE_PATHS=(\
"${XDG_LIB_HOME:-$HOME/.local/lib}/read_ini.sh" "${XDG_LIB_HOME:-$HOME/.local/lib}/bash_ini_parser/read_ini.sh" \
"./read_ini.sh" "./bash_ini_parser/read_ini.sh" \
"./lib/read_ini.sh" "./lib/bash_ini_parser/read_ini.sh" \
)
## Args
OPTIONAL_FLAGS=("--notification-timeout" "-v" "--verbose" "-c" "--config" "-n" "--notifcation" "-N" "--no-notification" "-h" "--help")
OPTIONAL_FLAGS=( \
"-v" "--verbose" \
"-c" "--config" \
"-h" "-?" "--help" \
)
verbose () {
[[ $VERBOSE -eq 1 ]] && return 0 || return 1
}
get_parser_location () {
local parser_location=""
for location in "${READ_INI_POSSIBLE_PATHS[@]}"; do
[[ -f "$location" ]] && parser_location="$location" && break
done
echo "$parser_location"
}
checkout_config () {
local parser_location="$(get_parser_location)"
[[ -z "$parser_location" ]] && verbose && echo "Unable to find the 'read_ini.sh' file, which is used to read the config file. Will be using the default vlaues of the script." && return 0
verbose && echo "Sourcing the ini parser"
source "$parser_location"
verbose && echo "Reading the config file"
read_ini "$CONFIG_PATH"
BRIGHTNESS_CONTROLLER=${INI__Controller__brightness_controller:-$DEFAULT_BRIGHTNESS_CONTROLLER}
INCREASE_FLAG=${INI__Controller__increase_flag:-$DEFAULT_INCREASE_FLAG}
DECREASE_FLAG=${INI__Controller__decrease_flag:-$DEFAULT_DECREASE_FLAG}
SET_BRIGHTNESS_FLAG=${INI__Controller__set_brightness_flag:-$DEFAULT_SET_BRIGHTNESS_FLAG}
GET_BRIGHTNESS_FLAG=${INI__Controller__get_brightness_flag:-$DEFAULT_GET_BRIGHTNESS_FLAG}
ADDITIONAL_FLAGS=${INI__Controller__additional_flags:-$DEFAULT_ADDITIONAL_FLAGS}
NORMAL_DELTA=${INI__Controller__normal_delta:-$DEFAULT_NORMAL_DELTA}
BIG_DELTA=${INI__Controller__big_delta:-$DEFAULT_BIG_DELTA}
BRIGTHNESS_MAX=${INI__Controller__brigthness_max:-$DEFAULT_BRIGTHNESS_MAX}
BRIGTHNESS_MIN=${INI__Controller__brigthness_min:-$DEFAULT_BRIGTHNESS_MIN}
NOTIFICATION_ENABLED=${INI__Notification__enabled:-$DEFAULT_NOTIFICATION_ENABLED}
NOTIFICATION_TIMEOUT=${INI__Notification__timeout:-$DEFAULT_NOTIFICATION_TIMEOUT}
verbose && echo "" && echo "After parsing the config file, variables are:" && \
echo "BRIGHTNESS_CONTROLLER: '$BRIGHTNESS_CONTROLLER'" && \
echo "INCREASE_FLAG: '$INCREASE_FLAG'" && \
echo "DECREASE_FLAG: '$DECREASE_FLAG'" && \
echo "SET_BRIGHTNESS_FLAG: '$SET_BRIGHTNESS_FLAG'" && \
echo "GET_BRIGHTNESS_FLAG: '$GET_BRIGHTNESS_FLAG'" && \
echo "ADDITIONAL_FLAGS: '$ADDITIONAL_FLAGS'" && \
echo "NORMAL_DELTA: '$NORMAL_DELTA'" && \
echo "BIG_DELTA: '$BIG_DELTA'" && \
echo "BRIGTHNESS_MAX: '$BRIGTHNESS_MAX'" && \
echo "BRIGTHNESS_MIN: '$BRIGTHNESS_MIN'" && \
echo "NOTIFICATION_ENABLED: '$NOTIFICATION_ENABLED'" && \
echo "NOTIFICATION_TIMEOUT: '$NOTIFICATION_TIMEOUT'" && \
echo ""
}
notify () {
local brightness=$(get_brightness | sed 's/\..*$//')
local cmd="dunstify -h int:value:$brightness -t $NOTIFICATION_TIMEOUT -h string:x-dunst-stack-tag:volume -i \"xfpm-brightness-lcd\" \"Brightness ($brightness%)\""
@ -116,17 +178,14 @@ Options:
--set-max-brightness set brightness to maximum
Optional arguments:
--notification-timeout arg forces the notification timeout, in milliseconds (overrides the configuation setting,
the default value is $NOTIFICATION_TIMEOUT)
-n, --notification forces the notification to appear (overrides the configuration setting)
-N, --no-notification forces the notification NOT to appear (overrides the configuration setting)
-c path, --config path instead of reading from the default location ($CONFIG_PATH),
read from the path given
-v, --verbose print each step the script passes through
-h, -?, --help show this message
EOF
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then #if '__name__' == '__main__'
# check if the notification is being forced or not
while [[ " ${OPTIONAL_FLAGS[@]} " =~ " $1 " ]]; do
verbose && echo "Checking for optional arguments"
@ -134,29 +193,14 @@ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then #if '__name__' == '__main__'
case $1 in
-v|--verbose)
VERBOSE=1
verbose && echo "Setting \$VERBOSE to true (\$VERBOSE=$VERBOSE)"
echo "Setting \$VERBOSE to true (\$VERBOSE=$VERBOSE)"
shift
;;
--notification-timeout)
NOTIFICATION_TIMEOUT="$2";
verbose && echo "Setting \$NOTIFICATION_TIMEOUT to $2 (\$NOTIFICATION_TIMEOUT=$NOTIFICATION_TIMEOUT)"
shift 2
;;
-c|--config)
CONFIG_PATH="$2";
[[ -f "$2" ]] && CONFIG_PATH="$2" || echo "Error: the config file given '$2' doesn't exists." 1>&2 && exit 1
verbose && echo "Setting \$CONFIG_PATH to $2 (\$CONFIG_PATH=$CONFIG_PATH)"
shift 2
;;
-n|--notification)
NOTIFICATION_ENABLED=1
verbose && echo "Setting \$NOTIFICATION_ENABLED to true (\$NOTIFICATION_ENABLED=$NOTIFICATION_ENABLED)"
shift
;;
-N|--no-notification)
NOTIFICATION_ENABLED=0;
verbose && echo "Setting \$NOTIFICATION_ENABLED to false (\$NOTIFICATION_ENABLED=$NOTIFICATION_ENABLED)"
shift
;;
-h|-\?|--help)
verbose && echo "Detected -h, -? or --help"
usage
@ -165,6 +209,8 @@ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then #if '__name__' == '__main__'
esac
done
checkout_config
verbose && echo "Looking at the argument '$@'"
case $1 in
-i|--increase)