aboutsummaryrefslogtreecommitdiff
path: root/.local/share/bin/mode
blob: 98c536f9dfb1a286a8de2324b17f6793db630e5e (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/bin/sh
MODE="$1"

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/$1"
	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)"
	[ "$1" == "light" ] && {
		accent="$color15"
		accent_text="$color0"
	}
	[ "$1" == "dark" ] && {
		accent="$(mix_rgb $bg $color0 0.5)"
		accent_text="$fg"
	}
}

switch_dunst() {
	cat "$XDG_CONFIG_HOME/dunst/base" - << EOF > "$XDG_CONFIG_HOME/dunst/dunstrc"
frame_color = "$color0"

[urgency_low]
background = "$bg"
foreground = "$fg"
timeout = 10

[urgency_normal]
background = "$bg"
foreground = "$fg"
timeout = 10

[urgency_critical]
background = "$color1"
foreground = "$bg"
frame_color = "$color1"
timeout = 0
EOF
}

switch_zathura() {
	cat << EOF > "$XDG_CONFIG_HOME/zathura/colors"
set default-bg				"$bg"
set statusbar-bg			"$bg"
set inputbar-bg				"$bg"
set completion-highlight-fg	"$bg"
set completion-bg			"$bg"
set notification-error-fg	"$bg"
set notification-warning-fg	"$bg"
set notification-fg			"$bg"
set recolor-lightcolor		"$bg"

set default-fg			"$fg"
set inputbar-fg			"$fg"
set completion-fg		"$fg"
set statusbar-fg		"$fg"
set recolor-darkcolor	"$fg"

set notification-error-bg   "$color9"
set notification-warning-bg "$color9"
set completion-highlight-bg "$color9"
set highlight-color         "$color9"

set highlight-active-color "$color13"
set notification-bg        "$color13"
EOF
}

switch_gtk() {
	GTK_3_SETTINGS_INI="$XDG_CONFIG_HOME/gtk-3.0/settings.ini"
	GTK_4_SETTINGS_ENV="$XDG_CONFIG_HOME/gtk-4.0/env"
	if [ $1 = "light" ]; then
		sed 's/^\(export\s\+GTK_THEME=.\+\)-Dark\(.*\)$/\1-Light\2/g' -i "$GTK_4_SETTINGS_ENV"
		sed "s/-Dark/-Light/" -i "$GTK_3_SETTINGS_INI"
		sed "s/gtk-application-prefer-dark-theme.*/gtk-application-prefer-dark-theme=false/" -i "$GTK_3_SETTINGS_INI"
	else
		sed 's/^\(export\s\+GTK_THEME=.\+\)-Light\(.*\)$/\1-Dark\2/g' -i "$GTK_4_SETTINGS_ENV"
		sed "s/-Light/-Dark/" -i "$GTK_3_SETTINGS_INI"
		sed "s/gtk-application-prefer-dark-theme.*/gtk-application-prefer-dark-theme=true/" -i "$GTK_3_SETTINGS_INI"
	fi
	grep gtk-theme-name "$GTK_3_SETTINGS_INI" | cut -f2 -d= | xargs xfconf-query -c xsettings -p /Net/ThemeName -s
}

switch_fcitx5() {
	cat "$XDG_DATA_HOME/fcitx5/themes/loek/base.conf" - << EOF > "$XDG_DATA_HOME/fcitx5/themes/loek/theme.conf"
[InputPanel]
NormalColor=$fg
HighlightCandidateColor=$accent_text
HighlightColor=$color13
HighlightBackgroundColor=$bg

[InputPanel/Background]
Color=$bg
BorderColor=$accent

[InputPanel/Highlight]
Color=$accent

[Menu/Background]
Color=$bg

[Menu]
NormalColor=$fg

[Menu/Highlight]
Color=$accent

[Menu/Separator]
Color=$color7
EOF
}

switch_chrome() (
	cd ~/.cache/wal/chromium
	rm -f 'Cached Theme.pak' 'bg.png' 'manifest.json'
	convert -size 100x100 "xc:$bg" bg.png
  bg_alt=$(mix_rgb $color7 $bg 0.20)
  fg_alt=$(mix_rgb $color15 $fg 0.60)
	bg="$(hex_to_rgb_array "$bg")"
	fg="$(hex_to_rgb_array "$fg")"
	bg_alt="$(hex_to_rgb_array "$bg_alt")"
	fg_alt="$(hex_to_rgb_array "$fg_alt")"
	cat << EOF > manifest.json
{
	"description": "colorscheme generated by mode",
	"manifest_version": 2,
	"name": "mode theme",
	"theme": {
		"images": { "theme_frame": "bg.png" },
		"colors": {
			"frame": $bg,
			"button_background": $fg,
			"ntp_background": $bg,
			"ntp_text": $bg,
			"toolbar": $bg_alt,
			"toolbar_button_icon": $fg,
			"tab_background_text": $fg_alt,
			"tab_background_text_inactive": $fg_alt,
			"tab_background_text_incognito": $fg_alt,
			"tab_background_text_incognito_inactive": $fg_alt,
			"bookmark_text": $fg,
			"tab_text": $fg
		},
		"tints": {
			"buttons": [ -1, -1, -1 ],
			"frame_inactive": [ -1, -1, -1 ],
			"frame_incognito": [ -1, -1, -1 ],
			"frame_incognito_inactive": [ -1, -1, -1 ]
		}
	},
	"version": "2"
}
EOF
)

switch_polybar() {
	cat << EOF > "$XDG_CONFIG_HOME/polybar/colors.ini"
[color]
bg = \${xrdb:background:$bg}
fg = \${xrdb:foreground:$fg}
fg-alt = \${xrdb:background:$bg}
fg-half = \${xrdb:color8:$color8}

alpha = #00000000

; vim:ft=dosini
EOF
}

switch_vim_lightline() {
	cat - "$XDG_CONFIG_HOME/nvim/lightline-base.vim" << EOF > "$XDG_CONFIG_HOME/nvim/lightline.vim"
let s:bg = [ '$bg', 'NONE' ]
let s:fg = [ '$fg', 'NONE' ]
let s:mode = [ '$color0', 'NONE' ]
let s:faint = [ '$color8', 'NONE' ]
let s:none = [ 'NONE', 'NONE' ]
let s:test = [ '#ff00ff', 'NONE' ]
EOF
}

switch_vim_terminal() {
	cat << EOF > "$XDG_CONFIG_HOME/nvim/termcolors.vim"
let g:terminal_color_0 = '$color0'
let g:terminal_color_1 = '$color1'
let g:terminal_color_2 = '$color2'
let g:terminal_color_3 = '$color3'
let g:terminal_color_4 = '$color4'
let g:terminal_color_5 = '$color5'
let g:terminal_color_6 = '$color6'
let g:terminal_color_7 = '$color7'
let g:terminal_color_8 = '$color8'
let g:terminal_color_9 = '$color9'
let g:terminal_color_10 = '$color10'
let g:terminal_color_11 = '$color11'
let g:terminal_color_12 = '$color12'
let g:terminal_color_13 = '$color13'
let g:terminal_color_14 = '$color14'
let g:terminal_color_15 = '$color15'
EOF
}

switch_discord() {
	cat << EOF > "$XDG_CONFIG_HOME/BetterDiscord/themes/mode.theme.css"
/**
 * @name mode
 * @author mode
 * @version 0
*/

/* AUTOMATICALLY GENERATED, DO NOT EDIT */

.theme-dark, .theme-light {
	--background-primary: $bg;
	--background-secondary: $bg;
	--background-secondary-alt: $bg;
	--text-primary: $fg;
	--text-secondary: $color15;
	--accent: $color4;
	--accent-alt: $color12;
	--error: $color1;
	--error-alt: $color9;
}

.theme-dark {
	--background-tertiary: $(mix_rgb $bg '#000000' 0.7);
}

.theme-light {
	--background-tertiary: $bg;
}
EOF
}

reload_terms() {
	escape_msgs="$(cat << EOF | tr -d '\n'
\033]11;$bg\007
\033]10;$fg\007
\033]12;$fg\007
\033]14;$bg\007
\033]13;$fg\007
\033]17;$color8\007
\033]708;$bg\007
\033]4;0;$color0\007
\033]4;1;$color1\007
\033]4;2;$color2\007
\033]4;3;$color3\007
\033]4;4;$color4\007
\033]4;5;$color5\007
\033]4;6;$color6\007
\033]4;7;$color7\007
\033]4;8;$color8\007
\033]4;9;$color9\007
\033]4;10;$color10\007
\033]4;11;$color11\007
\033]4;12;$color12\007
\033]4;13;$color13\007
\033]4;14;$color14\007
\033]4;15;$color15\007
EOF
)"
	find /dev/pts -exec sh -c "printf \"$escape_msgs\" > {}" \; 2> /dev/null
}

switch_rofi() {
	cat << EOF > "$XDG_CONFIG_HOME/rofi/colors"
export background="$bg"
export foreground="$fg"
export accent="$accent"
export accent_text="$accent_text"
EOF
}

reload_dunst() {
	killall dunst
	fork dunst
}

reload_polybar() {
	fork polybar-msg cmd restart
}

reload_fcitx5() {
	fork fcitx5 -rd
}

reload_gtk() {
	# this is max jank but xfsettingsd does other things besides live gtk theme
	# reloading, and i don't like having it running constantly because it fucks
	# up fcitx5 and xbindkeys
	(
		fork xfsettingsd
		sleep 1
		killall xfsettingsd
	)
}

reload_nvim() {
	nvr --serverlist | while read -r nvim_socket ; do
		fork nvr --nostart --servername "$nvim_socket" -c 'source $XDG_CONFIG_HOME/nvim/mode.vim'
	done
}

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

	switch_dunst $1 &
	switch_zathura $1 &
	switch_gtk $1 &
	switch_fcitx5 $1 &
	switch_chrome $1 &
	switch_polybar $1 &
	switch_vim_lightline $1 &
	switch_discord $1 &
	switch_vim_terminal $1 &
	switch_rofi $1 &

	mkdir -p "$XDG_DATA_HOME/mode"
	echo $1 > "$XDG_DATA_HOME/mode/active"
	rm -f "$XDG_DATA_HOME/mode/dark" "$XDG_DATA_HOME/mode/light"
	touch "$XDG_DATA_HOME/mode/$1"

	wait $(jobs -rp)
}

[ $MODE = "restore" ] && MODE="$(cat "$XDG_DATA_HOME/mode/active")"
[ $MODE = "dark" -o $MODE = "light" ] && switch_cfgs $MODE

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

	reload_polybar &
	reload_dunst &
	reload_terms &
	reload_fcitx5 &
	reload_gtk &
	reload_nvim &
	rwall &

	wait $(jobs -rp)
}

reload_apps