-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_text
269 lines (226 loc) · 4.7 KB
/
util_text
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
#! /bin/bash
RESET_CODE=0
BOLD=1
DIM=2
ITALIC=3
UNDERLINED=4
BLINK=5
REVERSE=7
HIDDEN=8
STRIKE=9
DEFAULT=39
BLACK=30
RED=31
GREEN=32
YELLOW=33
BLUE=34
MAGENTA=35
CYAN=36
LIGHT_GREY=37
DARK_GREY=90
LIGHT_RED=91
LIGHT_GREEN=92
LIGHT_YELLOW=93
LIGHT_BLUE=94
LIGHT_MAGENTA=95
LIGHT_CYAN=96
WHITE=97
BG_DEFAULT=49
BG_BLACK=40
BG_RED=41
BG_GREEN=42
BG_YELLOW=43
BG_BLUE=44
BG_MAGENTA=45
BG_CYAN=46
BG_LIGHT_GREY=47
BG_DARK_GREY=100
BG_LIGHT_RED=101
BG_LIGHT_GREEN=102
BG_LIGHT_YELLOW=103
BG_LIGHT_BLUE=104
BG_LIGHT_MAGENTA=105
BG_LIGHT_CYAN=106
BG_WHITE=107
regex_anychar="\(.*\)"
function strformats(){
strformat "0 RESET_CODE" RESET_CODE
strformat "1 BOLD" BOLD
strformat "2 DIM" DIM
strformat "3 ITALIC" ITALIC
strformat "4 UNDERLINED" UNDERLINED
strformat "5 BLINK" BLINK
strformat "7 REVERSE" REVERSE
strformat "8 HIDDEN" DARK_GREY
strformat "9 STRIKE" STRIKE
strformat ""
strformat "39 DEFAULT" DEFAULT
strformat "30 BLACK" DARK_GREY
strformat "31 RED" RED
strformat "32 GREEN" GREEN
strformat "33 YELLOW" YELLOW
strformat "34 BLUE" BLUE
strformat "35 MAGENTA" MAGENTA
strformat "36 CYAN" CYAN
strformat "37 LIGHT_GREY" LIGHT_GREY
strformat "90 DARK_GREY" DARK_GREY
strformat "91 LIGHT_RED" LIGHT_RED
strformat "92 LIGHT_GREEN" LIGHT_GREEN
strformat "93 LIGHT_YELLOW" LIGHT_YELLOW
strformat "94 LIGHT_BLUE" LIGHT_BLUE
strformat "95 LIGHT_MAGENTA" LIGHT_MAGENTA
strformat "96 LIGHT_CYAN" LIGHT_CYAN
strformat "97 WHITE" WHITE
strformat ""
strformat "49 BG_DEFAULT" BG_DEFAULT
strformat "40 BG_BLACK" BG_BLACK
strformat "41 BG_RED" BG_RED
strformat "42 BG_GREEN" BG_GREEN DARK_GREY
strformat "43 BG_YELLOW" BG_YELLOW DARK_GREY
strformat "44 BG_BLUE" BG_BLUE
strformat "45 BG_MAGENTA" BG_MAGENTA
strformat "46 BG_CYAN" BG_CYAN
strformat "47 BG_LIGHT_GREY" BG_LIGHT_GREY DARK_GREY
strformat "100 BG_DARK_GREY" BG_DARK_GREY
strformat "101 BG_LIGHT_RED" BG_LIGHT_RED DARK_GREY
strformat "102 BG_LIGHT_GREEN" BG_LIGHT_GREEN DARK_GREY
strformat "103 BG_LIGHT_YELLOW" BG_LIGHT_YELLOW DARK_GREY
strformat "104 BG_LIGHT_BLUE" BG_LIGHT_BLUE
strformat "105 BG_LIGHT_MAGENTA" BG_LIGHT_MAGENTA DARK_GREY
strformat "106 BG_LIGHT_CYAN" BG_LIGHT_CYAN DARK_GREY
strformat "107 BG_WHITE" BG_WHITE DARK_GREY
}
function strformat(){
local msg=$1
local codes=""
for name in "${@:2}"; do
local code="${(P)name}"
codes="${codes}\e[${code}m"
done
echo -e "${codes}${msg}\e[${RESET_CODE}m"
}
function strlen(){
echo ${#1}
}
function substring(){
local str=$1
local from=$2
local to=$3
echo ${str:$from:$to}
}
function strdrop_right(){
local str=$1
local n=$2
local length=$(strlen $str)
local from=0
local to=$length-$n
echo $(substring $str $from $to)
}
function strdrop_left(){
local str=$1
local n=$2
local length=$(strlen $str)
local from=$n
local to=$length
local result=$(substring $str $from $to)
echo $result
}
function strfill_right(){
local str=$1
local chr=$2
local n=$3
local i="0"
while [ $i -lt $n ]
do
i=$[$i+1]
str="$str$chr"
done
echo $str
}
function strfill_left(){
local str=$1
local chr=$2
local n=$3
local i="0"
while [ $i -lt $n ]
do
i=$[$i+1]
str="$chr$str"
done
echo $str
}
function strto_array(){
local str=$1
echo $str | fold -w1
}
function strreplace(){
local text=$1
local target=$2
local replacement=$3
local char='['
local escapedTarget=$(sed 's/['$char'/\]/\\'$char'/g' <<<"$target")
echo $text | sed "s/$escapedTarget/$replacement/g"
}
function strdecorate(){
local str=$1
local left=$2
if [ -z "$3" ]; then
local right=$left;
else
local right=$3;
fi
echo "$left$str$right"
}
function strregex(){
local str=$1
local regex=$2
setopt local_options BASH_REMATCH
if [[ $str =~ $regex ]]; then
for e in $BASH_REMATCH[@]; do
echo $e
done
fi
}
function strunwrap(){
local str=$1
local left=$2
local right=$3
local left_len=${#left}
local str_len=${#str}
local right_len=${#right}
if [[ $str == $left* ]]; then
str=${str:${left_len}:${#str}}
fi
if [[ $str == *$right ]]; then
local to=$((${#str} - $right_len))
str=${str:0:$to}
fi
echo $str
}
function strtake_left(){
local str=$1
local n=$2
local len=${#str}
local offset=$(($len-$n))
local result=$(strdrop_right $str $offset)
echo $result
}
function strtake_right(){
local str=$1
local n=$2
local len=${#str}
local offset=$(($len-$n))
local result=$(strdrop_left $str $offset)
echo $result
}
function strsplit(){
local str=$1
local delim=$2
local IFS=$delim
if [ $ZSH_VERSION ]; then
setopt sh_word_split
fi
for e in $str; do
echo $e
done
}