Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

luxsh: added left paren, replaced some tests with case. #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 44 additions & 37 deletions lux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
usage() {
echo 'Usage: lux OPERATION [-c CONTROLLER_NAME] [-m MIN] [-M MAX]
cat <<-'USAGE'
Usage: lux OPERATION [-c CONTROLLER_NAME] [-m MIN] [-M MAX]

Brightness option values are positive integers.
Percent mode, add "%" after values (operation options only).
Expand Down Expand Up @@ -49,72 +50,79 @@ Controllers:
-c CONTROLLER_NAME
Set the controller to use.
Use any CONTROLLER_NAME in /sys/class/backlight.
'
USAGE
}

version() {
echo 'Lux 1.21
cat <<-'VERSION'
Lux 1.21
Copyright (C) 2020 Thomas "Ventto" Venriès.

License MIT
<https://opensource.org/licenses/MIT>.
'
VERSION
}

no_conjunction() {
while [ "$#" -ne 0 ]; do $1 && return 1; shift; done
while :; do
case "$#" in (0) break; esac
$1 && return 1
shift
done
}

arg_err() {
usage; exit 2
}

is_positive_int() {
echo "${1}" | grep -E '^[0-9]+$' >/dev/null 2>&1 || return 1
case "$1" in (''|*[!0-9]*) return 1; esac
return 0
}

is_percentage() {
echo "${1}" | grep -E '%$' >/dev/null 2>&1 || return 1
case "$1" in (*'%') return 0; esac
return 1
}

check_perm() {
_ctrl="${1}"
udev_rule="/etc/udev/rules.d/99-lux.rules"

if [ ! -w "${_ctrl}/brightness" ] ; then
if [ "$(id -u)" -ne 0 ]; then
case "$(id -u)" in (0) :;; (*)
if ! id -nG "${USER}" | grep video >/dev/null 2>&1; then
echo "Use sudo once to setup group permissions,"
echo "to access to controller's brightness from user."
else
echo "To setup the group permissions permanently, you need to logout/login."
fi
exit
fi
esac
fi

if [ "$(id -u)" -eq 0 ]; then
case "$(id -u)" in (0)
if ! cut -d: -f1 /etc/group | grep video >/dev/null 2>&1; then
echo "Group ~video~ does not exist."
exit 1
fi

if [ -z "$(find "${_ctrl}" -name 'brightness' -group video)" ]; then
case "$(find "${_ctrl}" -name 'brightness' -group video)" in ('')
if [ ! -f "${udev_rule}" ] ; then
echo "${udev_rule}: missing file."
exit 1
fi
udevadm control -R
udevadm trigger -c add -s backlight
fi
esac

if ! id -nG "${SUDO_USER}" | grep video >/dev/null 2>&1; then
usermod -a -G video "${SUDO_USER}"
echo "User has been added to ~video~ group."
echo "To setup the group permissions permanently, you need to logout/login."
exit
fi
fi
esac
}

main() {
Expand All @@ -129,58 +137,58 @@ main() {

while getopts 'hvgGm:M:c:a:s:S:' opt; do
case $opt in
h) usage ; exit;;
v) version; exit;;
g|G) ! no_conjunction "${sFlag}" "${SFlag}" "${aFlag}" && arg_err
[ "$opt" = 'G' ] && percent_mode=true
(h) usage ; exit;;
(v) version; exit;;
(g|G) ! no_conjunction "${sFlag}" "${SFlag}" "${aFlag}" && arg_err
case "$opt" in ('G') percent_mode=true; esac
gFlag=true
;;
m) ! is_positive_int "$OPTARG" && arg_err
(m) ! is_positive_int "$OPTARG" && arg_err
mFlag=true
mArg="$OPTARG"
;;
M) ! is_positive_int "$OPTARG" && arg_err
MFlag=true
MArg="$OPTARG"
;;
c) controller_path="/sys/class/backlight/$OPTARG"
(c) controller_path="/sys/class/backlight/$OPTARG"
if [ ! -d "${controller_path}" ] ; then
echo "${controller_path}: controller not found."
exit
fi
[ "$#" -eq 2 ] && arg_err
case "$#" in (2) arg_err; esac
cFlag=true
cArg="${controller_path}"
;;
a) ! no_conjunction "${sFlag}" "${SFlag}" "${gFlag}" && arg_err
(a) ! no_conjunction "${sFlag}" "${SFlag}" "${gFlag}" && arg_err
if is_percentage "$OPTARG"; then
percent_mode=true
OPTARG=$(echo "$OPTARG" | cut -d'%' -f1)
OPTARG="${OPTARG%%'%'*}"
fi
! is_positive_int "$OPTARG" && arg_err
aFlag=true
valArg="$OPTARG"
;;
s) ! no_conjunction "${aFlag}" "${SFlag}" "${gFlag}" && arg_err
(s) ! no_conjunction "${aFlag}" "${SFlag}" "${gFlag}" && arg_err
if is_percentage "$OPTARG"; then
percent_mode=true
OPTARG=$(echo "$OPTARG" | cut -d'%' -f1)
OPTARG="${OPTARG%%'%'*}"
fi
! is_positive_int "$OPTARG" && arg_err
sFlag=true
valArg="$OPTARG"
;;
S) ! no_conjunction "${aFlag}" "${sFlag}" "${gFlag}" && arg_err
(S) ! no_conjunction "${aFlag}" "${sFlag}" "${gFlag}" && arg_err
if is_percentage "$OPTARG"; then
percent_mode=true
OPTARG=$(echo "$OPTARG" | cut -d'%' -f1)
OPTARG="${OPTARG%%'%'*}"
fi
! is_positive_int "$OPTARG" && arg_err
SFlag=true
valArg="$OPTARG"
;;
\?) usage; exit 2;;
:) usage; exit 2;;
(\?)usage; exit 2;;
(:) usage; exit 2;;
esac
done

Expand All @@ -200,10 +208,10 @@ main() {
best_controller="${ctrl}"
fi
done
if [ -z "${best_controller}" ]; then
case "${best_controller}" in ('')
echo "No backlight controller detected"
exit
fi
esac
fi

check_perm "${best_controller}"
Expand All @@ -213,10 +221,10 @@ main() {
best_max=$((best_max - 1))

# Display controller information if no argument
if [ "$#" -eq 0 ] ; then
case "$#" in (0)
echo "${best_controller} 0;${brightness};${best_max}"
exit
fi
esac

if ${gFlag}; then
${percent_mode} && { echo "$((brightness * 100 / best_max))%"; exit; }
Expand All @@ -226,7 +234,7 @@ main() {

if ${SFlag} ; then
${percent_mode} && valArg=$(( best_max * valArg / 100 ))
[ "$valArg" -lt 0 ] && valArg=0
case "$valArg" in (-*) valArg=0; esac
[ "$valArg" -gt "$best_max" ] && valArg="${best_max}"
echo "${valArg}" | tee "${file}"
exit
Expand All @@ -235,10 +243,9 @@ main() {
${mFlag} && own_min="${mArg}" || own_min=0
${MFlag} && own_max="${MArg}" || own_max="${best_max}"

if [ $(( own_max - own_min )) -le 0 ] || \
[ $(( own_max - own_min )) -gt "${best_max}" ] ; then
arg_err
fi
case $(( own_max - own_min )) in ([1-9]*)
[ $(( own_max - own_min )) -gt "${best_max}" ] && arg_err
esac

[ "$brightness" -lt "$own_min" ] && brightness="${own_min}"
[ "$brightness" -gt "$own_max" ] && brightness="${own_max}"
Expand Down