-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmintheme
executable file
·455 lines (438 loc) · 12.4 KB
/
mintheme
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#! /bin/sh
#############################################################################
# mintty theme switcher
# features:
# list themes from config directories
# set a theme from config directories
# set a theme from a file
# query current colour configuration as theme
# possible future features:
# set a theme from a URL
# load theme from a URL into config directory
# store current theme into config directory
help() {
echo "Usage:"
echo " `basename $0` [-h|-l]"
echo " `basename $0` THEME"
echo " `basename $0` -f THEMEFILE"
echo " `basename $0` --save [THEME|THEMEFILE]"
echo " `basename $0` [-t|-p|-%] IMAGE"
echo " `basename $0` [-s|-S] [PATTERNS]"
echo " `basename $0` [-d] -q > THEMEFILE"
echo "List/set/query mintty themes in current mintty window."
echo "A theme is a colour scheme to configure the 8 ANSI colours, their "
echo "bright versions, and optionally the foreground/background/cursor colours."
echo
echo "Arguments:"
echo " THEME set theme from config resources"
echo " -f, --file THEMEFILE set theme from themefile"
echo " --save [THEME|THEMEFILE] save theme also in config file"
echo " -t, --texture FILE[,DIM] set background texture (dim 1..255)"
echo " -p, --picture FILE[,DIM] set background picture (dim 1..255)"
echo " -%, --picscale FILE[,DIM] like -p and scale window"
echo " -q, --query query current colour configuration as theme"
echo " -d, --decimal decimal query output"
echo " -l, --list list available themes"
echo " -s, --show [PATTERN...] show (visualize) themes [matching PATTERNs]"
echo " -S, --Show [PATTERN...] show themes double-lined [matching PATTERNs]"
echo " -h, --help show this"
echo
echo "The following directories are considered for locating configured themes:"
echo ' ~/.mintty'
echo ' ~/.config/mintty'
echo ' $APPDATA/mintty'
echo ' /usr/share/mintty'
echo "Note that the command-line option --configdir cannot be considered."
}
terminaltype () {
case "$BASH" in
?*) # try simplified query as suggested by Brian Inglis:
# https://github.com/mintty/mintty/issues/776#issuecomment-475761566
read -s -dc -p $'\E[>c' da < /dev/tty
da=${da##$'\E'[>}
set - ${da//;/ }
;;
*) # other shells do not have flexible read and string operations
# so it's a bit more tricky...
test -t 0 && stty=`stty -g`
test -t 0 && stty raw -echo min 0 time 5
echo -n "[>c" > /dev/tty
da=`dd < /dev/tty 2> /dev/null`
test -t 0 && stty "$stty"
da=${da#??}
type="${da%%;*}"
rest="${da#*;}"
vers="${rest%%;*}"
rest="${rest#*;}"
rest="${rest%%c}"
set - $type $vers $rest
;;
esac
echo $1
}
showtheme() {
theme="$1"
export theme
sed \
-e 's/^\(ForegroundColour\)[ ]*=/\1=/' \
-e 's/^\(BackgroundColour\)[ ]*=/\1=/' \
-e 's/^\(CursorColour\)[ ]*=/\1=/' \
-e 's/^\(Black\)[ ]*=/\1=/' \
-e 's/^\(Red\)[ ]*=/\1=/' \
-e 's/^\(Green\)[ ]*=/\1=/' \
-e 's/^\(Yellow\)[ ]*=/\1=/' \
-e 's/^\(Blue\)[ ]*=/\1=/' \
-e 's/^\(Magenta\)[ ]*=/\1=/' \
-e 's/^\(Cyan\)[ ]*=/\1=/' \
-e 's/^\(White\)[ ]*=/\1=/' \
-e 's/^\(BoldBlack\)[ ]*=/\1=/' \
-e 's/^\(BoldRed\)[ ]*=/\1=/' \
-e 's/^\(BoldGreen\)[ ]*=/\1=/' \
-e 's/^\(BoldYellow\)[ ]*=/\1=/' \
-e 's/^\(BoldBlue\)[ ]*=/\1=/' \
-e 's/^\(BoldMagenta\)[ ]*=/\1=/' \
-e 's/^\(BoldCyan\)[ ]*=/\1=/' \
-e 's/^\(BoldWhite\)[ ]*=/\1=/' \
-e 't ok' -e d -e ': ok' -e 's/[ ]//g' \
"$1" |
(
cat <<\/EOS
xrgb() {
sed -e "s, , ,g" /usr/share/X11/rgb.txt |
sed -e "s/ *\([0-9][0-9]*\) *\([0-9][0-9]*\) *\([0-9][0-9]*\) *$1 *$/\1;\2;\3/" -e t -e d
}
ForegroundColour=39
BackgroundColour=49
CursorColour=49
Black=40
Red=41
Green=42
Yellow=43
Blue=44
Magenta=45
Cyan=46
White=47
BoldBlack=100
BoldRed=101
BoldGreen=102
BoldYellow=103
BoldBlue=104
BoldMagenta=105
BoldCyan=106
BoldWhite=107
/EOS
# transform colour specs:
# d,d,d
# d;d;d
# #RRGGBB
# 0xRR
# rgb:RR/GG/BB
# 0xRR
# rgb:RRRR/GGGG/BBBB
# 0xRR
# cmy:C.C/M.M/Y.Y
# r = (1 - c) * 255;
# g = (1 - m) * 255;
# b = (1 - y) * 255;
# cmyk:C.C/M.M/Y.Y/K.K
# r = (1 - c) * (1 - k) * 255;
# g = (1 - m) * (1 - k) * 255;
# b = (1 - y) * (1 - k) * 255;
# X11 color names
sed -e 's%=\([0-9]*\),\([0-9]*\),\([0-9]*\)%="\1;\2;\3"%' \
-e 's%=#\([0-9A-Fa-f][0-9A-Fa-f]\)\([0-9A-Fa-f][0-9A-Fa-f]\)\([0-9A-Fa-f][0-9A-Fa-f]\)%="$((0x\1));$((0x\2));$((0x\3))"%' \
-e 's%=rgb:\([0-9A-Fa-f][0-9A-Fa-f]\)/\([0-9A-Fa-f][0-9A-Fa-f]\)/\([0-9A-Fa-f][0-9A-Fa-f]\)%="$((0x\1));$((0x\2));$((0x\3))"%' \
-e 's%=rgb:\([0-9A-Fa-f][0-9A-Fa-f]\)[0-9A-Fa-f][0-9A-Fa-f]/\([0-9A-Fa-f][0-9A-Fa-f]\)[0-9A-Fa-f][0-9A-Fa-f]/\([0-9A-Fa-f][0-9A-Fa-f]\)[0-9A-Fa-f][0-9A-Fa-f]%="$((0x\1));$((0x\2));$((0x\3))"%' \
-e '/=cmy/ s%\([01]\)\.\([0-9]*\)%\1.\2000%g' \
-e '/=cmy/ s%\([01]\)\.\([0-9][0-9][0-9]\)[0-9]*%\1\2%g' \
-e '/=cmy:/ s%cmy:\([0-9/]*\)%cmyk:\1/0000%' \
-e 's%=cmyk:\([01][0-9][0-9][0-9]\)/\([01][0-9][0-9][0-9]\)/\([01][0-9][0-9][0-9]\)/\([01][0-9][0-9][0-9]\)%=cmyk:"$(( (1000 - \1) * (1000 - \4) * 255 / 1000000 ));$(( (1000 - \2) * (1000 - \4) * 255 / 1000000 ));$(( (1000 - \3) * (1000 - \4) * 255 / 1000000 ))"%' \
-e '/=cmyk:/ s%\([^0-9]\)0*\([0-9]\)%\1\2%g' -e 's%=cmyk:%=%' \
-e 's%=\([a-zA-Z][a-zA-Z ]*\)%=`xrgb "\1"`%' \
-e 't ok' -e d -e ': ok' -e 's%="%="48;2;%'
if $show2
then cat <<\/EOS
echo -en "\e[37;${Black}mblk"
echo -en "\e[30;${Red}mred"
echo -en "\e[30;${Green}mgrn"
echo -en "\e[30;${Yellow}mylw"
echo -en "\e[30;${Blue}mblu"
echo -en "\e[30;${Magenta}mmag"
echo -en "\e[30;${Cyan}mcyn"
echo -en "\e[30;${White}mwht"
echo -en "\e[39;${CursorColour}m "
echo -e "\e[m${theme}"
echo -en "\e[37;${BoldBlack}mBLK"
echo -en "\e[30;${BoldRed}mRED"
echo -en "\e[30;${BoldGreen}mGRN"
echo -en "\e[30;${BoldYellow}mYLW"
echo -en "\e[30;${BoldBlue}mBLU"
echo -en "\e[30;${BoldMagenta}mMAG"
echo -en "\e[30;${BoldCyan}mCYN"
echo -en "\e[30;${BoldWhite}mWHT"
echo -en "\e[39;${CursorColour}m "
echo -e "\e[${ForegroundColour};${BackgroundColour}m${theme}\e[m"
/EOS
else cat <<\/EOS
echo -en "\e[${ForegroundColour};${BackgroundColour}mfgbg"
echo -en "\e[39;${CursorColour}mC"
echo -en "\e[37;${Black}mbk"
echo -en "\e[30;${Red}mrd"
echo -en "\e[30;${Green}mgn"
echo -en "\e[30;${Yellow}myl"
echo -en "\e[30;${Blue}mbl"
echo -en "\e[30;${Magenta}mmg"
echo -en "\e[30;${Cyan}mcy"
echo -en "\e[30;${White}mwh"
echo -en "\e[37;${BoldBlack}mBK"
echo -en "\e[30;${BoldRed}mRD"
echo -en "\e[30;${BoldGreen}mGN"
echo -en "\e[30;${BoldYellow}mYL"
echo -en "\e[30;${BoldBlue}mBL"
echo -en "\e[30;${BoldMagenta}mMG"
echo -en "\e[30;${BoldCyan}mCY"
echo -en "\e[30;${BoldWhite}mWH"
echo -e "\e[m $theme"
/EOS
fi
) | sh
}
showthemes() {
for confdir in ~/.mintty ~/.config/mintty "$APPDATA"/mintty /usr/share/mintty
do if [ -d "$confdir/themes" -a -x "$confdir/themes" -a -r "$confdir/themes" ]
then echo "[4;53;7mthemes in '$confdir':[m"
(
cd "$confdir/themes"
for th in *[!~]
do for pat in "${@-}"
do case "$th" in
*$pat*) showtheme "$th";;
esac
done
done
)
fi
done
}
settheme() {
case `terminaltype` in
83) # screen
pre='P'
post='\\'
;;
*) pre=
post=
;;
esac
sed \
-e 's/^\(ForegroundColour\)[ ]*=/10;/' \
-e 's/^\(BackgroundColour\)[ ]*=/11;/' \
-e 's/^\(CursorColour\)[ ]*=/12;/' \
-e 's/^\(Black\)[ ]*=/4;0;/' \
-e 's/^\(Red\)[ ]*=/4;1;/' \
-e 's/^\(Green\)[ ]*=/4;2;/' \
-e 's/^\(Yellow\)[ ]*=/4;3;/' \
-e 's/^\(Blue\)[ ]*=/4;4;/' \
-e 's/^\(Magenta\)[ ]*=/4;5;/' \
-e 's/^\(Cyan\)[ ]*=/4;6;/' \
-e 's/^\(White\)[ ]*=/4;7;/' \
-e 's/^\(BoldBlack\)[ ]*=/4;8;/' \
-e 's/^\(BoldRed\)[ ]*=/4;9;/' \
-e 's/^\(BoldGreen\)[ ]*=/4;10;/' \
-e 's/^\(BoldYellow\)[ ]*=/4;11;/' \
-e 's/^\(BoldBlue\)[ ]*=/4;12;/' \
-e 's/^\(BoldMagenta\)[ ]*=/4;13;/' \
-e 's/^\(BoldCyan\)[ ]*=/4;14;/' \
-e 's/^\(BoldWhite\)[ ]*=/4;15;/' \
-e 't ok' -e d -e ': ok' -e 's/[ ]//g' \
-e "s/^/$pre]/" -e "s/$/$post/" "$1" |
tr -d '\012'
#]4;A;colour set ANSI colour A=0..15
#]10;colour set foreground colour
#]11;colour set background colour
#]12;colour set cursor colour
# unused:
#]104;A reset ANSI colour A=0..15
#]104 reset colour palette
#]110 reset foreground colour
#]111 reset background colour
#]112 reset cursor colour
}
query() {
len=$(( $1 + 3 ))
echo -n "]$2;?" > /dev/tty
read -s -n$len -t 2 esc < /dev/tty # read ESC ] prefix and colour index
read -s -n18 colour < /dev/tty # read rgb:colour spec
read -s -n1 -t 2 esc < /dev/tty # read ^G suffix
if $decimal
then
eval $( echo $colour | sed -e "s@rgb:\(..\)../\(..\)../\(..\)..@printf '$3=%d,%d,%d' 0x\1 0x\2 0x\3@" -e t -e "s,^,printf $3=," )
echo
else
# 4-digit hex
echo $3=$colour
# 2-digit hex
#echo $3=$colour | sed -e "s,rgb:\(..\)../\(..\)../\(..\)..,rgb:\1/\2/\3,"
fi
}
conftheme() {
if [ `uname` = "Linux" ] && type wslpath 2> /dev/null 1>&2
then
themefile=`wslpath -ma "$1"`
elif type cygpath 2> /dev/null 1>&2
then
themefile=`cygpath -wa "$1"`
else
echo "cannot save to config file in this system environment"
exit
fi
if [ `uname` = "Linux" ] && type wslpath 1>&2
then
ls "$APPDATA/mintty/config"
elif type cygpath 1>&2
then
ls ~/.minttyrc ~/.config/mintty/config "$APPDATA/mintty/config" /etc/minttyrc
fi 2> /dev/null |
while read conffile
do if [ -w "$conffile" ]
then
newconf=/tmp/minttyrc.$$
# create new config file with modified entry
sed -n -e "/^ThemeFile[ ]*=/ q" -e p "$conffile" > "$newconf"
echo "ThemeFile=$themefile" >> "$newconf"
sed -e "1,/^ThemeFile[ ]*=/ d" "$conffile" >> "$newconf"
# copy new config file back
/bin/cp "$newconf" "$conffile"
/bin/rm "$newconf"
# feedback
echo "saved theme '$themefile'"
echo "in config file '$conffile'"
return
fi
done
}
decimal=false
show2=false
case "$1" in
-d|--decimal)
decimal=true
shift;;
esac
case `uname` in
Linux) if [ -n "$APPDATA" ]
then APPDATA=`wslpath "$APPDATA"`
else echo APPDATA not set >&2
exit
fi;;
esac
case "$1" in
-h|--help|'')
help
;;
-l|--list)
for confdir in ~/.mintty ~/.config/mintty "$APPDATA"/mintty /usr/share/mintty
do if [ -d "$confdir/themes" -a -x "$confdir/themes" -a -r "$confdir/themes" ]
then echo "[4;53;7mthemes in '$confdir':[m"
(cd "$confdir/themes"; ls *[!~])
fi
done;;
-s|--show)
shift
showthemes "$@";;
-S|--Show|--SHOW)
shift
show2=true
export show2
showthemes "$@";;
-f|--file)
if [ -z "$2" ]
then help
elif [ -r "$2" ]
then settheme "$2"
else echo cannot read theme file
fi;;
--save)
if [ -z "$2" ]
then help
elif [ -r "$2" ]
then
settheme "$2"
conftheme "$2"
else
for confdir in ~/.mintty ~/.config/mintty "$APPDATA"/mintty /usr/share/mintty
do if [ -r "$confdir/themes/$2" ]
then
echo setting theme from config dir "$confdir"
settheme "$confdir/themes/$2"
conftheme "$confdir/themes/$2"
exit
fi
done
echo theme not found
fi;;
-p|--picture)
if [ -z "$2" ]
then help
elif [ -r "${2/,*/}" ]
then echo "]11;_$2"
else echo cannot read picture file
fi;;
-%|--picscale)
if [ -z "$2" ]
then help
elif [ -r "${2/,*/}" ]
then echo "]11;%$2"
else echo cannot read picture file
fi;;
-t|--texture)
if [ -z "$2" ]
then help
elif [ -r "${2/,*/}" ]
then echo "]11;*$2"
else echo cannot read texture file
fi;;
-q|--query)
# echo "]10;?]11;?]12;?"
test -t 0 && stty=`stty -g`
test -t 0 && stty -echo min 0 time 5 # raw
query 2 "10" ForegroundColour
query 2 "11" BackgroundColour
query 2 "12" CursorColour
query 3 "4;0" Black
query 3 "4;1" Red
query 3 "4;2" Green
query 3 "4;3" Yellow
query 3 "4;4" Blue
query 3 "4;5" Magenta
query 3 "4;6" Cyan
query 3 "4;7" White
query 3 "4;8" BoldBlack
query 3 "4;9" BoldRed
query 4 "4;10" BoldGreen
query 4 "4;11" BoldYellow
query 4 "4;12" BoldBlue
query 4 "4;13" BoldMagenta
query 4 "4;14" BoldCyan
query 4 "4;15" BoldWhite
read -s -n1 -t 1 esc < /dev/tty # swallow final bogus '\'
test -t 0 && stty "$stty"
;;
-*) echo unknown argument;;
*)
if [ -r "$1" ]
then echo setting theme from config dir "$confdir"
settheme "$1"
exit
fi
for confdir in ~/.mintty ~/.config/mintty "$APPDATA"/mintty /usr/share/mintty
do if [ -r "$confdir/themes/$1" ]
then
echo setting theme from config dir "$confdir"
settheme "$confdir/themes/$1"
exit
fi
done
echo theme not found;;
esac
#############################################################################
# end