Last active
March 15, 2018 09:50
-
-
Save caub/9281645c30e12b92c040795fcc17c205 to your computer and use it in GitHub Desktop.
This file contains 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 | |
regexp='^[-+]?[0-9]*(\.[0-9]*)?$' | |
validateNumber() { | |
if [[ $1 == *[0-9]* && $1 =~ $regexp ]]; then | |
echo "$1" | |
else | |
printf >&2 '%s is not a valid number\n' "$1" | |
fi | |
} | |
# compare 2 numbers, similar and faster than if (( $(bc <<<"$1 < $2") )) | |
isGreater() { | |
local v1 | |
local v2 | |
LC_NUMERIC=C printf -v v1 '%f' "$1" | |
LC_NUMERIC=C printf -v v2 '%f' "$2" | |
local i1="${v1%.*}" | |
local i2="${v2%.*}" | |
local d1="${v1#*.}" | |
local d2="${v2#*.}" | |
d1="${d1:0:2}" | |
d2="${d2:0:2}" | |
if [[ "$d1" == 0* ]]; then d1="${d1:1}"; fi # deal with octals | |
if [[ "$d2" == 0* ]]; then d2="${d2:1}"; fi | |
# now compare integer parts or decimal parts if needed | |
return $(( i1 == i2 ? d1 <= d2 : i1 <= i2 )) | |
} | |
# add 2 numbers (2 decimals max, similar and faster than echo "scale=2; $1 + $2" | bc) | |
addNum() { | |
local v1 | |
local v2 | |
LC_NUMERIC=C printf -v v1 '%f' "$1" | |
LC_NUMERIC=C printf -v v2 '%f' "$2" | |
local i1="${v1%.*}" | |
local i2="${v2%.*}" | |
local d1="${v1#*.}" | |
local d2="${v2#*.}" | |
d1="${d1:0:2}" | |
d2="${d2:0:2}" | |
if [[ "$d1" == 0* ]]; then d1="${d1:1}"; fi # deal with octals | |
if [[ "$d2" == 0* ]]; then d2="${d2:1}"; fi | |
if [[ "$i1" == -* ]]; then d1="-$d1"; fi | |
if [[ "$i2" == -* ]]; then d2="-$d2"; fi | |
local d3=$(( d1 + d2 )) | |
local i3=$(( i1 + i2 )) | |
if (( d3 >= 100 )); then | |
d3=$(( d3 - 100 )) | |
i3=$(( i3 + 1 )) | |
elif (( d3 <= -100 )); then | |
d3=$(( -d3 - 100 )) | |
i3=$(( i3 - 1 )) | |
elif (( d3 < 0 && i3 > 0 )); then | |
d3=$(( d3 + 100 )) | |
i3=$(( i3 - 1 )) | |
elif (( d3 > 0 && i3 < 0 )); then | |
d3=$(( d3 - 100 )) | |
i3=$(( i3 + 1 )) | |
fi | |
if (( d3 < 0 && i3 >= 0 )); then | |
i3="-${i3#-}" | |
fi | |
local u="00${d3#-}" | |
d3="${u:0-2}" | |
echo "$i3.$d3" | |
} | |
getCurrent() { | |
local bl=$(xbacklight -get) | |
if [[ "$bl" == 0.* || "$bl" == 1.00* ]]; then | |
xrandr --verbose | awk '/Brightness/ { print $2; exit }' | |
else | |
echo "$bl" | |
fi | |
} | |
if [ ! "$1" ]; then | |
xrandr --verbose | grep -E '(Brightness|Gamma|Backlight)' | |
exit | |
fi | |
# use this to decr or incr brightness: light +/- value? | |
case "$1" in | |
[+-]) | |
c=$(getCurrent) | |
auto=0 | |
step="$1$2" | |
if [[ ! "$2" ]]; then | |
auto=1 | |
step="${1}1" | |
if [[ "$c" == 0.* || ("$1" == '-' && "$c" == 1.*) ]]; then | |
step="$1.05" | |
fi | |
fi | |
val=$(addNum "$c" "$step");; | |
*) | |
val=$(validateNumber "$1");; | |
esac | |
if [[ ! $val ]]; then | |
echo "nope:" "$@" | |
exit | |
fi | |
if [[ $val == 0.* || $val == .* ]]; then | |
# xbacklight -set 1 | |
tee /sys/class/backlight/intel_backlight/brightness <<< 30 | |
if isGreater "$val" 0.66; then | |
xrandr --output eDP1 --brightness "$val" --gamma 1.0:0.8:0.65 | |
else | |
xrandr --output eDP1 --brightness "$val" --gamma 1.0:0.7:0.45 | |
fi | |
else | |
xbacklight -set "$val" | |
if (( val < 3 )); then | |
xrandr --output eDP1 --brightness 1 --gamma 1.0:0.9:0.82 | |
elif (( val < 10 )); then | |
xrandr --output eDP1 --brightness 1 --gamma 1.0:0.96:0.9 | |
else | |
xrandr --output eDP1 --brightness 1 --gamma 1.0:1.0:0.95 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this file above is in my home
I've added a sudoers entry to run the line 102
caub@inspiron:~$ sudo visudo -f /etc/sudoers.d/light
now
sudo ~/.light .7
works fine, you can add a bash_aliasaslias li='sudo ~/.light'