.dotfiles/.local/bin/light

47 lines
971 B
Bash
Executable File

#!/bin/bash -e
function round() {
local nb="$1"
local closest="${2#-}"
local mod; mod="$((nb % closest))"
if [ "$mod" -lt "$((closest / 2))" ]; then
echo "$((nb - mod))"
else
echo "$((nb + closest - mod))"
fi
}
function get_current() {
local cur; cur="$(cat "$1/brightness")"
local max; max="$(cat "$1/max_brightness")"
echo "$((cur * 100 / max))"
}
function set_light() {
local max; max="$(cat "$1/max_brightness")"
echo "$((CUR * max / 100))" > "$1/brightness"
}
NOTIFY=(notify-send -t 1000 -a "$(basename "$0")" -u low)
DIRS=(/sys/class/backlight/*)
CUR="$(get_current "${DIRS[0]}")"
case "${1:0:1}" in
'') echo "$CUR"; exit ;;
'+'|'-') CUR="$(round $((CUR + $1)) "$1")" ;;
*) CUR="$1" ;;
esac
[ "$CUR" -lt 0 ] && CUR=0
[ "$CUR" -gt 100 ] && CUR=100
for d in "${DIRS[@]}"; do
set_light "$d"
done
"${NOTIFY[@]}" -h string:synchronous:light -h int:value:"$CUR" "$CUR%"