From 2107c520e536f2b35c7e516c016a54860b314c76 Mon Sep 17 00:00:00 2001 From: Robert Günzler Date: Wed, 17 Aug 2022 15:30:17 +0200 Subject: bin/darkmode: support setting terminal colors via escape seq --- bin/darkmode | 71 ++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 16 deletions(-) diff --git a/bin/darkmode b/bin/darkmode index 55dc32f..31dda5c 100755 --- a/bin/darkmode +++ b/bin/darkmode @@ -5,28 +5,67 @@ set -e icon=$(iconez -n weather-sunset) -# case "${1}" in -# dawn | dusk) -# touch "/tmp/darkmode.${1}" -# ;; -# day* | night) -# rm /tmp/darkmode* || true -# ;; -# esac +trap '{ notify-send -c error -i "$icon" -a darkmode "darkmode" "switching failed!"; }' ERR +gui__dark() { + gsettings set org.gnome.desktop.interface gtk-theme Adwaita + # gsettings set org.gnome.desktop.interface gtk-theme Adwaita-dark + gsettings set org.gnome.desktop.interface color-scheme prefer-dark +} +gui__light() { + gsettings set org.gnome.desktop.interface gtk-theme Adwaita + gsettings set org.gnome.desktop.interface color-scheme prefer-light +} + +term__apply_theme() { + # stolen from https://github.com/lemnos/theme.sh/blob/master/bin/theme.sh + awk ' + BEGIN { + fgesc="\033]10;#%s\007" + bgesc="\033]11;#%s\007" + curesc="\033]12;#%s\007" + colesc="\033]4;%d;#%s\007" + } + /^foreground:/ { printf fgesc, substr($2, 2) > "/dev/tty" } + /^background:/ { printf bgesc, substr($2, 2) > "/dev/tty" } + /^cursor:/ { printf curesc, substr($2, 2) > "/dev/tty" } + /^[0-9]+:/ { printf colesc, $1, substr($2, 2) > "/dev/tty" } +' +} +term__dark() { + term__apply_theme <"$XDG_CONFIG_HOME"/darkmode/night +} +term__light() { + term__apply_theme <"$XDG_CONFIG_HOME"/darkmode/day +} + +# debounce dawn/dusk phasing +marker="/tmp/darkmode_transition" +case "${1}" in +dawn | dusk) + [ ! -f $marker ] && touch "$marker" + ;; +day* | night) + [ -f $marker ] && rm -f "$marker" + ;; +esac + +# showtime... case "${1}" in dawn | day*) - # [ -f "/tmp/darkmode.${1}" ] && exit 0 - notify-send -i "${icon}" -a wlsunset "wlsunset" "switching to day ${1}" - gsettings set org.gnome.desktop.interface gtk-theme Adwaita - pkill -fe -USR1 waybar-gammastep + [ -f $marker ] && exit 0 + notify-send -i "${icon}" -a darkmode "darkmode" "switching to ${1}" + gui__light + # term__light + # pkill -fe -USR1 waybar-gammastep ;; dusk | night) - # [ -f "/tmp/darkmode.${1}" ] && exit 0 - notify-send -i "${icon}" -a wlsunset "wlsunset" "switching to ${1}" - gsettings set org.gnome.desktop.interface gtk-theme Adwaita-dark - pkill -fe -USR2 waybar-gammastep + [ -f $marker ] && exit 0 + notify-send -i "${icon}" -a darkmode "darkmode" "switching to ${1}" + gui__dark + # term__dark + # pkill -fe -USR2 waybar-gammastep ;; *) -- cgit 1.4.1