aboutsummaryrefslogtreecommitdiff
path: root/.local/share/bin/mode
blob: 6589b32a673cbc074a2abda526be7b9cdab707ba (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/sh
progname="$(basename "$0")"
stupid() {
	echo "run \`$progname --help\` for options" >&2
}
usage() {
	cat << EOF
$progname -- switch between dark / light theme

usage:
	$progname [-chr] action

options:
	-c, --no-cfggen    disable updating of configuration files       
	-h, --help         display this help text
	-r, --no-reload    disable reloading of applications after applying theme

actions:
	dark       switch to dark mode
	light      switch to light mode
	restore    switch to last set mode ($XDG_DATA_HOME/mode/active)
	reload     only reload applications, do not update configuration files
	help       same as --help option

examples:
	mode dark                   switch to dark mode and reload applications
	mode restore --no-reload    make sure all configuration files are up-to-date
	mode reload                 only reload applications

EOF
}

get_color() { xrdb -get "$1" | tr -d '\n'; }
hex_to_rgb_array() { pastel format rgb "$1" | cut -c4- | tr '()' '[]' ; }
mix_rgb() { pastel mix --colorspace=RGB --fraction="$3" "$1" "$2" | pastel format hex ; }

switch_xrdb() {
	xrdb -override "$XDG_CONFIG_HOME/X11/$mode"
	bg="$(get_color background)"
	fg="$(get_color foreground)"
	color0="$(get_color color0)"
	color1="$(get_color color1)"
	color2="$(get_color color2)"
	color3="$(get_color color3)"
	color4="$(get_color color4)"
	color5="$(get_color color5)"
	color6="$(get_color color6)"
	color7="$(get_color color7)"
	color8="$(get_color color8)"
	color9="$(get_color color9)"
	color10="$(get_color color10)"
	color11="$(get_color color11)"
	color12="$(get_color color12)"
	color13="$(get_color color13)"
	color14="$(get_color color14)"
	color15="$(get_color color15)"
	[ "$mode" == "light" ] && {
		accent="$color15"
		accent_text="$color0"
	}
	[ "$mode" == "dark" ] && {
		accent="$(mix_rgb $bg $color0 0.5)"
		accent_text="$fg"
	}
}

switch() {
	echo "updating configuration files to $mode mode..."
	
	# xrdb needs to complete first, as the rest of the color schemes are derived
	# from querying xrdb for colors
	switch_xrdb $mode

	for switch_function in "$XDG_CONFIG_HOME"/mode/switch.d/* ; do
		! [ -x "$switch_function" ] && continue
		. "$switch_function" &
	done

	wait $(jobs -rp)
}

reload() {
	echo "reloading programs..."

	for reload_function in "$XDG_CONFIG_HOME"/mode/reload.d/* ; do
		! [ -x "$reload_function" ] && continue
		. "$reload_function" &
	done

	wait $(jobs -rp)
}

run_cfggen=1
run_reload=1
mode=""

for arg in "$@" ; do
	case "$arg" in
		--) break ;;
		-h|--help|help) usage ; exit 0 ;;
		dark|light) mode="$arg" ;;
		restore) mode="$(cat "$XDG_DATA_HOME/mode/active")" ;;
		reload) run_reload=1 run_cfggen=0 ;;
		-r|--no-reload) run_reload=0 ;;
		-c|--no-cfggen) run_cfggen=0 ;;
		*)
			echo "error: unknown argument: $arg" >&2
			stupid && exit 1
			;;
	esac
done

if [ $run_cfggen -eq 1 ] && [ -z "$mode" ] ; then
	echo "error: no action provided" >&2
	stupid && exit 1
fi
case "$mode" in
	dark|light) break ;;
	*)
		echo "error: $mode is not a valid mode" >&2
		stupid && exit 1
		;;
esac

[ $run_cfggen -eq 1 ] && switch $mode
[ $run_reload -eq 1 ] && reload