-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_manipulations.sh
369 lines (327 loc) · 8.38 KB
/
text_manipulations.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
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
#!/bin/bash
function args_or_stdin() {
if [[ "${#@}" -gt 0 ]]; then
(
IFS=$'\n'
echo "$*"
)
else
cat
fi
}
function sum() {
local sum=0
local line
while read line; do
((sum+=line))
done
echo "$sum"
}
function textwidth() {
local IFS=':'
read -ra positions <<< "$(visible_char_positions "$1")"
echo "${#positions[@]}"
}
function test_textwidth_color() {
assert "$(textwidth "hurp $(blue durp) gablurp")" == 17
}
function test_textwidth_escapecode() (
set -e
assert "$(textwidth "hurp $(bold durp) gablurp")" == 17
)
function test_textwidth_fullwidth() (
set -e
assert $(textwidth "hurp") == 8
)
function indexes_of() {
local str="$1"
local pattern="$2"
for ((i=0; i<${#str}; i++)); do
if [[ "${str:i}" == "$pattern"* ]]; then
echo "$i"
fi
done
}
function visible_char_positions() (
shopt -s extglob
local str="$1"
local escape_code_pattern=$'\e\[+([0-9])m'
local double_width_pattern='[!-~]'
local positions=()
for ((i=0; i<${#str}; i++)); do
case "${str:i}" in
${escape_code_pattern}*)
local rest_of_str="${str:i}"
local str_without_escape_code="${rest_of_str##${escape_code_pattern}}"
local escape_code="${rest_of_str%%"${str_without_escape_code}"}" #" wtf sublime
((i+=${#escape_code}-1))
;;
${double_width_pattern}*)
positions+=("" "$i")
;;
*)
positions+=("$i")
;;
esac
done
echo "${positions[*]}"
)
function test_visible_char_positions() {
set -e
assert "$(visible_char_positions "foo $(bold bar)")" == '0 1 2 3 8 9 10'
}
function test_visible_char_positions_fullwidth() {
set -e
assert "$(visible_char_positions "hurp")" == ' 0 1 2 3'
}
function test_visible_char_positions_fullwidth_array() (
set -e
local IFS=$'\n'
array=($(visible_char_positions "hurp"))
)
function linewrap() {
local line outline nextaddition
args_or_stdin "$@" | while read line; do
while [[ "$line" ]]; do
IFS=' ' read -ra positions <<< "$(visible_char_positions "$line")"
if [[ "${#positions[@]}" -le "$COLUMNS" ]]; then
outline="$line"
else
outline="${line:0:${positions[$COLUMNS]}+1}"
outline="${outline% *}"
if [[ ! "$outline" ]]; then
outline="${line}"
fi
outline="${outline:0:${positions[$COLUMNS]}}"
fi
echo "$outline"
line="${line:${#outline}}"
line="${line# }"
done
done
}
function test_linewrap_basic() {
(
set -e
COLUMNS=7
assert "$(linewrap "hi hello")" == $'hi\nhello'
)
}
function test_linewrap_longword() {
(
set -e
COLUMNS=3
assert "$(linewrap "hi hello")" == $'hi\nhel\nlo'
)
}
function test_linewrap_formatting() {
(
set -e
COLUMNS=5
assert "$(linewrap "hi $(bold hello)")" == $'hi\n'"$(bold hello)"
)
}
function center() {
# Center the input or argument.
local line
args_or_stdin "$@" | while read line; do
local linewidth="$(textwidth "$line")"
printf "%$(( (COLUMNS - linewidth) / 2 ))s%s\n" "" "$line"
done
}
function escapecode() {
# Wrap each input line (excluding leading and trailing spaces) in \e[<code>m, where code = $1
local code=$'\e'"\[$1m"
shift
args_or_stdin "$@" | sed -e "s/^ */&${code}/" -e $'s/\e\\[0m/&'"${code}/g" -e $'s/ *$/\e\\[0m&/g'
}
function underline() {
escapecode 4 "$@"
}
function bold() {
escapecode 1 "$@"
}
function capitalize() {
# Change all letters to uppercase, except those in an escape sequence.
local IFS=''
lowers=({a..z})
uppers=({A..Z})
args_or_stdin "$@" | sed -e "y/${lowers[*]}/${uppers[*]}/" -e $'s/\\(\e\\[[0-9]\\)M/\\1m/g'
}
function test_capitalize() {
set -e
assert "$(capitalize hurp)" == "HURP"
assert "$(capitalize "$(bold hurp)")" == "$(bold HURP)"
}
function line() {
local num="$1"
local char="$2"
spaces="$(printf "%${num}s")"
echo "${spaces// /$char}"
}
function test_line() {
set -e
assert "$(line 5 -)" == "-----"
}
function pad_on_right() {
local width="$1"
local line
shift
local IFS=''
args_or_stdin "$@" | while read line; do
echo "${line}$(line "$((width-$(textwidth "$line")))" ' ')"
done
}
function pad_on_left() {
local width="$1"
local line
local IFS=''
shift
args_or_stdin "$@" | while read line; do
echo "$(line "$((width-$(textwidth "$line")))" ' ')${line}"
done
}
function margin_left() {
local marginsize="$1"
shift
local margin="$(line "$marginsize" ' ')"
local IFS=''
args_or_stdin "$@" | while read line; do
echo "${margin}${line}"
done
}
function pad_on_bottom() {
local height="$1"
shift
args_or_stdin "$@" | (
local inputlines=0
local IFS=''
while read line; do
echo "$line"
((inputlines++))
done
while ((inputlines + 1 < height)); do
echo
((inputlines++))
done
)
}
function box() {
local bottomleft=$'\xe2\x94\x94'
local bottomright=$'\xe2\x94\x98'
local topleft=$'\xe2\x94\x8c'
local topright=$'\xe2\x94\x90'
local horizontal=$'\xe2\x94\x80'
local vertical=$'\xe2\x94\x82'
local longestline=0
local line
local IFS=''
lines=()
while read line; do
width="$(textwidth "$line")"
if [[ "$width" -gt "$longestline" ]]; then
longestline="$width"
fi
lines+=("$line")
done < <(args_or_stdin "$@")
echo "${topleft}$(line "$longestline" "$horizontal")${topright}"
for line in "${lines[@]}"; do
echo "${vertical}$(pad_on_right "$longestline" "$line")${vertical}"
done
echo "${bottomleft}$(line "$longestline" "$horizontal")${bottomright}"
}
function test_box() {
local IFS=$'\n'
expected=(
"┌─────┐"
"│hi │"
"│hello│"
"└─────┘"
)
assert "$(box hi hello)" == "${expected[*]}"
}
function bigger_letterpart() (
cd "$( dirname "${BASH_SOURCE[0]}" )"
letter="$1"
part="$2"
fgrep "#${letter}${part}#" "smbraille.txt" | sed 's/^#..#\(.*\)#$/\1/'
# IFS='#'
# # exec 3<smbraille.txt
# while read line; do
# if [[ "${line:1:2}" == "${letter}${part}" ]]; then
# echo "${line:4:${#line}-5}"
# # break
# fi
# done <smbraille.txt
)
function bigger_line() {
local line="$1"
local part="$2"
local outline=''
for ((i=0; i<${#line}; i++)); do
letter="${line:$i:1}"
outline="${outline}$(bigger_letterpart "$letter" "$part")"
done
echo "$outline"
}
function bigger() {
local line
local IFS=''
args_or_stdin "$@" | while read line; do
local outline=''
local wrappedline
bigger_line "$line" "0" |
linewrap |
tr -d '%' |
sed 's/ / /g' |
while read wrappedline; do
bigger_line "$wrappedline" 1
bigger_line "$wrappedline" 2
done
done
}
function fullwidth_char() {
args_or_stdin "$@" | tr '!-~' '!-~'
}
function fullwidth() (
# set -x
local line pos
args_or_stdin "$@" | while read line; do
local outline=''
local lastpos=-1
local IFS=' '
for pos in $(visible_char_positions "$line") "${#line}"; do
local invisiblestart="$((${lastpos}+1))"
local invisiblelength=$((pos - invisiblestart))
local invisible="${line:$invisiblestart:$invisiblelength}"
local char="${line:$pos:1}"
outline+="${invisible}$(fullwidth_char "$char")"
lastpos="$pos"
done
echo "$outline"
done
)
function black() {
escapecode 30 "$@"
}
function red() {
escapecode 31 "$@"
}
function green() {
escapecode 32 "$@"
}
function yellow() {
escapecode 33 "$@"
}
function blue() {
escapecode 34 "$@"
}
function purple() {
escapecode 35 "$@"
}
function cyan() {
escapecode 36 "$@"
}
function white() {
escapecode 37 "$@"
}