aboutsummaryrefslogtreecommitdiff
path: root/.local/share/bin/mode
blob: efa8ad49bfa03210a172bf2b2a8ac310a93bda51 (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
#!/bin/sh

MODE="$1"

get_color() {
	xrdb -query | grep $1 | head -n1 | cut -f2
}

hex_to_rgb_array() {
	printf "%d\n%d\n%d\n" "0x${1:1:2}" "0x${1:3:2}" "0x${1:5:2}" | jq -jc --slurp '.'
}

get_color_rgb_array() {
	hex_to_rgb_array "`get_color $1`"
}

mix_rgb() {
	R1="`printf "%d" "0x${1:1:2}"`"
	G1="`printf "%d" "0x${1:3:2}"`"
	B1="`printf "%d" "0x${1:5:2}"`"
	R2="`printf "%d" "0x${2:1:2}"`"
	G2="`printf "%d" "0x${2:3:2}"`"
	B2="`printf "%d" "0x${2:5:2}"`"
	
	printf "#"
	dc -e"$R1 $3 * $R2 1 $3 - * + 1 / p" | xargs printf "%02x"
	dc -e"$G1 $3 * $G2 1 $3 - * + 1 / p" | xargs printf "%02x"
	dc -e"$B1 $3 * $B2 1 $3 - * + 1 / p" | xargs printf "%02x"
}

switch_xrdb() {
	cat ~/.config/X11/base ~/.config/X11/$1 | xrdb
}

switch_dunst() {
	read -r -d '' conf << EOF
frame_color = "$(get_color color1)"

[urgency_low]
background = "$(get_color background)"
foreground = "$(get_color color1)"
timeout = 10

[urgency_normal]
background = "$(get_color background)"
foreground = "$(get_color foreground)"
timeout = 10

[urgency_critical]
background = "$(get_color color1)"
foreground = "$(get_color background)"
frame_color = "$(get_color color1)"
timeout = 0
EOF
	echo "$conf" | cat ~/.config/dunst/base - > ~/.config/dunst/dunstrc
}

switch_zathura() {
	read -r -d '' conf << EOF
set default-bg				"$(get_color background)"
set statusbar-bg			"$(get_color background)"
set inputbar-bg				"$(get_color background)"
set completion-highlight-fg	"$(get_color background)"
set completion-bg			"$(get_color background)"
set notification-error-fg	"$(get_color background)"
set notification-warning-fg	"$(get_color background)"
set notification-fg			"$(get_color background)"
set recolor-lightcolor		"$(get_color background)"

set default-fg			"$(get_color foreground)"
set inputbar-fg			"$(get_color foreground)"
set completion-fg		"$(get_color foreground)"
set statusbar-fg		"$(get_color foreground)"
set recolor-darkcolor	"$(get_color foreground)"

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

set highlight-active-color "$(get_color color13)"
set notification-bg        "$(get_color color13)"
EOF
	echo "$conf" > ~/.config/zathura/colors
}

switch_gtk() {
	if [[ $1 == "light" ]]; then
		sed "s/-Dark/-Light/" -i ~/.config/gtk-3.0/settings.ini
		sed "s/gtk-application-prefer-dark-theme.*/gtk-application-prefer-dark-theme=false/" -i ~/.config/gtk-3.0/settings.ini
	else
		sed "s/-Light/-Dark/" -i ~/.config/gtk-3.0/settings.ini
		sed "s/gtk-application-prefer-dark-theme.*/gtk-application-prefer-dark-theme=true/" -i ~/.config/gtk-3.0/settings.ini
	fi
	grep gtk-theme-name ~/.config/gtk-3.0/settings.ini | cut -f2 -d= | xargs xfconf-query -c xsettings -p /Net/ThemeName -s
}

switch_fcitx5() {
	accent="$(get_color color9)"
	read -r -d '' conf << EOF
[InputPanel]
NormalColor=$(get_color foreground)
HighlightCandidateColor=$(get_color background)
HighlightColor=$(get_color color13)
HighlightBackgroundColor=$(get_color background)

[InputPanel/Background]
Color=$(get_color background)
BorderColor=${accent}

[InputPanel/Highlight]
Color=${accent}

[Menu/Background]
Color=$(get_color background)

[Menu]
NormalColor=$(get_color foreground)

[Menu/Highlight]
Color=${accent}

[Menu/Separator]
Color=$(get_color color7)
EOF
	echo "$conf" | cat ~/.local/share/fcitx5/themes/loek/base.conf - > ~/.local/share/fcitx5/themes/loek/theme.conf
}

switch_startpage() {
	read -r -d '' conf << EOF
/* AUTOMATICALLY GENERATED, DO NOT EDIT */

:root {
	--background:  $(get_color background);
	--foreground:  $(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);
}
EOF
	echo "$conf" > ~/.config/startpage/colors.css
}

switch_chrome() {
	cd ~/.cache/wal/chromium
	rm -f 'Cached Theme.pak' 'bg.png' 'manifest.json'
	convert -size 100x100 "xc:`get_color background`" bg.png
  BG_ALT=$(mix_rgb $(get_color color8) $(get_color color0) '0.2')
  FG_ALT=$(mix_rgb $(get_color color15) $(get_color color8) '0.5')
	jq -nc \
		--argjson bg     "`get_color_rgb_array color0`" \
		--argjson bg_alt "`hex_to_rgb_array $BG_ALT`" \
		--argjson fg     "`get_color_rgb_array color7`" \
		--argjson fg_alt "`hex_to_rgb_array $FG_ALT`" \
		'{
			"description": "colorscheme generated by mode",
			"manifest_version": 2,
			"name": "mode theme",
			"theme": {
				"images": { "theme_frame": "bg.png" },
				"colors": {
					"frame": $fg,
					"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"
		}' > manifest.json
}

switch_polybar() {
	read -r -d '' conf << EOF
[color]
bg = \${xrdb:background:$(get_color background)}
fg = \${xrdb:foreground:$(get_color foreground)}
fg-alt = \${xrdb:background:$(get_color background)}
fg-half = \${xrdb:color8:$(get_color color8)}

alpha = #00000000

; vim:ft=dosini
EOF
	echo "$conf" > ~/.config/polybar/colors
}

switch_vim_lightline() {
	read -r -d '' conf << EOF
let s:bg = [ '$(get_color background)', 'NONE' ]
let s:fg = [ '$(get_color foreground)', 'NONE' ]
let s:mode = [ '$(get_color color0)', 'NONE' ]
let s:faint = [ '$(get_color color8)', 'NONE' ]
let s:none = [ 'NONE', 'NONE' ]
let s:test = [ '#ff00ff', 'NONE' ]
EOF
	echo "$conf" | cat - ~/.config/nvim/lightline-base.vim > ~/.config/nvim/lightline.vim
}

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

reload_dunst() {
	killall dunst
	dunst &> /dev/null & disown
}

reload_polybar() {
	polybar-msg cmd restart &> /dev/null
}

reload_fcitx5() {
	fcitx5 -rd &> /dev/null & disown
}

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
	xfsettingsd &> /dev/null &
	PID=$!
	sleep 1
	kill $PID
}

switch_cfgs() {
	echo "switching to $1 mode..."
	
	switch_xrdb $1
	switch_dunst $1
	switch_zathura $1
	switch_gtk $1
	switch_fcitx5 $1
	switch_chrome $1
	switch_startpage $1
	switch_polybar $1
	switch_vim_lightline $1

	mkdir -p ~/.local/share/mode
	echo $1 > ~/.local/share/mode/active
	rm -f ~/.local/share/mode/dark ~/.local/share/mode/light
	touch ~/.local/share/mode/$1
}

[[ $MODE == "restore" ]] && MODE=`cat ~/.local/share/mode/active`
[[ $MODE == "dark" || $MODE == "light" ]] && switch_cfgs $MODE

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

	reload_polybar
	reload_dunst
	reload_terms
	reload_fcitx5
	reload_gtk
}

reload_apps