-
Notifications
You must be signed in to change notification settings - Fork 0
/
textfmt
287 lines (254 loc) · 6.49 KB
/
textfmt
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
#!/usr/bin/env bash
set -o pipefail
declare -r EC_ILLEGAL_CMD=127 # Command not found
has_command()
{
command -v "${1}" &> /dev/null
}
join_by()
{
local d="${1}"; shift; echo -n "$1"; shift; printf "%s" "${@/#/$d}";
}
is_tty()
{
[ -t "${1}" ]
}
BOLD="\033[1m"
DIM="\033[2m"
ITALIC="\033[3m"
UNDERLINED="\033[4m"
BLINK="\033[5m"
RAPID_BLINK="\033[6m"
REVERSE="\033[7m"
HIDDEN="\033[8m"
CROSSED_OUT="\033[9m"
NORMAL="\033[0m"
RESET_ALL="${NORMAL}"
RESET_BOLD="\033[21m"
RESET_DIM="\033[22m"
RESET_ITALIC="\033[23m"
RESET_UNDERLINED="\033[24m"
RESET_BLINK="\033[25m"
RESET_RAPID_BLINK="\033[26m"
RESET_REVERSE="\033[27m"
RESET_HIDDEN="\033[28m"
RESET_CROSSED_OUT="\033[29m"
RESET_FG="\033[39m"
RESET_BG="\033[49m"
BLACK="\033[30m"
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
BLUE="\033[34m"
MAGENTA="\033[35m"
CYAN="\033[36m"
SILVER="\033[37m"
BG_BLACK="\033[40m"
BG_RED="\033[41m"
BG_GREEN="\033[42m"
BG_YELLOW="\033[43m"
BG_BLUE="\033[44m"
BG_MAGENTA="\033[45m"
BG_CYAN="\033[46m"
BG_SILVER="\033[47m"
GREY="\033[90m"
LIGHT_RED="\033[91m"
LIGHT_GREEN="\033[92m"
LIGHT_YELLOW="\033[93m"
LIGHT_BLUE="\033[94m"
LIGHT_MAGENTA="\033[95m"
LIGHT_CYAN="\033[96m"
WHITE="\033[97m"
BG_GREY="\033[100m"
BG_LIGHT_RED="\033[101m"
BG_LIGHT_GREEN="\033[102m"
BG_LIGHT_YELLOW="\033[103m"
BG_LIGHT_BLUE="\033[104m"
BG_LIGHT_MAGENTA="\033[105m"
BG_LIGHT_CYAN="\033[106m"
BG_WHITE="\033[107m"
declare -A COLORS
COLORS[bold]=1
COLORS[dim]=2
COLORS[italic]=3
COLORS[underlined]=4
COLORS[blink]=5
COLORS[rapid blink]=6
COLORS[reverse]=7
COLORS[hidden]=8
COLORS[crossed out]=9
COLORS[reset all]=0
COLORS[reset bold]=21
COLORS[reset dim]=22
COLORS[reset italic]=23
COLORS[reset underlined]=24
COLORS[reset blink]=25
COLORS[reset rapid blink]=26
COLORS[reset reverse]=27
COLORS[reset hidden]=28
COLORS[reset crossed out]=29
COLORS[reset fg]=39
COLORS[reset bg]=49
COLORS[black]=30
COLORS[red]=31
COLORS[green]=32
COLORS[yellow]=33
COLORS[blue]=34
COLORS[magenta]=35
COLORS[cyan]=36
COLORS[silver]=37
COLORS[bg black]=40
COLORS[bg red]=41
COLORS[bg green]=42
COLORS[bg yellow]=43
COLORS[bg blue]=44
COLORS[bg magenta]=45
COLORS[bg cyan]=46
COLORS[bg silver]=47
COLORS[grey]=90
COLORS[light red]=91
COLORS[light green]=92
COLORS[light yellow]=93
COLORS[light blue]=94
COLORS[light magenta]=95
COLORS[light cyan]=96
COLORS[white]=97
COLORS[bg grey]=100
COLORS[bg light red]=101
COLORS[bg light green]=102
COLORS[bg light yellow]=103
COLORS[bg light blue]=104
COLORS[bg light magenta]=105
COLORS[bg light cyan]=106
COLORS[bg white]=107
combine()
{
local names=()
while [ "${#}" -gt 0 ]; do
if [ -n "${COLORS[${1}]}" ]; then
names+=("${COLORS[${1}]}")
fi
shift
done
local esc=''
if [ -n "${names}" ]; then
esc+='\033['
esc+=$(join_by ';' "${names[@]}")
esc+='m'
fi
echo "${esc}"
}
__log_field_delimiter=': '
__log_headers=()
# __log_levelname=true
# __log_datetime=true
log_header_push()
{
while [ "${#}" \> 0 ]; do
__log_headers["${#__log_headers[@]}"]="${1}"
shift
done
}
log_header_pop()
{
if [ "${#__log_headers[@]}" \> 0 ]; then
unset __log_headers["${#__log_headers[@]}"-1]
fi
}
log_fatal()
{
local line=$(join_by "${__log_field_delimiter}" ${__log_levelname:+FATAL} ${__log_datetime:+"$(date +'%Y-%m-%dT%H:%M:%S%z')"} "${__log_headers[@]}" "${@}")
if is_tty 2; then
line="${RED}${line}${NORMAL}"
fi
echo -e "${line}" >&2
}
log_error()
{
local line=$(join_by "${__log_field_delimiter}" ${__log_levelname:+ERROR} ${__log_datetime:+"$(date +'%Y-%m-%dT%H:%M:%S%z')"} "${__log_headers[@]}" "${@}")
if is_tty 2; then
line="$(combine 'bold' 'red')${line}${NORMAL}"
fi
echo -e "${line}" >&2
}
log_warning()
{
local line=$(join_by "${__log_field_delimiter}" ${__log_levelname:+WARNING} ${__log_datetime:+"$(date +'%Y-%m-%dT%H:%M:%S%z')"} "${__log_headers[@]}" "${@}")
if is_tty 2; then
line="${BOLD}${YELLOW}${line}${NORMAL}"
fi
echo -e "${line}" >&2
}
log_info()
{
local line=$(join_by "${__log_field_delimiter}" ${__log_levelname:+INFO} ${__log_datetime:+"$(date +'%Y-%m-%dT%H:%M:%S%z')"} "${__log_headers[@]}" "${@}")
if is_tty 1; then
line="${BOLD}${line}${NORMAL}"
fi
echo -e "${line}" >&1
}
log_debug()
{
local line=$(join_by "${__log_field_delimiter}" ${__log_levelname:+DEBUG} ${__log_datetime:+"$(date +'%Y-%m-%dT%H:%M:%S%z')"} "${__log_headers[@]}" "${@}")
echo -e "${line}" >&1
}
peval()
{
local rs rv
rs=$(eval "${1}" 2>&1); rv="${?}"
if [ "$rv" != 0 ]; then
log_error "${@:2:${#}}" "${1}" "[$rv]" "$rs"
return "$rv"
fi
echo -n "$rs"
}
to_upper()
{
echo "${@}" | tr '[a-z]' '[A-Z]'
}
readonly SCRIPT_NAME="$(basename $0)"
usage()
{
printf "USAGE: $SCRIPT_NAME %s\n" "${*}" >&2
exit
}
main()
{
[ "${#}" == 0 ] && usage DIR
log_header_push "$SCRIPT_NAME"
if ! has_command dos2unix || ! has_command mac2unix; then
log_error 'dos2unix<http://waterlan.home.xs4all.nl/dos2unix.html> required'
exit "$EC_ILLEGAL_CMD"
fi
if ! has_command sponge; then
log_error 'moreutils<https://joeyh.name/code/moreutils/> required'
exit "$EC_ILLEGAL_CMD"
fi
if ! has_command uchardet; then
log_error 'uchardet<https://www.freedesktop.org/wiki/Software/uchardet> required'
exit "$EC_ILLEGAL_CMD"
fi
IFS=$'\n'
for dir ; do
files=$(find "${dir}" -name .git -prune -o -name .svn -prune -o -type f -exec file {} \; | grep text | grep 'with CRLF line terminators' | cut -d: -f1 2> /dev/null)
for f in $files; do
log_info "$(readlink -f ${f} 2> /dev/null)" "CRLF to LF"
dos2unix -q -k -o "${f}"
done
files=$(find "${dir}" -name .git -prune -o -name .svn -prune -o -type f -exec file {} \; | grep text | grep 'with CR line terminators' | cut -d: -f1 2> /dev/null)
for f in $files; do
log_info "$(readlink -f ${f} 2> /dev/null)" "CR to LF"
mac2unix -q -k -o "${f}"
done
files=$(find "${dir}" -name .git -prune -o -name .svn -prune -o -type f -exec file {} \; | grep text | cut -d: -f1 2> /dev/null)
for f in $files; do
if enc=$(peval "uchardet '${f}'"); then
if [[ $(to_upper "$enc") != 'UTF-8' ]]; then
log_info "$(readlink -f ${f} 2> /dev/null)" "${enc} to UTF-8"
peval "iconv -c -f ${enc} -t UTF-8 '${f}'" | sponge "${f}"
fi
fi
done
done
}
main "${@}"