nightlight/nightlight
2021-03-29 22:24:48 +02:00

97 lines
2.4 KiB
Bash
Executable file

#!/bin/sh
# SETTINGS
# Edit from here -->
location="GMXX0128" # Location code (get it from https://weather.codes/search/)
displays=("eDP-1-1") # Displays e.g. ("eDP-1-1" "eDP-1-2") (get displays with "xrandr --listmonitors")
window=60 # Fading window in minutes
# <-- to here
if ! command -v wget &> /dev/null
then
echo -ne "\e[1mwget\e[0m was not found, please install it"
exit 1
elif ! command -v bc &> /dev/null
then
echo -ne "\e[1mbc\e[0m was not found, please install it"
exit 1
elif ! command -v xrandr &> /dev/null
then
echo -ne "\e[1mxrandr\e[0m was not found, are you running the X.Org Server?"
exit 1
fi
file=/tmp/$location
window=$(( $window * 30 ))
function get_times {
wget -q "https://weather.com/weather/today/l/$location" -O "$file"
SUNR=$(grep SunriseSunset "$file" | grep -oE '((1[0-2]|0?[1-9]):([0-5][0-9]) ?([AaPp][Mm]))' | head -1)
SUNS=$(grep SunriseSunset "$file" | grep -oE '((1[0-2]|0?[1-9]):([0-5][0-9]) ?([AaPp][Mm]))' | tail -1)
sunrise=$(date --date="$SUNR" +%s)
sunset=$(date --date="$SUNS" +%s)
}
function set_dim_value {
# Get current time
now=$(date +%s)
# Night - pre day
if (( $now <= $sunrise - $window )); then
value=$(( 2 * $window ))
# Sun rising
elif (( $now > $sunrise - $window )) && (( $now < $sunrise + $window )); then
value=$(( (2 * $window) - ($now - ($sunrise - $window)) ))
else
# Day
if (( $now <= $sunset - $window )); then
value=0
# Sun setting
elif (( $now > $sunset - $window )) && (( $now < $sunset + $window )); then
value=$(( $now - ($sunset - $window) ))
# Night - after day
else
value=$(( 2 * $window ))
fi
fi
# Scale dim value in [0:1]
value=$(echo "$value / (2.0 * $window)" | bc -l)
}
function set_display {
red=1.0
green=$(echo "1.0 - (0.1 * $value)" | bc -l)
blue=$(echo "1.0 - (0.2 * $value)" | bc -l)
brightness=$(echo "1.0 - (0.2 * $value)" | bc -l)
# Set nightlight for all displays
for d in ${displays[@]}; do
xrandr --output $d --gamma $red:$green:$blue --brightness $brightness
done
}
function log {
log_time=$(date '+%H:%M')
echo -ne "[\e[1m$log_time\e[0m] $1"
}
echo -ne "\n..:: \e[1mnightlight\e[0m ::..\n\n"
get_times
log "got sunrise and sunset values\n"
while true; do
set_dim_value
set_display
log "applied display values\r"
sleep 60
done