summary refs log tree commit diff
path: root/bin/darkmode
blob: dd88100befcf380c9e7193d735c95499d8063885 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/sh

set -e
[ -n "$DEBUG" ] && set -x
# make rm inactive
export UNSAFE=1

THEME=${THEME:-Adwaita}
# THEME=${THEME:-adw-gtk3}

lockfile="/tmp/darkmode_lock"
[ -e "$lockfile" ] && exit 1
touch "$lockfile"
trap '{ rm $lockfile; }' EXIT

transition_marker="/tmp/darkmode_transition"
force_marker="/tmp/darkmode_force"
icon=$(iconez -n weather-sunset)
trap '{ notify-send -c error -i "$icon" -a darkmode "darkmode: error" "switching failed!"; }' ERR

gui__dark() {
	gsettings set org.gnome.desktop.interface gtk-theme ${THEME}-dark
	gsettings set org.gnome.desktop.interface color-scheme prefer-dark
}
gui__light() {
	gsettings set org.gnome.desktop.interface gtk-theme ${THEME}
	gsettings set org.gnome.desktop.interface color-scheme prefer-light
}

term__dark() {
	term-apply-colors <"$XDG_CONFIG_HOME"/darkmode/night
}
term__light() {
	term-apply-colors <"$XDG_CONFIG_HOME"/darkmode/day
}

nvim__dark() {
	killall -s SIGUSR1 nvim
}
nvim__light() {
	nvim__dark
}

while getopts fh opt; do
	case "$opt" in
	f) touch "$force_marker" ;;
	h | ?) exit 1 ;;
	esac
done
shift $((OPTIND - 1))

# if there's a force marker and it's not older than 24h -> exit
[ -f $force_marker ] && [ "$(stat -c '%Y' "$force_marker")" -gt "$(date --date=yesterday '+%s')" ] && exit 0

# debounce dawn/dusk phasing
case "${1}" in
dawn | dusk)
	[ ! -f $transition_marker ] && touch "$transition_marker"
	;;
day* | night)
	[ -f $transition_marker ] && rm -f "$transition_marker"
	;;
esac

# showtime...
case "${1}" in
off | dawn | day*)
	[ -f $transition_marker ] && exit 0
	notify-send -i "${icon}" -a darkmode "darkmode" "switching to <i>${1}</i>"
	gui__light
	# term__light
	nvim__light
	# pkill -fe -USR1 waybar-gammastep
	;;

on | dusk | night)
	[ -f $transition_marker ] && exit 0
	notify-send -i "${icon}" -a darkmode "darkmode" "switching to <i>${1}</i>"
	gui__dark
	# term__dark
	nvim__dark
	# pkill -fe -USR2 waybar-gammastep
	;;

*)
	printf "usage: %s [night|daytime]\n" "$(basename "$(readlink -f -- "$0")")"
	;;
esac