-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathui.sh
220 lines (198 loc) · 5.79 KB
/
ui.sh
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
#!/usr/bin/env bash
# shellcheck source=./core.sh
source $(dirname ${BASH_SOURCE[0]})/core.sh
# shellcheck disable=SC2034
ui__doc__='
This module provides variables for printing colorful and unicode glyphs.
The Terminal features are detected automatically but can also be
enabled/disabled manually (see
[ui.enable_color](#function-ui_enable_color) and
[ui.enable_unicode_glyphs](#function-ui_enable_unicode_glyphs)).
'
# region colors
ui_color_enabled=false
ui_enable_color() {
local __doc__='
Enables color output explicitly.
>>> ui.disable_color
>>> ui.enable_color
>>> echo -E $ui_color_red red $ui_color_default
\033[0;31m red \033[0m
'
ui_color_enabled=true
ui_color_default='\033[0m'
ui_color_black='\033[0;30m'
ui_color_red='\033[0;31m'
ui_color_green='\033[0;32m'
ui_color_yellow='\033[0;33m'
ui_color_blue='\033[0;34m'
ui_color_magenta='\033[0;35m'
ui_color_cyan='\033[0;36m'
ui_color_lightgray='\033[0;37m'
ui_color_darkgray='\033[0;90m'
ui_color_lightred='\033[0;91m'
ui_color_lightgreen='\033[0;92m'
ui_color_lightyellow='\033[0;93m'
ui_color_lightblue='\033[0;94m'
ui_color_lightmagenta='\033[0;95m'
ui_color_lightcyan='\033[0;96m'
ui_color_white='\033[0;97m'
# flags
ui_color_bold='\033[1m'
ui_color_dim='\033[2m'
ui_color_underline='\033[4m'
ui_color_blink='\033[5m'
ui_color_invert='\033[7m'
ui_color_invisible='\033[8m'
ui_color_nobold='\033[21m'
ui_color_nodim='\033[22m'
ui_color_nounderline='\033[24m'
ui_color_noblink='\033[25m'
ui_color_noinvert='\033[27m'
ui_color_noinvisible='\033[28m'
}
# shellcheck disable=SC2034
ui_disable_color() {
local __doc__='
Disables color output explicitly.
>>> ui.enable_color
>>> ui.disable_color
>>> echo -E "$ui_color_red" red "$ui_color_default"
red
'
ui_color_enabled=false
ui_color_default=''
ui_color_black=''
ui_color_red=''
ui_color_green=''
ui_color_yellow=''
ui_color_blue=''
ui_color_magenta=''
ui_color_cyan=''
ui_color_lightgray=''
ui_color_darkgray=''
ui_color_lightred=''
ui_color_lightgreen=''
ui_color_lightyellow=''
ui_color_lightblue=''
ui_color_lightmagenta=''
ui_color_lightcyan=''
ui_color_white=''
# flags
ui_color_bold=''
ui_color_dim=''
ui_color_underline=''
ui_color_blink=''
ui_color_invert=''
ui_color_invisible=''
ui_color_nobold=''
ui_color_nodim=''
ui_color_nounderline=''
ui_color_noblink=''
ui_color_noinvert=''
ui_color_noinvisible=''
}
# endregion
# region glyphs
# NOTE: use 'xfd -fa <font-name>' to watch glyphs
ui_unicode_enabled=false
ui_enable_unicode_glyphs() {
local __doc__='
Enables unicode glyphs explicitly.
>>> ui.disable_unicode_glyphs
>>> ui.enable_unicode_glyphs
>>> echo -E "$ui_powerline_ok"
\u2714
'
ui_unicode_enabled=true
ui_powerline_pointingarrow='\u27a1'
ui_powerline_arrowleft='\ue0b2'
ui_powerline_arrowright='\ue0b0'
ui_powerline_arrowrightdown='\u2198'
ui_powerline_arrowdown='\u2b07'
ui_powerline_plusminus='\ue00b1'
ui_powerline_branch='\ue0a0'
ui_powerline_refersto='\u27a6'
ui_powerline_ok='\u2714'
ui_powerline_fail='\u2718'
ui_powerline_lightning='\u26a1'
ui_powerline_cog='\u2699'
ui_powerline_heart='\u2764'
# colorful
ui_powerline_star='\u2b50'
ui_powerline_saxophone='\u1f3b7'
ui_powerline_thumbsup='\u1f44d'
}
# shellcheck disable=SC2034
ui_disable_unicode_glyphs() {
local __doc__='
Disables unicode glyphs explicitly.
>>> ui.enable_unicode_glyphs
>>> ui.disable_unicode_glyphs
>>> echo -E "$ui_powerline_ok"
+
'
ui_unicode_enabled=false
ui_powerline_pointingarrow='~'
ui_powerline_arrowleft='<'
ui_powerline_arrowright='>'
ui_powerline_arrowrightdown='>'
ui_powerline_arrowdown='_'
ui_powerline_plusminus='+-'
ui_powerline_branch='|}'
ui_powerline_refersto='*'
ui_powerline_ok='+'
ui_powerline_fail='x'
ui_powerline_lightning='!'
ui_powerline_cog='{*}'
ui_powerline_heart='<3'
# colorful
ui_powerline_star='*'
ui_powerline_saxophone='(yeah)'
ui_powerline_thumbsup='(ok)'
}
# endregion
# region detect terminal capabilities
if [[ "${TERM}" == *"xterm"* ]]; then
ui_enable_color
else
ui_disable_color
fi
# TODO improve unicode detection
ui_glyph_available_in_font() {
#local font=$1
local current_font
current_font=$(xrdb -q| grep -i facename | cut -d: -f2)
local font_file_name
font_file_name=$(fc-match "$current_font" | cut -d: -f1)
#font_path=$(fc-list "$current_font" | grep "$font_file_name" | cut -d: -f1)
local font_file_extension="${font_file_name##*.}"
# Alternative or to be sure
#font_path=$(lsof -p $(ps -o ppid= -p $$) | grep fonts)
if [[ $font_file_extension == otf ]]; then
otfinfo /usr/share/fonts/OTF/Hack-Regular.otf -u | grep -i uni27a1
elif [[ $font_file_extension == ttf ]]; then
ttfdump -t cmap /usr/share/fonts/TTF/Hack-Regular.ttf 2>/dev/null| grep 'Char 0x27a1'
else
return 1
fi
return $?
}
# TODO this breaks dracut (segfault)
#(echo -e $'\u1F3B7' | grep -v F3B7) &> /dev/null
if core_is_defined NO_UNICODE; then
ui_disable_unicode_glyphs
else
ui_enable_unicode_glyphs
fi
# endregion
# region public interface
alias ui.enable_color='ui_enable_color'
alias ui.disable_color='ui_disable_color'
alias ui.enable_unicode_glyphs='ui_enable_unicode_glyphs'
alias ui.disable_unicode_glyphs='ui_disable_unicode_glyphs'
# endregion
# region vim modline
# vim: set tabstop=4 shiftwidth=4 expandtab:
# vim: foldmethod=marker foldmarker=region,endregion:
# endregion