Skip to content

Instantly share code, notes, and snippets.

@Tuurlijk
Last active February 12, 2022 13:21
Show Gist options
  • Save Tuurlijk/99572d666b0db8fa4ac9c3997da2ecbd to your computer and use it in GitHub Desktop.
Save Tuurlijk/99572d666b0db8fa4ac9c3997da2ecbd to your computer and use it in GitHub Desktop.
Kitty theme switching based on the time of day

Kitty theme switching based on the time of day

I realy like the PHPStorm extension ChronoMorph which switches between light and dark themes depending on the time of day.

I wanted that for my terminal too. And since the kitty terminal can be remote controlled, we can implement this with two simple scripts.

One script fetches the sunrise and sunset times. It can run once a day.

The other one checks the current time against the sunrise and sunset times and determines if a light or dark mode should be used. It can run every five minutes or however often you like.

This requires kitty to have the remote control feature enabled.

Enjoy!

#!/bin/bash
location=NLXX5790
IFS=':'
read -r sunrise < $HOME/tmp/$location.sunrise
sunrise=${sunrise/':'/}
read -r sunset < $HOME/tmp/$location.sunset
sunset=${sunset/':'/}
now=`date +%H%M`
mode=dark
if [ $now -ge $sunrise ] && [ $now -lt $sunset ]; then
mode=light
fi
if [ $mode == "light" ]; then
kitty @ --to unix:@kitty-`pidof kitty` set-colors -a background=\#fdf6e3 foreground=\#586e75
kitty @ --to unix:@kitty-`pidof kitty` set-background-image ~/Pictures/Backgrounds/termBg.day.png
else
kitty @ --to unix:@kitty-`pidof kitty` set-colors -a background=\#000000 foreground=\#c0c0c0
kitty @ --to unix:@kitty-`pidof kitty` set-background-image ~/Pictures/Backgrounds/termBg.png
fi
#!/bin/bash
# First obtain a location code from: https://weather.codes/search/
# Insert your location. For example LOXX0001 is a location code for Bratislava, Slovakia
location="NLXX5790"
tmpfile=$HOME/tmp/$location.out
sunriseCache=$HOME/tmp/$location.sunrise
sunsetCache=$HOME/tmp/$location.sunset
# Obtain sunrise and sunset raw data from weather.com
wget -q "https://weather.com/weather/today/l/$location" -O "$tmpfile"
SUNR=$(grep SunriseSunset "$tmpfile" | grep -oE '((1[0-2]|0?[1-9]):([0-5][0-9]) ?([AaPp][Mm]))' | head -1)
SUNS=$(grep SunriseSunset "$tmpfile" | grep -oE '((1[0-2]|0?[1-9]):([0-5][0-9]) ?([AaPp][Mm]))' | tail -1)
sunrise=$(date --date="$SUNR" +%R)
sunset=$(date --date="$SUNS" +%R)
# Use $sunrise and $sunset variables to fit your needs. Example:
echo "$sunrise" > $sunriseCache
echo "$sunset" > $sunsetCache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment