summary refs log tree commit diff
path: root/bin/darkmode
diff options
context:
space:
mode:
Diffstat (limited to 'bin/darkmode')
-rwxr-xr-xbin/darkmode47
1 files changed, 24 insertions, 23 deletions
diff --git a/bin/darkmode b/bin/darkmode
index 31dda5c..8e0d3b2 100755
--- a/bin/darkmode
+++ b/bin/darkmode
@@ -3,8 +3,14 @@
 set -e
 [ -n "$DEBUG" ] && set -x
 
-icon=$(iconez -n weather-sunset)
+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" "switching failed!"; }' ERR
 
 gui__dark() {
@@ -17,43 +23,38 @@ gui__light() {
 	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-apply-colors <"$XDG_CONFIG_HOME"/darkmode/night
 }
 term__light() {
-	term__apply_theme <"$XDG_CONFIG_HOME"/darkmode/day
+	term-apply-colors <"$XDG_CONFIG_HOME"/darkmode/day
 }
 
+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
-marker="/tmp/darkmode_transition"
 case "${1}" in
 dawn | dusk)
-	[ ! -f $marker ] && touch "$marker"
+	[ ! -f $transition_marker ] && touch "$transition_marker"
 	;;
 day* | night)
-	[ -f $marker ] && rm -f "$marker"
+	[ -f $transition_marker ] && rm -f "$transition_marker"
 	;;
 esac
 
 # showtime...
 case "${1}" in
 dawn | day*)
-	[ -f $marker ] && exit 0
+	[ -f $transition_marker ] && exit 0
 	notify-send -i "${icon}" -a darkmode "darkmode" "switching to <i>${1}</i>"
 	gui__light
 	# term__light
@@ -61,7 +62,7 @@ dawn | day*)
 	;;
 
 dusk | night)
-	[ -f $marker ] && exit 0
+	[ -f $transition_marker ] && exit 0
 	notify-send -i "${icon}" -a darkmode "darkmode" "switching to <i>${1}</i>"
 	gui__dark
 	# term__dark