/
cfold
executable file
·405 lines (349 loc) · 12.6 KB
/
cfold
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
#!/usr/bin/awk -f
# cfold -- [OPTIONS] [FILE...]
#
# the '--' is required, so AWK itself doesn't read the options
#
# Wraps input lines in each FILE (standard input if not provided), writing to
# standard output. The default width is that of the terminal, or 80 columns if
# standard output is not a terminal. If FILE is '-', also reads the standard
# input
#
# Options:
# -c, --center center each line on the terminal. assumes a width of 80
# columns if standard output is not a terminal
# -b, --break[=LIST] break lines at spaces. LIST, if provided, is a list of
# characters to break lines at instead of spaces. For
# example, --break=abc will break lines on "a" or "b"
# Note: an empty string for LIST will revert to the
# default behavior, it is not the same as omitting -b
# -i, --ignore-breaks convert existing single line breaks to spaces. multiple
# line breaks like those at the end of paragraphs will be
# truncated to a single empty line
# -w, --width WIDTH use WIDTH columns instead of the terminal's width
# -t, --trim trim leading and trailing whitespace from each line
# prior to folding
# -h, --help display this help and exit
## usage: center(string[, width])
## returns "string" centered based on "width". if "width" is not provided (or
## is 0), uses the width of the terminal, or 80 if standard output is not open
## on a terminal.
## note: does not check the length of the string. if it's wider than the
## terminal, it will not center lines other than the first. for best results,
## combine with fold() (see the cfold script in the examples directory for a
## script that does exactly this)
function center(str, cols, off, cmd) {
if (!cols) {
# checks if stdout is a tty
if (system("test -t 1")) {
cols = 80;
} else {
cmd = "tput cols";
cmd | getline cols;
close(cmd);
}
}
off = int((cols/2) + (length(str)/2));
return sprintf("%*s", off, str);
}
## usage: fold(string, sep[, width])
## returns "string", wrapped, with lines broken on "sep" to "width" columns.
## "sep" is a list of characters to break at, similar to IFS in a POSIX shell.
## if "sep" is empty, wraps at exactly "width" characters. if "width" is not
## provided (or is 0), uses the width of the terminal, or 80 if standard output
## is not open on a terminal.
## note: currently, tabs are squeezed to a single space. this will be fixed
function fold(str, sep, cols, out, cmd, i, len, chars, c, last, f, first) {
if (!cols) {
# checks if stdout is a tty
if (system("test -t 1")) {
cols = 80;
} else {
cmd = "tput cols";
cmd | getline cols;
close(cmd);
}
}
# squeeze tabs and newlines to spaces
gsub(/[\t\n]/, " ", str);
# if "sep" is empty, just fold on cols with substr
if (!length(sep)) {
len = length(str);
out = substr(str, 1, cols);
for (i=cols+1; i<=len; i+=cols) {
out = out "\n" substr(str, i, cols);
}
return out;
# otherwise, we have to loop over every character (can't split() on sep, it
# would destroy the existing separators)
} else {
# split string into char array
len = split(str, chars, //);
# set boolean, used to assign the first line differently
first = 1;
for (i=1; i<=len; i+=last) {
f = 0;
for (c=i+cols-1; c>=i; c--) {
if (index(sep, chars[c])) {
last = c - i + 1;
f = 1;
break;
}
}
if (!f) {
last = cols;
}
if (first) {
out = substr(str, i, last);
first = 0;
} else {
out = out "\n" substr(str, i, last);
}
}
}
# return the output
return out;
}
## usage: getopts(optstring [, longopt_array ])
## parses options, and deletes them from ARGV. "optstring" is of the form
## "ab:c". each letter is a possible option. if the letter is followed by a
## colon (:), then the option requires an argument. if an argument is not
## provided, or an invalid option is given, getopts will print the appropriate
## error message and return "?". returns each option as it's read, and -1 when
## no options are left. "optind" will be set to the index of the next
## non-option argument when finished. "optarg" will be set to the option's
## argument, when provided. if not provided, "optarg" will be empty. "optname"
## will be set to the current option, as provided. getopts will delete each
## option and argument that it successfully reads, so awk will be able to treat
## whatever's left as filenames/assignments, as usual. if provided,
## "longopt_array" is the name of an associative array that maps long options to
## the appropriate short option. (do not include the hyphens on either).
## sample usage can be found in the examples dir, with gawk extensions, or in
## the ogrep script for a POSIX example: https://github.com/e36freak/ogrep
function getopts(optstring, longarr, opt, trimmed, hasarg, repeat) {
hasarg = repeat = 0;
optarg = "";
# increment optind
optind++;
# return -1 if the current arg is not an option or there are no args left
if (ARGV[optind] !~ /^-/ || optind >= ARGC) {
return -1;
}
# if option is "--" (end of options), delete arg and return -1
if (ARGV[optind] == "--") {
for (i=1; i<=optind; i++) {
delete ARGV[i];
}
return -1;
}
# if the option is a long argument...
if (ARGV[optind] ~ /^--/) {
# trim hyphens
trimmed = substr(ARGV[optind], 3);
# if of the format --foo=bar, split the two. assign "bar" to optarg and
# set hasarg to 1
if (trimmed ~ /=/) {
optarg = trimmed;
sub(/=.*/, "", trimmed); sub(/^[^=]*=/, "", optarg);
hasarg = 1;
}
# invalid long opt
if (!(trimmed in longarr)) {
printf("unrecognized option -- '%s'\n", ARGV[optind]) > "/dev/stderr";
return "?";
}
opt = longarr[trimmed];
# set optname by prepending dashes to the trimmed argument
optname = "--" trimmed;
# otherwise, it is a short option
} else {
# remove the hyphen, and get just the option letter
opt = substr(ARGV[optind], 2, 1);
# set trimmed to whatevers left
trimmed = substr(ARGV[optind], 3);
# invalid option
if (!index(optstring, opt)) {
printf("invalid option -- '%s'\n", opt) > "/dev/stderr";
return "?";
}
# if there is more to the argument than just -o
if (length(trimmed)) {
# if option requires an argument, set the rest to optarg and hasarg to 1
if (index(optstring, opt ":")) {
optarg = trimmed;
hasarg = 1;
# otherwise, prepend a hyphen to the rest and set repeat to 1, so the
# same arg is processed again without the first option
} else {
ARGV[optind] = "-" trimmed;
repeat = 1;
}
}
# set optname by prepending a hypen to opt
optname = "-" opt;
}
# if the option requires an arg and hasarg is 0
if (index(optstring, opt ":") && !hasarg) {
# increment optind, check if no arguments are left
if (++optind >= ARGC) {
printf("option requires an argument -- '%s'\n", optname) > "/dev/stderr";
return "?";
}
# set optarg
optarg = ARGV[optind];
# if repeat is set, decrement optind so we process the same arg again
# mutually exclusive to needing an argument, otherwise hasarg would be set
} else if (repeat) {
optind--;
}
# delete all arguments up to this point, just to make sure
for (i=1; i<=optind; i++) {
delete ARGV[i];
}
# return the option letter
return opt;
}
## usage: trim(string)
## returns "string" with leading and trailing whitespace trimmed
function trim(str) {
gsub(/^[[:blank:]]+|[[:blank:]]+$/, "", str);
return str;
}
# prints usage
function usage() {
printf("%s\n\n%s\n\n%s\n%s\n%s\n%s\n\n",
"cfold -- [OPTIONS] [FILE...]",
"the '--' is required, so AWK itself doesn't read the options",
"Wraps input lines in each FILE (standard input if not provided), writing to",
"standard output. The default width is that of the terminal, or 80 columns if",
"standard output is not a terminal. If FILE is '-', also reads the standard",
"input") > "/dev/stderr";
printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
" Options:",
" -c, --center center each line on the terminal. assumes a width of 80",
" columns if standard output is not a terminal",
" -b, --break[=LIST] break lines at spaces. LIST, if provided, is a list of",
" characters to break lines at instead of spaces. For",
" example, --break=abc will break lines on \"a\" or \"b\"",
" Note: an empty string for LIST will revert to the",
" default behavior, it is not the same as omitting -b",
" -i, --ignore-breaks convert existing single line breaks to spaces. multiple",
" line breaks like those at the end of paragraphs will be",
" truncated to a single empty line",
" -w, --width WIDTH use WIDTH columns instead of the terminal's width",
" -t, --trim trim leading and trailing whitespace from each line",
" prior to folding",
" -h, --help display this help and exit") > "/dev/stderr";
}
BEGIN {
# initialize variables to defaults
toexit = err = 0;
tocenter = toignore = totrim = 0;
break_chars = "";
# get default width
if (system("test -t 1")) {
# stdout is not open on a tty
width = 80
} else {
cmd = "tput cols";
cmd | getline width;
close(cmd);
}
# map long options to short options
longopts["center"] = "c";
longopts["break"] = "b";
longopts["ignore-breaks"] = "i";
longopts["width"] = "w";
longopts["trim"] = "t";
longopts["help"] = "h";
# parse the options
while ((opt = getopts("cbiw:th", longopts)) != -1) {
switch(opt) {
# -c, --center
case "c":
tocenter = 1; break;
# -b, --break
case "b":
if (length(optarg)) {
break_chars = optarg;
} else {
break_chars = " \t\n";
}
break;
# -i, --ignore-breaks
case "i":
toignore = 1; break;
# w, --width
case "w":
# make sure optarg is an integer
if (optarg !~ /^[0-9]+$/) {
printf("'%s' is not a valid argument for '%s', must be a number\n",
optarg, optname) > "/dev/stderr";
err = toexit = 1;
exit;
}
width = optarg;
break;
# -t, --trim
case "t":
totrim = 1; break;
# -h, --help
case "h":
usage(); toexit = 1; exit;
# error
case "?":
default:
err = toexit = 1;
exit;
}
}
# if --ignore-breaks was used, set RS to null so that paragraphs are
# treated as one line
if (toignore) {
RS = "";
}
}
########
# if --ignore-breaks was used, print extra newline between records
toignore && NR > 1 {
print "";
}
# fold each record (line, or paragraph)
{
# if --trim was used, reassign $0 with leading/trailing whitespace removed
if (totrim) {
$0 = trim($0);
}
out = fold($0, break_chars, width);
# if text is to be centered, split out into an array of lines and center each
if (tocenter) {
len = split(out, lines, /\n/);
for (i=1; i<=len; i++) {
print center(lines[i]);
}
} else {
print out;
}
}
# exit according to "err"
END {
exit err;
}
# Copyright Daniel Mills <dm@e36freak.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.