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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
|
#!/usr/bin/env bun
import { closeSync, openSync, readSync } from "node:fs";
import { stat } from "node:fs/promises";
import { decodeSurrogateEscapedBytes, isWriteError, localeQuotedEscapedDiagnostic, nodeErrorMessage, normalizeLongOptionByPrefix, parseOptions, readAll, systemErrorMessage, textInputDiagnosticName } from "../shared/common.js";
import { UsageError, stderr, stdout } from "../shared/diagnostics.js";
import { prInputIsNonRegular } from "../shared/text.js";
import { strftime } from "../shared/time.js";
import { defineCommand, runAsMain } from "../shared/command.js";
export const PR_LONG_OPTIONS = ["across", "columns", "date-format", "double-space", "expand-tabs", "first-line-number", "form-feed", "header", "indent", "join-lines", "length", "merge", "no-file-warnings", "number-lines", "omit-header", "omit-pagination", "output-tabs", "page-width", "pages", "sep-string", "separator", "show-control-chars", "show-nonprinting", "width", "help", "version"];
export function prMetaOption(args) {
const longValueOptions = new Set(["columns", "date-format", "first-line-number", "header", "indent", "length", "page-width", "pages", "width"]);
const longOptionalValueOptions = new Set(["expand-tabs", "number-lines", "output-tabs", "sep-string", "separator"]);
const shortValueOptions = new Set(["D", "h", "l", "N", "o", "w", "W"]);
const shortOptionalValueOptions = new Set(["e", "i", "n", "s", "S"]);
const shortKnownOptions = new Set(["a", "b", "c", "d", "e", "f", "F", "h", "i", "J", "l", "m", "n", "N", "o", "r", "s", "S", "t", "T", "v", "w", "W", "D"]);
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === "--") return null;
if (arg.startsWith("--")) {
const option = normalizeLongOptionByPrefix(arg, PR_LONG_OPTIONS);
const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2);
if (!PR_LONG_OPTIONS.includes(name)) return null;
if (name === "help" || name === "version") {
if (inlineValue != null) throw new UsageError(`option '--${name}' doesn't allow an argument`, true);
return option;
}
const value = inlineValue ?? (longValueOptions.has(name) ? args[i + 1] : undefined);
if (value !== undefined) validatePrMetaOptionValue(name, value);
if (inlineValue == null && longValueOptions.has(name)) i++;
else if (inlineValue == null && longOptionalValueOptions.has(name)) continue;
else if (inlineValue != null && !longValueOptions.has(name) && !longOptionalValueOptions.has(name)) return null;
continue;
}
if (!arg.startsWith("-") || arg === "-") continue;
if (/^-\d+$/.test(arg)) continue;
if (/^-\d/.test(arg)) return null;
for (let j = 1; j < arg.length; j++) {
const ch = arg[j];
if (!shortKnownOptions.has(ch)) return null;
if (shortValueOptions.has(ch)) {
const inlineValue = arg.slice(j + 1);
const value = inlineValue === "" ? args[i + 1] : inlineValue;
validatePrMetaShortValue(ch, value);
if (inlineValue === "") i++;
break;
}
if (shortOptionalValueOptions.has(ch)) {
const inlineValue = arg.slice(j + 1);
if (inlineValue !== "") validatePrMetaShortValue(ch, inlineValue);
break;
}
}
}
return null;
}
export function validatePrMetaOptionValue(name, value) {
if (name === "columns") parsePrColumnCount(value);
else if (name === "first-line-number") parsePrLineNumber(value);
else if (name === "indent") parsePrNonnegativeInteger(value, "-o MARGIN", "line offset");
else if (name === "page-width") parsePrPositiveInteger(value, "-W PAGE_WIDTH", "characters");
else if (name === "width") parsePrPositiveInteger(value, "-w PAGE_WIDTH", "characters");
else if (name === "length") parsePrPositiveInteger(value, "-l PAGE_LENGTH", "lines");
else if (name === "pages") validatePrPages(value);
else if (name === "expand-tabs") validatePrOptionalNumberSpec(value, "-e");
else if (name === "number-lines") validatePrOptionalNumberSpec(value, "-n");
else if (name === "output-tabs") validatePrOptionalNumberSpec(value, "-i");
}
export function validatePrMetaShortValue(ch, value) {
if (value === undefined) return;
if (ch === "l") validatePrMetaOptionValue("length", value);
else if (ch === "o") validatePrMetaOptionValue("indent", value);
else if (ch === "w") validatePrMetaOptionValue("width", value);
else if (ch === "W") validatePrMetaOptionValue("page-width", value);
else if (ch === "e") validatePrMetaOptionValue("expand-tabs", value);
else if (ch === "n") validatePrMetaOptionValue("number-lines", value);
else if (ch === "i") validatePrMetaOptionValue("output-tabs", value);
}
export function validatePrOptionalNumberSpec(value, option) {
const text = String(value);
if (text === "") throw new UsageError(`'${option}': Invalid argument: ${localeQuotedEscapedDiagnostic(text)}`, true);
const numberText = /^\d/.test(text) ? text : text.slice(1);
if (numberText === "") return;
let tooLarge = false;
let valid = /^\d+$/.test(numberText);
if (valid) {
const n = BigInt(numberText);
valid = n > 0n;
tooLarge = n > BigInt(Number.MAX_SAFE_INTEGER);
}
if (!valid || tooLarge) {
const suffix = tooLarge ? ": Value too large for defined data type" : "";
throw new UsageError(`'${option}' extra characters or invalid number in the argument: ${localeQuotedEscapedDiagnostic(numberText)}${suffix}`, true);
}
}
export async function prCmd(args) {
args = normalizePrColumnArgs(args);
const { opts, operands } = parseOptions(args, { short: { a: false, b: false, c: false, d: false, e: "optional-value", f: false, F: false, h: "value", i: "optional-value", J: false, l: "value", m: false, n: "optional-value", N: "value", o: "value", r: false, s: "optional-value", S: "optional-value", t: false, T: false, v: false, w: "value", W: "value", D: "value" }, long: { across: false, columns: "value", "date-format": "value", "double-space": false, "expand-tabs": "optional-value", "first-line-number": "value", "form-feed": false, header: "value", indent: "value", "join-lines": false, length: "value", merge: false, "no-file-warnings": false, "number-lines": "optional-value", "omit-header": false, "omit-pagination": false, "output-tabs": "optional-value", "page-width": "value", pages: "value", "sep-string": "optional-value", separator: "optional-value", "show-control-chars": false, "show-nonprinting": false, width: "value" } });
validatePrPages(opts.pages);
const files = operands.length ? operands : ["-"];
const numberSpec = opts.n ?? opts["number-lines"];
const numberLines = numberSpec !== undefined;
const firstLineNumberSpecified = opts.N !== undefined || opts["first-line-number"] !== undefined;
const { separator, width } = parsePrNumberSpec(numberSpec);
let lineNumber = parsePrLineNumber(opts.N ?? opts["first-line-number"] ?? 1);
const indent = parsePrNonnegativeInteger(opts.o ?? opts.indent ?? 0, "-o MARGIN", "line offset");
const columns = parsePrColumnCount(opts.columns);
const pageWidthText = opts.W ?? opts["page-width"] ?? opts.w ?? opts.width;
const pageWidthLabel = opts.W !== undefined || opts["page-width"] !== undefined ? "-W PAGE_WIDTH" : "-w PAGE_WIDTH";
const pageWidth = pageWidthText == null ? 72 : parsePrPositiveInteger(pageWidthText, pageWidthLabel, "characters");
const pageLengthText = opts.l ?? opts.length;
const pageLength = pageLengthText == null ? 66 : parsePrPositiveInteger(pageLengthText, "-l PAGE_LENGTH", "lines");
const pageRange = parsePrPageRange(opts.pages);
const explicitPageWidth = pageWidthText != null;
const oldSeparatorSpecified = opts.s !== undefined || opts.separator !== undefined;
const newSeparatorSpecified = opts.S !== undefined || opts["sep-string"] !== undefined;
const columnSeparatorSpecified = oldSeparatorSpecified || newSeparatorSpecified;
const columnSeparator = newSeparatorSpecified
? (opts.S === true || opts["sep-string"] === true ? "" : String(opts.S ?? opts["sep-string"]))
: (opts.s === true || opts.separator === true ? (opts.w ?? opts.width ? "" : "\t") : (typeof (opts.s ?? opts.separator) === "string" ? String(opts.s ?? opts.separator) : " "));
const omitHeader = opts.t || opts.T || opts["omit-header"] || opts["omit-pagination"] || pageLength <= 10;
const expandTabs = opts.e !== undefined || opts["expand-tabs"] !== undefined;
const tabSpec = parsePrTabSpec(opts.e ?? opts["expand-tabs"]);
const tabSize = tabSpec.size;
const outputTabs = opts.i !== undefined || opts["output-tabs"] !== undefined;
const outputTabSize = parsePrTabSize(opts.i ?? opts["output-tabs"]);
const formFeed = opts.f || opts.F || opts["form-feed"];
if (opts.m || opts.merge) {
const texts = [];
let failed = false;
for (const file of files) {
let text;
try {
text = decodeSurrogateEscapedBytes(await readAll(file));
} catch (error) {
if (!(opts.r || opts["no-file-warnings"])) stderr(prReadError(file, error));
failed = true;
continue;
}
if (expandTabs) text = expandPrTabs(text, tabSize);
if (hasPrNonprintingRendering(opts)) text = renderPrNonprinting(text, opts);
texts.push(text);
}
let pageHeader = null;
if (!omitHeader) {
const headerName = opts.h ?? opts.header ?? "";
const headerDate = opts.D || opts["date-format"]
? await prHeaderDate(files[0] ?? "-", opts)
: strftime(new Date(), "%Y-%m-%d %H:%M");
pageHeader = (page) => formatPrPageHeader(formatPrHeader(headerDate, headerName, pageWidth, page), indent);
}
const mergeJoinLines = opts.J || opts["join-lines"];
const mergeColumnSeparator = mergeJoinLines && !columnSeparatorSpecified ? "\t" : columnSeparator;
let rendered = renderPrMerge(texts, { columnCount: files.length, columnSeparator: mergeColumnSeparator, defaultColumnSeparator: !columnSeparatorSpecified, explicitPageWidth, formFeed, indent, joinLines: mergeJoinLines, numberLines, numberSkippedLines: !firstLineNumberSpecified, pageHeader, pageLength, pageRange, padColumns: !oldSeparatorSpecified, pageWidth, preserveMergeTabs: oldSeparatorSpecified && !newSeparatorSpecified && columnSeparator === "\t", separator, stripFormFeeds: opts.T || opts["omit-pagination"], width, nextNumber: () => lineNumber++ });
if (outputTabs) rendered = tabifyPrOutput(rendered, outputTabSize);
stdout(rendered);
return failed ? 1 : 0;
}
let out = "";
let failed = false;
for (const file of files) {
let text;
const paginatedBody = pageLength != null && !omitHeader;
if (files.length === 1 && prInputIsNonRegular(file) && columns === 1 && paginatedBody && opts.pages == null && !expandTabs && !outputTabs && !hasPrNonprintingRendering(opts) && !(opts.J || opts["join-lines"]) && !oldSeparatorSpecified && !newSeparatorSpecified) {
const headerName = opts.h ?? opts.header ?? "";
const headerDate = await prHeaderDate(file, opts);
const pageHeader = (page) => formatPrPageHeader(formatPrHeader(headerDate, headerName, pageWidth, page), indent);
let fd = 0;
try {
if (file !== "-") fd = openSync(file, "r");
streamPrPaginatedSingleColumnFd(fd, { doubleSpace: opts.d || opts["double-space"], explicitPageWidth: effectivePrExplicitPageWidth(explicitPageWidth, false, columns, pageWidth, opts), formFeed, indent, numberLines, numberSkippedLines: !firstLineNumberSpecified, pageHeader, pageLength, pageRange, pageWidth, separator, width, nextNumber: () => lineNumber++ });
return 0;
} catch (error) {
if (isWriteError(error)) throw error;
if (!(opts.r || opts["no-file-warnings"])) stderr(prReadError(file, error));
return 1;
} finally {
if (file !== "-" && fd !== 0) closeSync(fd);
}
}
try {
text = decodeSurrogateEscapedBytes(await readAll(file));
} catch (error) {
if (!(opts.r || opts["no-file-warnings"])) stderr(prReadError(file, error));
failed = true;
continue;
}
if (text.length === 0) continue;
if (hasPrNonprintingRendering(opts)) text = renderPrNonprinting(text, opts);
if (expandTabs && columns === 1) text = tabSpec.char === "\t" ? expandPrTabs(text, tabSize) : expandPrInputTabs(text, tabSpec.char, tabSize);
else if (expandTabs && tabSpec.char !== "\t") text = translatePrInputTabs(text, tabSpec.char);
let pageHeader = null;
if (!omitHeader) {
const headerName = opts.h ?? opts.header ?? (file === "-" ? "" : file);
const headerDate = await prHeaderDate(file, opts);
pageHeader = (page) => formatPrPageHeader(formatPrHeader(headerDate, headerName, pageWidth, page), indent);
if (!paginatedBody) out += pageHeader(1);
}
const joinLines = opts.J || opts["join-lines"] || (oldSeparatorSpecified && !(opts.w ?? opts.width));
const effectiveExplicitPageWidth = effectivePrExplicitPageWidth(explicitPageWidth, joinLines, columns, pageWidth, opts);
const bodyColumnSeparator = joinLines && columns > 1 && !columnSeparatorSpecified ? "\t" : columnSeparator;
let rendered = renderPrBody(text, { across: opts.a || opts.across, columns, columnSeparator: bodyColumnSeparator, columnSeparatorSpecified, doubleSpace: opts.d || opts["double-space"], expandTabs, explicitPageWidth: effectiveExplicitPageWidth, formFeed, indent, joinLines, numberLines, numberSkippedLines: !firstLineNumberSpecified, pageHeader, pageLength: omitHeader && pageLengthText == null ? undefined : pageLength, pageRange, pageWidth, padExplicitSeparatorColumns: newSeparatorSpecified, separator, stripFormFeeds: opts.T || opts["omit-pagination"], tabSize, width, nextNumber: () => lineNumber++ });
if (rendered === "" && opts.pages != null && pageRange.first > 1) stderr(`pr: starting page number ${pageRange.first} exceeds page count 1\n`);
if (outputTabs) rendered = tabifyPrOutput(rendered, outputTabSize);
out += rendered;
if (formFeed && !omitHeader && !paginatedBody) out += "\f";
if (rendered !== "" && !out.endsWith("\n") && !out.endsWith("\f")) out += "\n";
}
stdout(out);
return failed ? 1 : 0;
}
export function prReadError(file, error) {
return file === "-" ? `pr: ${nodeErrorMessage(error)}\n` : `pr: ${textInputDiagnosticName(file)}: ${systemErrorMessage(error)}\n`;
}
export function hasPrNonprintingRendering(opts) {
return Boolean(opts.c || opts["show-control-chars"] || opts.v || opts["show-nonprinting"]);
}
export function renderPrNonprinting(text, opts) {
const octal = opts.v || opts["show-nonprinting"];
let out = "";
for (const ch of text) {
const code = ch.charCodeAt(0);
if (ch === "\n" || ch === "\t" || ch === "\f") out += ch;
else if (code >= 0xdc80 && code <= 0xdcff) out += `\\${(code - 0xdc00).toString(8).padStart(3, "0")}`;
else if (octal && (code < 32 || code === 127)) out += `\\${code.toString(8).padStart(3, "0")}`;
else if (code < 32) out += `^${String.fromCharCode(code + 64)}`;
else if (code === 127) out += "^?";
else out += ch;
}
return out;
}
export function normalizePrColumnArgs(args) {
const normalized = [];
const valueOptions = new Set(["-w", "-W", "-l", "-h", "-N", "-o", "-D", "--width", "--page-width", "--length", "--header", "--first-line-number", "--indent", "--date-format", "--pages", "--columns"]);
for (let i = 0; i < args.length; i++) {
const arg = args[i];
normalized.push(arg);
if (valueOptions.has(arg)) {
if (i + 1 < args.length) normalized.push(args[++i]);
continue;
}
const compactColumnMatch = String(arg).match(/^-(t|T)(\d+)$/);
if (compactColumnMatch) {
normalized.pop();
normalized.push(`-${compactColumnMatch[1]}`, `--columns=${compactColumnMatch[2]}`);
continue;
}
const compactObsoleteMatch = String(arg).match(/^-([1-9]\d*)(\D.+|[^\d])$/);
if (compactObsoleteMatch) {
const [, columnText, rest] = compactObsoleteMatch;
const columns = Number(columnText);
if (!Number.isSafeInteger(columns)) throw new UsageError(`invalid number of columns: ${localeQuotedEscapedDiagnostic(columnText)}: Value too large for defined data type`);
if (columns < 1) throw new UsageError(`invalid number of columns: ${localeQuotedEscapedDiagnostic(columnText)}: Numerical result out of range`);
normalized.pop();
normalized.push(`--columns=${columnText}`, `-${rest}`);
continue;
}
const match = String(arg).match(/^-(\d+)$/);
if (!match) {
const pageMatch = String(arg).match(/^\+(.+)$/);
if (pageMatch) normalized[normalized.length - 1] = `--pages=${pageMatch[1]}`;
continue;
}
const columnText = match[1];
const columns = Number(columnText);
if (!Number.isSafeInteger(columns)) throw new UsageError(`invalid number of columns: ${localeQuotedEscapedDiagnostic(columnText)}: Value too large for defined data type`);
if (columns < 1) throw new UsageError(`invalid number of columns: ${localeQuotedEscapedDiagnostic(columnText)}: Numerical result out of range`);
normalized[normalized.length - 1] = `--columns=${columnText}`;
}
return normalized;
}
export function parsePrPositiveInteger(value, label, unit) {
const text = String(value);
if (!/^-?\d+$/.test(text)) throw new UsageError(`'${label}' invalid number of ${unit}: ${localeQuotedEscapedDiagnostic(text)}`);
const n = Number(text);
if (!Number.isSafeInteger(n)) throw new UsageError(`'${label}' invalid number of ${unit}: ${localeQuotedEscapedDiagnostic(text)}: Value too large for defined data type`);
if (n < 1) throw new UsageError(`'${label}' invalid number of ${unit}: ${localeQuotedEscapedDiagnostic(text)}: Numerical result out of range`);
return n;
}
export function parsePrNonnegativeInteger(value, label, unit) {
const text = String(value);
if (!/^[+-]?\d+$/.test(text)) throw new UsageError(`'${label}' invalid ${unit}: ${localeQuotedEscapedDiagnostic(text)}`);
const n = BigInt(text);
if (n < 0n || n > BigInt(Number.MAX_SAFE_INTEGER)) throw new UsageError(`'${label}' invalid ${unit}: ${localeQuotedEscapedDiagnostic(text)}: Value too large for defined data type`);
return Number(n);
}
export function parsePrLineNumber(value) {
const text = String(value);
if (!/^[+-]?\d+$/.test(text)) throw new UsageError(`'-N NUMBER' invalid starting line number: ${localeQuotedEscapedDiagnostic(text)}`);
const n = BigInt(text);
if (n < -2147483648n || n > 2147483647n) throw new UsageError(`'-N NUMBER' invalid starting line number: ${localeQuotedEscapedDiagnostic(text)}: Value too large for defined data type`);
return Number(n);
}
export function parsePrColumnCount(value) {
if (value == null) return 1;
const text = String(value);
if (!/^[+-]?\d+$/.test(text)) throw new UsageError(`invalid number of columns: ${localeQuotedEscapedDiagnostic(text)}`);
const columns = BigInt(text.replace(/^\+/, ""));
if (columns < 1n) throw new UsageError(`invalid number of columns: ${localeQuotedEscapedDiagnostic(text)}: Numerical result out of range`);
if (columns > BigInt(Number.MAX_SAFE_INTEGER)) throw new UsageError(`invalid number of columns: ${localeQuotedEscapedDiagnostic(text)}: Value too large for defined data type`);
return Number(columns);
}
export function validatePrPages(pages) {
if (pages == null) return;
const text = String(pages);
if (/^\d+[A-Za-z]+$/.test(text) && text.length > 30) throw new UsageError(`invalid suffix in --pages argument '${text}'`);
if (/^\d+$/.test(text) && text.length > 30) throw new UsageError(`--pages argument '${text}' too large`);
if (/^[A-Za-z]+$/.test(text)) throw new UsageError(`invalid --pages argument '${text}'`);
if (/^\d+[A-Za-z]/.test(text) || (/^\d/.test(text) && /[\x00-\x1f\x7f]/.test(text))) throw new UsageError(`invalid page range ${localeQuotedEscapedDiagnostic(text)}`);
if (!/^\d+([:-]\d+)?$/.test(text)) throw new UsageError(`invalid --pages argument '${text}'`);
}
export function parsePrPageRange(pages) {
if (pages == null) return { first: 1, last: Infinity };
const [firstText, lastText] = String(pages).split(/[:-]/, 2);
return { first: Number(firstText), last: lastText === undefined ? Infinity : Number(lastText) };
}
export function parsePrTabSize(spec) {
if (spec === undefined || spec === true || spec === "") return 8;
const text = String(spec);
const match = text.match(/(\d+)$/);
const size = Number(match?.[1] ?? 8);
return Number.isSafeInteger(size) && size > 0 ? size : 8;
}
export function parsePrTabSpec(spec) {
const size = parsePrTabSize(spec);
if (spec === undefined || spec === true || spec === "") return { char: "\t", size };
const text = String(spec);
const match = text.match(/^(.*?)(\d+)$/s);
const marker = match ? match[1] : text;
return { char: marker ? marker[0] : "\t", size };
}
export function translatePrInputTabs(text, marker) {
return text.replaceAll(marker, "\t");
}
export function expandPrInputTabs(text, marker, markerTabSize) {
let out = "";
let column = 0;
for (const ch of text) {
if (ch === marker || ch === "\t") {
const size = ch === marker ? markerTabSize : 8;
const spaces = size - (column % size);
out += " ".repeat(spaces);
column += spaces;
} else if (ch === "\b") {
if (column > 0) {
out += ch;
column--;
}
} else {
out += ch;
if (ch === "\n" || ch === "\f") column = 0;
else column++;
}
}
return out;
}
export function expandPrTabs(text, tabSize) {
let out = "";
let column = 0;
for (const ch of text) {
if (ch === "\t") {
const spaces = tabSize - (column % tabSize);
out += " ".repeat(spaces);
column += spaces;
} else if (ch === "\b") {
if (column > 0) {
out += ch;
column--;
}
} else {
out += ch;
if (ch === "\n" || ch === "\f") column = 0;
else column++;
}
}
return out;
}
export function tabifyPrOutput(text, tabSize) {
let out = "";
let column = 0;
let pendingSpaces = 0;
const flushSpaces = (nextColumn = column + pendingSpaces) => {
while (pendingSpaces > 0) {
const toStop = tabSize - (column % tabSize);
if (pendingSpaces >= toStop && toStop > 1) {
out += "\t";
column += toStop;
pendingSpaces -= toStop;
} else {
out += " ";
column++;
pendingSpaces--;
}
}
column = nextColumn;
};
for (const ch of text) {
if (ch === " ") {
pendingSpaces++;
continue;
}
flushSpaces();
out += ch;
if (ch === "\t") column += tabSize - (column % tabSize);
else if (ch === "\b") column = Math.max(0, column - 1);
else if (ch === "\n" || ch === "\f") column = 0;
else column++;
}
flushSpaces();
return out;
}
export async function prHeaderDate(file, opts) {
const format = opts.D ?? opts["date-format"];
if (!format) {
const date = file === "-" ? new Date() : (await stat(file)).mtime;
return strftime(date, "%Y-%m-%d %H:%M");
}
const date = file === "-" ? new Date() : (await stat(file)).mtime;
return format.startsWith("+") ? `+${strftime(date, format.slice(1))}` : strftime(date, format);
}
export function formatPrHeader(dateText, name, pageWidth = 72, page = 1) {
const pageText = `Page ${page}`;
if (pageWidth < dateText.length + String(name).length + pageText.length + 2) return `${dateText} ${name} ${pageText}`;
const nameColumn = dateText.length + Math.floor((pageWidth - pageText.length - dateText.length - String(name).length) / 2);
return `${dateText.padEnd(nameColumn)}${String(name).padEnd(Math.max(1, pageWidth - pageText.length - nameColumn))}${pageText}`;
}
export function formatPrPageHeader(headerLine, indent = 0) {
const margin = " ".repeat(indent);
return indent > 0 ? `${margin}\n\n${margin}${headerLine}\n\n\n` : `\n\n${headerLine}\n\n\n`;
}
export function parsePrNumberSpec(spec) {
if (spec === undefined || spec === true) return { separator: "\t", width: 5 };
const text = String(spec);
const match = text.match(/^(.*?)(\d+)?$/s);
return { separator: match?.[1] || "\t", width: Number(match?.[2] || 5) };
}
export function renderPrBody(text, opts) {
if (opts.columns > 1 && text.includes("\f") && !(opts.pageLength != null && opts.pageHeader)) return renderPrColumnGroups(text, opts);
if (opts.pageLength != null && opts.pageHeader) return opts.columns > 1 ? renderPrPaginatedColumns(text, opts) : renderPrPaginatedSingleColumn(text, opts);
const margin = " ".repeat(opts.indent);
const lines = text.split(/(?<=\n)/).filter(Boolean).map((raw) => {
let line = raw.endsWith("\n") ? raw.slice(0, -1) : raw;
if (opts.stripFormFeeds) {
if (/^\f+$/u.test(line)) return null;
line = line.replace(/\f+/gu, "\n");
}
return opts.explicitPageWidth ? line.slice(0, opts.pageWidth) : line;
}).filter((line) => line != null);
const rendered = opts.columns > 1 ? renderPrColumns(lines, opts) : renderPrSingleColumn(lines, opts);
return opts.doubleSpace ? rendered.replaceAll("\n", "\n\n") : rendered;
}
export function effectivePrExplicitPageWidth(explicitPageWidth, joinLines, columns, pageWidth, opts) {
const modernPageWidth = opts.W !== undefined || opts["page-width"] !== undefined;
return explicitPageWidth && !joinLines && (columns > 1 || pageWidth < 72 || modernPageWidth);
}
export function renderPrColumnGroups(text, opts) {
const tokens = tokenizePrPages(text, opts);
const groups = [[]];
for (const token of tokens) {
if (token.type === "formFeed") groups.push([]);
else groups[groups.length - 1].push(token.line);
}
let out = "";
for (let i = 0; i < groups.length; i++) {
if (groups[i].length > 0) out += renderPrColumns(groups[i], opts);
if (!opts.stripFormFeeds && i < groups.length - 1) out += "\f";
}
return opts.doubleSpace ? out.replaceAll("\n", "\n\n") : out;
}
export function renderPrSingleColumn(lines, opts) {
const margin = " ".repeat(opts.indent);
return lines.map((line) => {
if (/^\f+$/u.test(line)) return opts.stripFormFeeds ? "" : line;
const number = opts.numberLines ? `${formatPrLineNumber(opts.nextNumber(), opts.width)}${opts.separator}` : "";
if (!opts.stripFormFeeds && line.includes("\f")) {
const rendered = line.replace(/([^\f])(\f+)/gu, "$1\n$2");
return `${margin}${number}${rendered}${line.endsWith("\f") ? "" : "\n"}`;
}
if (opts.stripFormFeeds && line.endsWith("\n")) return `${margin}${number}${line}`;
return `${margin}${number}${line}\n`;
}).join("");
}
export function renderPrPaginatedSingleColumn(text, opts) {
let out = "";
const state = createPrPaginatedSingleColumnState(opts, (text) => { out += text; });
for (const token of tokenizePrPages(text, opts)) state.accept(token);
state.finish();
return out;
}
export function streamPrPaginatedSingleColumnFd(fd, opts) {
const state = createPrPaginatedSingleColumnState(opts, stdout);
const buffer = Buffer.allocUnsafe(64 * 1024);
if (!opts.explicitPageWidth) {
let lineHasText = false;
let skipNextNewline = false;
while (true) {
const n = readSync(fd, buffer, 0, buffer.length, null);
if (n === 0) break;
const bytes = buffer.subarray(0, n);
let start = 0;
for (let index = 0; index < bytes.length; index++) {
const byte = bytes[index];
if (byte !== 0x0a && byte !== 0x0c) continue;
if (index > start) {
state.writeLineChunk(bytes.subarray(start, index));
lineHasText = true;
skipNextNewline = false;
}
if (byte === 0x0a) {
if (skipNextNewline) skipNextNewline = false;
else state.endLine();
lineHasText = false;
} else {
if (lineHasText) state.endLine();
lineHasText = false;
state.accept({ type: "formFeed" });
skipNextNewline = true;
}
start = index + 1;
}
if (start < bytes.length) {
state.writeLineChunk(bytes.subarray(start));
lineHasText = true;
skipNextNewline = false;
}
}
if (lineHasText) state.endLine();
state.finish();
return;
}
let pending = "";
let skipNextNewline = false;
const pushLine = (line) => state.accept({ type: "line", line: opts.explicitPageWidth ? line.slice(0, opts.pageWidth) : line });
const processText = (text, final = false) => {
let line = pending;
pending = "";
for (let i = 0; i < text.length; i++) {
const ch = text[i];
if (ch === "\n") {
if (skipNextNewline) {
skipNextNewline = false;
continue;
}
pushLine(line);
line = "";
} else if (ch === "\f") {
if (line.length > 0) pushLine(line);
line = "";
state.accept({ type: "formFeed" });
skipNextNewline = true;
} else {
skipNextNewline = false;
line += ch;
}
}
if (final) {
if (line.length > 0) pushLine(line);
} else {
pending = line;
}
};
while (true) {
const n = readSync(fd, buffer, 0, buffer.length, null);
if (n === 0) break;
processText(decodeSurrogateEscapedBytes(buffer.subarray(0, n)));
}
processText("", true);
state.finish();
}
export function createPrPaginatedSingleColumnState(opts, emit) {
const bodyRows = Math.max(1, opts.pageLength - 10);
const margin = " ".repeat(opts.indent);
let page = 1;
let row = 0;
let pageStarted = false;
let lineOpen = false;
const inRange = () => page >= opts.pageRange.first && page <= opts.pageRange.last;
const startPage = () => {
if (!pageStarted && inRange()) emit(opts.pageHeader(page));
pageStarted = true;
};
const endPage = () => {
const renderedRows = opts.doubleSpace ? row * 2 : row;
if (pageStarted && inRange()) emit(opts.formFeed ? "\f" : "\n".repeat(Math.max(1, opts.pageLength - 5 - renderedRows)));
page++;
row = 0;
pageStarted = false;
};
const startLine = () => {
if (lineOpen || page > opts.pageRange.last) return;
if (row >= bodyRows) endPage();
startPage();
let number = "";
if (opts.numberLines && (inRange() || opts.numberSkippedLines)) number = `${formatPrLineNumber(opts.nextNumber(), opts.width)}${opts.separator}`;
if (inRange()) emit(`${margin}${number}`);
lineOpen = true;
};
const writeLineChunk = (text) => {
if (!text.length) return;
startLine();
if (lineOpen && inRange()) emit(text);
};
const endLine = () => {
startLine();
if (!lineOpen) return;
if (inRange()) emit(opts.doubleSpace ? "\n\n" : "\n");
row++;
lineOpen = false;
};
return {
writeLineChunk,
endLine,
accept(token) {
if (page > opts.pageRange.last) return;
if (token.type === "formFeed") {
if (lineOpen) endLine();
startPage();
if (opts.formFeed && inRange() && row === 0) emit("\n");
if (row === 0 && opts.formFeed) row = 1;
endPage();
return;
}
writeLineChunk(token.line);
endLine();
},
finish() {
if (lineOpen) endLine();
if (pageStarted) endPage();
},
};
}
export function renderPrPaginatedColumns(text, opts) {
const bodyRows = Math.max(1, opts.pageLength - 10);
const pageCapacity = Math.max(1, bodyRows * opts.columns);
const tokens = tokenizePrPages(text, opts);
const separatorWidth = opts.columnSeparatorSpecified ? opts.columnSeparator.length * (opts.columns - 1) : 0;
const columnWidth = Math.max(1, Math.floor((opts.pageWidth - separatorWidth) / opts.columns));
const clipWidth = opts.joinLines ? Infinity : Math.max(1, columnWidth - (!opts.columnSeparatorSpecified && (!opts.explicitPageWidth || opts.columns === 2) ? 1 : 0));
const margin = " ".repeat(opts.indent);
let out = "";
let page = 1;
let entries = [];
const inRange = () => page >= opts.pageRange.first && page <= opts.pageRange.last;
const makeEntry = (line) => {
const number = opts.numberLines && (inRange() || opts.numberSkippedLines) ? opts.nextNumber() : null;
return { line, number };
};
const emitPage = (forceBlankBody = false) => {
if (inRange()) {
out += opts.pageHeader(page);
if (forceBlankBody) out += "\n";
else out += renderPrColumnPage(entries, { ...opts, bodyRows, clipWidth, columnWidth, margin });
const renderedRows = forceBlankBody ? 1 : prColumnPageRowCount(entries.length, opts.columns, bodyRows, opts.across);
out += opts.formFeed ? "\f" : "\n".repeat(Math.max(1, opts.pageLength - 5 - renderedRows));
}
page++;
entries = [];
};
for (const token of tokens) {
if (page > opts.pageRange.last) break;
if (token.type === "formFeed") {
emitPage(opts.formFeed && entries.length === 0);
continue;
}
if (entries.length >= pageCapacity) emitPage();
entries.push(makeEntry(token.line));
}
if (entries.length > 0) emitPage();
return opts.doubleSpace ? out.replaceAll("\n", "\n\n") : out;
}
export function renderPrColumnPage(entries, opts) {
const rows = prColumnPageRowCount(entries.length, opts.columns, opts.bodyRows, opts.across);
const out = [];
for (let row = 0; row < rows; row++) {
const parts = [];
for (let column = 0; column < opts.columns; column++) {
const index = opts.across ? row * opts.columns + column : prDownColumnIndex(entries.length, opts.columns, rows, row, column);
const entry = entries[index];
if (entry === undefined) continue;
const entryClipWidth = opts.numberLines && column > 0 && !opts.columnSeparatorSpecified ? Math.max(1, opts.clipWidth - 1) : opts.clipWidth;
const implicitSeparatorWidth = opts.columnSeparator.length;
const explicitImplicitSeparatorWidth = !opts.columnSeparatorSpecified && opts.columns === 2 ? 0 : implicitSeparatorWidth;
const clipStartColumn = opts.margin.length + (opts.explicitPageWidth || opts.columnSeparatorSpecified ? column * (opts.columnWidth + explicitImplicitSeparatorWidth) : Math.floor((opts.pageWidth * column) / opts.columns));
let clipped = formatPrColumnEntry(entry, column, { ...opts, compactColumnNumbers: true, paginated: true, clipStartColumn }, entryClipWidth);
if (opts.numberLines && opts.expandTabs && !opts.columnSeparatorSpecified && column < opts.columns - 1) clipped = `${clipped} \t`;
parts.push(opts.joinLines || column === opts.columns - 1 || (opts.numberLines && opts.expandTabs && !opts.columnSeparatorSpecified)
? clipped
: opts.columnSeparatorSpecified && opts.padExplicitSeparatorColumns
? padPrColumnEnd(clipped.replace(/\s+$/u, ""), column, { columnWidth: opts.columnWidth, clipWidth: opts.columnWidth, formFeed: opts.formFeed, startOffset: opts.margin.length, separatorWidth: opts.columnSeparator.length, allowUnitTabs: true, disallowSingleColumnTab: true })
: opts.columnSeparatorSpecified
? clipped
: (opts.numberLines ? padPrColumnEnd(clipped.replace(/\s+$/u, ""), column, { ...opts, padExtra: 1 }) : padPrDefaultColumnEnd(clipped.replace(/\s+$/u, ""), column, { ...opts, allowUnitTabs: opts.columns === 2 || opts.margin.length > 0, startOffset: opts.margin.length })));
}
let separator = !opts.columnSeparatorSpecified && !opts.joinLines && (!opts.explicitPageWidth || opts.columns === 2) ? "" : opts.columnSeparator;
if (opts.numberLines && opts.expandTabs && !opts.columnSeparatorSpecified) separator = "";
if (opts.numberLines && opts.expandTabs && opts.columnSeparatorSpecified) separator += "\t ";
if (parts.length) {
let line = parts.join(separator);
if (!opts.columnSeparatorSpecified && opts.joinLines && !opts.across) line = formatPrStoredJoinLine(parts, opts);
else if (!opts.columnSeparatorSpecified && opts.joinLines) line = tabifyPrSpacesAtGnuOutput(line, opts.margin.length);
else if (!opts.columnSeparatorSpecified && opts.explicitPageWidth) line = tabifyPrSpacesAt(line, opts.margin.length);
const preserveAcrossPadding = opts.across && row < rows - 1 && parts.slice(1).every((part) => !/\S/u.test(part));
out.push(`${opts.margin}${preserveAcrossPadding ? line : line.replace(/\s+$/u, "")}\n`);
}
}
return out.join("");
}
export function prColumnPageRowCount(entryCount, columns, bodyRows, across) {
if (entryCount <= 0) return 0;
if (across) return Math.ceil(entryCount / columns);
if (entryCount >= bodyRows * columns) return bodyRows;
return Math.ceil(entryCount / columns);
}
export function prDownColumnIndex(entryCount, columns, rows, row, column) {
const full = Math.floor(entryCount / columns);
const extra = entryCount % columns;
const height = full + (column < extra ? 1 : 0);
if (row >= height) return -1;
let offset = 0;
for (let c = 0; c < column; c++) offset += full + (c < extra ? 1 : 0);
return offset + row;
}
export function tokenizePrPages(text, opts) {
const tokens = [];
let line = "";
let skipNextNewline = false;
const pushLine = () => {
tokens.push({ type: "line", line: opts.explicitPageWidth ? line.slice(0, opts.pageWidth) : line });
line = "";
};
for (let i = 0; i < text.length; i++) {
const ch = text[i];
if (ch === "\n") {
if (skipNextNewline) {
skipNextNewline = false;
continue;
}
pushLine();
} else if (ch === "\f") {
if (line.length > 0) pushLine();
tokens.push({ type: "formFeed" });
skipNextNewline = true;
} else {
skipNextNewline = false;
line += ch;
}
}
if (line.length > 0) pushLine();
return tokens;
}
export function padPrColumnEnd(text, column, opts) {
const start = (opts.startOffset ?? 0) + column * opts.columnWidth + column * (opts.separatorWidth ?? 1);
const width = column === 0 ? opts.columnWidth : opts.clipWidth;
const target = start + width + (opts.padExtra ?? 0);
let out = text;
let display = prColumnAfter(text, start);
while (display < target) {
const toStop = 8 - (display % 8);
const remaining = target - display;
if ((toStop > 1 || !opts.formFeed || (opts.allowUnitTabs && !(opts.disallowSingleColumnTab && remaining === 1))) && display + toStop <= target) {
out += "\t";
display += toStop;
} else {
out += " ";
display++;
}
}
return out;
}
export function renderPrMerge(texts, opts) {
const files = texts.map((text) => {
if (opts.stripFormFeeds) text = text.replaceAll("\f", "");
return text.split(/(?<=\n)/).filter(Boolean).map((raw) => raw.endsWith("\n") ? raw.slice(0, -1) : raw);
});
if (opts.pageLength != null && opts.pageHeader) return renderPrPaginatedMerge(files, opts);
const rowCount = Math.max(0, ...files.map((lines) => lines.length));
const columnCount = opts.columnCount ?? files.length;
const columnSeparator = opts.defaultColumnSeparator && opts.padColumns && !opts.joinLines ? "" : opts.columnSeparator;
const separatorWidth = columnSeparator.length * Math.max(0, columnCount - 1);
const columnWidth = columnCount ? Math.max(1, Math.floor((opts.pageWidth - separatorWidth) / columnCount)) : opts.pageWidth;
const out = [];
for (let row = 0; row < rowCount; row++) {
const parts = [];
let column = opts.indent;
for (let index = 0; index < files.length; index++) {
if (index > 0) column += columnSeparator.length;
let value = files[index][row] ?? "";
if (!opts.joinLines && index > 0 && !opts.preserveMergeTabs) value = tabifyPrSpacesAt(expandPrTabs(value, 8), column, true);
if (!opts.joinLines && opts.padColumns && index < files.length - 1) value = padPrMergeEnd(value, columnWidth);
parts.push(value);
column = prColumnAfter(value, column);
}
const body = opts.joinLines ? formatPrMergeJoinLine(parts, opts.columnSeparator) : parts.join(columnSeparator);
out.push(`${" ".repeat(opts.indent)}${opts.joinLines ? body : body.slice(0, opts.pageWidth)}\n`);
}
return out.join("");
}
export function renderPrPaginatedMerge(files, opts) {
const bodyRows = Math.max(1, opts.pageLength - 10);
const pages = files.map((lines) => paginatePrMergeLines(lines, bodyRows));
const pageCount = Math.max(0, ...pages.map((filePages) => filePages.length));
const columnCount = opts.columnCount ?? files.length;
const separatorWidth = opts.columnSeparator.length * Math.max(0, columnCount - 1);
const numberWidth = opts.numberLines ? opts.width + opts.separator.length : 0;
const columnWidth = columnCount ? Math.max(1, Math.floor((opts.pageWidth - numberWidth - separatorWidth) / columnCount)) : opts.pageWidth;
let out = "";
for (let page = 1; page <= pageCount; page++) {
const pageRows = pages.map((filePages) => filePages[page - 1] ?? []);
const hasFirstFileRows = (pageRows[0] ?? []).some((line) => line !== null);
const visible = page >= opts.pageRange.first && page <= opts.pageRange.last;
if (!visible && opts.numberLines && opts.numberSkippedLines && hasFirstFileRows) {
for (let row = 0; row < bodyRows; row++) if ((pageRows[0] ?? [])[row] != null) opts.nextNumber();
continue;
}
if (!visible) continue;
out += opts.pageHeader(page);
let emittedRows = 0;
for (let row = 0; row < bodyRows; row++) {
const values = pageRows.map((rows) => rows[row]);
if (!values.some((value) => value != null)) continue;
const number = opts.numberLines ? `${formatPrLineNumber(opts.nextNumber(), opts.width)}${opts.separator}` : "";
if (opts.joinLines) {
const pageEndMissingRight = values[values.length - 1] == null
&& !pageRows.some((rows) => rows.slice(row + 1).some((line) => line != null));
const bodyValues = pageEndMissingRight && page < pageCount && (!opts.numberLines || values[0] != null) ? values.slice(0, values.reduce((last, value, index) => value == null ? last : index, -1) + 1) : values;
const body = formatPrMergeJoinLine(bodyValues, opts.columnSeparator, opts.indent + prDisplayWidth(number));
out += `${" ".repeat(opts.indent)}${number}${body}\n`;
emittedRows++;
continue;
}
const clippedParts = values.map((value, index) => {
const startColumn = opts.indent + numberWidth + index * (columnWidth + opts.columnSeparator.length);
const text = tabifyPrSpacesAt(prMergeCellText(value), startColumn, true);
return index === values.length - 1
? (opts.numberLines ? clipPrLastMergeColumn(text, columnWidth, startColumn, !opts.padColumns) : clipPrDisplay(text, columnWidth, startColumn).replace(/\s+$/u, ""))
: clipPrDisplay(text, columnWidth, startColumn);
});
const lastNonNull = values.reduce((last, value, index) => value == null ? last : index, -1);
const pageEndMissingRight = (!opts.padColumns || page === pageCount || values.some((value) => value?.beforeFormFeed || value?.sourceHadFormFeed))
&& values[values.length - 1] == null
&& !pageRows.some((rows) => rows.slice(row + 1).some((line) => line != null));
if (pageEndMissingRight && lastNonNull >= 0 && lastNonNull < values.length - 1) {
const startColumn = opts.indent + numberWidth + lastNonNull * (columnWidth + opts.columnSeparator.length);
const text = tabifyPrSpacesAt(prMergeCellText(values[lastNonNull]), startColumn, true);
clippedParts[lastNonNull] = opts.explicitPageWidth
? clipPrDisplay(text, columnWidth, startColumn)
: clipPrLastMergeColumn(text, columnWidth, startColumn, true);
const remainingText = text.slice(clippedParts[lastNonNull].length);
if (opts.explicitPageWidth && /^\S/u.test(remainingText)) clippedParts[lastNonNull] = clippedParts[lastNonNull].replace(/\s+$/u, "");
else if (opts.explicitPageWidth && remainingText.startsWith(" ")) clippedParts[lastNonNull] += " ";
if (!opts.explicitPageWidth && !values[lastNonNull]?.sourceHadFormFeed && !(opts.numberLines && values.some((value) => value?.beforeFormFeed || value?.sourceHadFormFeed)) && !/\s$/u.test(clippedParts[lastNonNull]) && /\S/u.test(text.slice(clippedParts[lastNonNull].length))) {
clippedParts[lastNonNull] += " ";
}
}
const unpadPageEnd = pageEndMissingRight
&& (values.some((value) => value?.beforeFormFeed) || prDisplayWidth(clippedParts[lastNonNull].replace(/\s+$/u, "")) >= columnWidth - 1);
const parts = clippedParts.map((clipped, index) => {
if (index >= values.length - 1 || (unpadPageEnd && index === lastNonNull)) return clipped;
return padPrColumnEnd(clipped.replace(/\s+$/u, ""), index, { columnWidth, clipWidth: columnWidth, formFeed: opts.formFeed, startOffset: opts.indent + numberWidth, separatorWidth: opts.columnSeparator.length, allowUnitTabs: true, disallowSingleColumnTab: opts.padColumns && (files.length > 2 || opts.numberLines) });
});
const body = (unpadPageEnd ? parts.slice(0, lastNonNull + 1) : parts).join(opts.columnSeparator);
out += `${" ".repeat(opts.indent)}${number}${body}\n`;
emittedRows++;
}
out += opts.formFeed ? `${emittedRows === 0 ? "\n" : ""}\f` : "\n".repeat(Math.max(1, opts.pageLength - 5 - emittedRows));
}
return out;
}
export function prMergeCellText(value) {
if (value == null) return "";
return typeof value === "object" ? value.line : String(value);
}
export function formatPrMergeJoinLine(values, separator, startColumn = 0) {
const parts = [];
let column = startColumn;
for (let index = 0; index < values.length; index++) {
let text = tabifyPrSpacesAt(prMergeCellText(values[index]), column, true);
if (index === values.length - 1) text = text.replace(/[ \t]+$/u, "");
parts.push(text);
column = prColumnAfter(text, column);
if (index < values.length - 1) column += separator.length;
}
return parts.join(separator);
}
export function clipPrLastMergeColumn(text, width, startColumn = 0, preserveSourceContinuation = false) {
const clipped = clipPrDisplay(text, width, startColumn);
if (!/\s$/u.test(clipped)) return clipped;
const lookahead = preserveSourceContinuation ? clipPrDisplay(text, width + 1, startColumn) : clipped;
if (lookahead.length > clipped.length && /\S/u.test(lookahead.slice(clipped.length))) return clipped;
return clipped.replace(/\s+$/u, "");
}
export function paginatePrMergeLines(lines, bodyRows) {
const pages = [];
let page = [];
let justFlushed = false;
for (const line of lines) {
const parts = String(line).split("\f");
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
let flushedFullPage = false;
if (part !== "" || !String(line).includes("\f")) {
page.push({ line: part, beforeFormFeed: false, sourceHadFormFeed: String(line).includes("\f") });
if (page.length >= bodyRows) {
pages.push(page);
page = [];
justFlushed = true;
flushedFullPage = true;
}
}
if (i < parts.length - 1) {
if (page.length > 0) page[page.length - 1].beforeFormFeed = true;
if (justFlushed) justFlushed = false;
else pages.push(page);
page = [];
}
if (part !== "" && !flushedFullPage) justFlushed = false;
}
}
if (page.length > 0) pages.push(page);
return pages;
}
export function renderPrColumns(lines, opts) {
const pageRows = opts.pageLength ?? Math.ceil(lines.length / opts.columns);
const pageCapacity = Math.max(1, pageRows * opts.columns);
const separatorWidth = opts.columnSeparator.length * (opts.columns - 1);
const columnWidth = Math.max(1, Math.floor((opts.pageWidth - separatorWidth) / opts.columns));
const clipWidth = opts.joinLines ? Infinity : columnWidth;
const out = [];
for (let pageStart = 0, page = 1; pageStart < lines.length; pageStart += pageCapacity, page++) {
if (page < opts.pageRange.first || page > opts.pageRange.last) continue;
const pageLines = lines.slice(pageStart, pageStart + pageCapacity).map((line) => ({ line, number: opts.numberLines ? opts.nextNumber() : null }));
const rows = opts.pageLength ?? Math.ceil(pageLines.length / opts.columns);
for (let row = 0; row < rows; row++) {
const parts = [];
for (let column = 0; column < opts.columns; column++) {
const index = opts.across ? row * opts.columns + column : prDownColumnIndex(pageLines.length, opts.columns, rows, row, column);
const entry = pageLines[index];
if (entry === undefined) continue;
let clipped = formatPrColumnEntry(entry, column, { ...opts, compactColumnNumbers: true }, clipWidth);
if (opts.numberLines && opts.expandTabs && !opts.columnSeparatorSpecified && column < opts.columns - 1) clipped = `${clipped} \t`;
parts.push(column === opts.columns - 1 || (opts.columnSeparatorSpecified && !opts.padExplicitSeparatorColumns) || (opts.numberLines && opts.expandTabs && !opts.columnSeparatorSpecified)
? clipped
: opts.columnSeparatorSpecified
? padPrColumnEnd(clipped.replace(/\s+$/u, ""), column, { columnWidth, clipWidth: columnWidth, formFeed: opts.formFeed, startOffset: opts.indent, separatorWidth: opts.columnSeparator.length, allowUnitTabs: true, disallowSingleColumnTab: true })
: padPrDefaultColumnEnd(clipped.replace(/\s+$/u, ""), column, { ...opts, columnWidth, allowUnitTabs: opts.allowUnitTabs || opts.numberLines, finalNumberBoundaryTab: opts.numberLines && !opts.columnSeparatorSpecified, padExtra: opts.numberLines && !opts.columnSeparatorSpecified ? 4 : opts.padExtra }));
}
let separator = !opts.columnSeparatorSpecified && !opts.explicitPageWidth ? "" : opts.columnSeparator;
if (opts.numberLines && opts.expandTabs && !opts.columnSeparatorSpecified) separator = "";
if (opts.numberLines && opts.expandTabs && opts.columnSeparatorSpecified) separator += "\t ";
if (parts.length) out.push(`${" ".repeat(opts.indent)}${parts.join(separator).replace(/\s+$/u, "")}\n`);
}
}
return out.join("");
}
export function padPrDefaultColumnEnd(text, column, opts) {
const implicitSeparatorWidth = opts.columnSeparatorSpecified ? 0 : opts.columnSeparator.length;
const implicitSeparatorTrim = opts.explicitPageWidth && !opts.columnSeparatorSpecified ? 1 : 0;
const startOffset = opts.startOffset ?? 0;
const target = opts.explicitPageWidth
? startOffset + (column + 1) * opts.columnWidth + column * implicitSeparatorWidth
: startOffset + Math.floor((opts.pageWidth * (column + 1)) / opts.columns) - implicitSeparatorTrim + (opts.padExtra ?? 0);
const start = startOffset + (opts.explicitPageWidth ? column * (opts.columnWidth + implicitSeparatorWidth) : Math.floor((opts.pageWidth * column) / opts.columns));
let out = text;
let display = prColumnAfter(text, start);
while (display < target) {
const toStop = 8 - (display % 8);
if (opts.finalNumberBoundaryTab && display % 8 === 0 && target - display === 8) {
out += " \t";
display += 8;
continue;
}
if ((toStop > 1 || opts.allowUnitTabs) && display + toStop <= target) {
out += "\t";
display += toStop;
} else {
out += " ";
display++;
}
}
return out;
}
export function formatPrColumnEntry(entry, column, opts, columnWidth) {
let text = entry.line;
if (!opts.numberLines) {
if (column > 0 && !opts.columnSeparatorSpecified) text = text.replaceAll("\t", "\t ");
if (opts.paginated && !opts.joinLines) text = tabifyPrSpacesAt(text, opts.clipStartColumn ?? 0, !opts.explicitPageWidth || opts.columnSeparatorSpecified);
return opts.paginated ? finishPrColumnClip(text, columnWidth, opts) : text.slice(0, columnWidth);
}
let clipWidth = columnWidth;
let prefix;
if (opts.expandTabs && opts.indent > 0 && !opts.columnSeparatorSpecified) {
return formatPrIndentedTabbedColumn(entry, column, opts, columnWidth);
}
if (opts.expandTabs && column > 0 && !opts.columnSeparatorSpecified) {
const number = String(entry.number);
prefix = opts.separator === "\t" ? `${number}${" ".repeat(Math.max(1, opts.width - number.length - 1))}` : `${number}${opts.separator}`;
clipWidth = Math.max(1, columnWidth - 4);
} else if (opts.expandTabs && column > 0 && opts.columnSeparatorSpecified) {
const number = String(entry.number);
prefix = `${number}${opts.separator}`;
clipWidth = Math.max(1, columnWidth - 2);
} else {
if (opts.compactColumnNumbers && column > 0 && !opts.columnSeparatorSpecified) {
const number = String(entry.number);
prefix = opts.separator === "\t" ? `${number}${" ".repeat(Math.max(1, opts.width - number.length - 1))}` : `${number}${opts.separator}`;
} else {
prefix = `${formatPrLineNumber(entry.number, opts.width)}${opts.separator}`;
}
}
text = `${prefix}${text}`;
if (opts.numberLines && opts.expandTabs && column > 0 && !opts.columnSeparatorSpecified) text = text.replace(/\t+/g, "\t ");
else if (opts.numberLines && opts.expandTabs && column > 0 && opts.columnSeparatorSpecified) text = text.replace(/\t+/g, "\t ");
if (opts.paginated && opts.columnSeparatorSpecified && !opts.joinLines) text = tabifyPrSpacesAt(text, opts.clipStartColumn ?? 0, true);
return finishPrColumnClip(text, clipWidth, opts);
}
export function finishPrColumnClip(text, width, opts) {
const startColumn = opts.clipStartColumn ?? 0;
const clipped = clipPrDisplay(text, width, startColumn);
if (!opts.columnSeparatorSpecified || !/\s$/u.test(clipped)) return clipped;
const lookahead = clipPrDisplay(text, width + 1, startColumn);
if (lookahead.length > clipped.length && /\S/u.test(lookahead.slice(clipped.length))) return clipped.replace(/\s+$/u, " ");
return clipped;
}
export function formatPrIndentedTabbedColumn(entry, column, opts, columnWidth) {
const number = String(entry.number);
const numberPrefix = column === 0
? `${formatPrLineNumber(entry.number, opts.width)} `
: `${" ".repeat(Math.max(1, opts.width - number.length - 1))}${number} `;
const body = formatPrIndentedTabbedBody(entry.line, column, opts.tabSize);
const clipWidth = Math.max(1, columnWidth + (column === 0 ? 3 : -1));
return clipPrDisplay(`${numberPrefix}${body}`, clipWidth);
}
export function formatPrIndentedTabbedBody(text, column, tabSize) {
const fields = text.split("\t");
if (fields.length >= 4 && fields[2] === "") {
const [first, second] = fields;
const rest = fields.slice(3).join("\t");
if (tabSize === 5) return column === 0 ? `${first}\t ${second}\t\t ${rest}` : `${first} ${second}\t ${rest}`;
return column === 0 ? `${first}\t ${second}\t ${rest}` : `${first} ${second}\t ${rest}`;
}
if (tabSize === 5) {
if (column === 0) return text.replace(/\t\t/g, "\t\t ").replace(/\t(?!\t)/g, "\t ");
return text.replace(/\t+/g, " ");
}
if (column === 0) return text.replace(/\t+/g, "\t ");
return text.replace(/\t+/g, " ");
}
export function clipPrDisplay(text, width, startColumn = 0) {
let out = "";
let column = startColumn;
const limit = startColumn + width;
for (const ch of text) {
const nextColumn = ch === "\t" ? column + (8 - (column % 8)) : ch === "\b" ? Math.max(0, column - 1) : column + 1;
if (nextColumn > limit) break;
out += ch;
column = nextColumn;
}
return out;
}
export function tabifyPrSpacesAt(text, startColumn = 0, allowUnitTabs = false) {
let out = "";
let column = startColumn;
for (let index = 0; index < text.length;) {
const ch = text[index];
if (ch !== " ") {
out += ch;
column = ch === "\t" ? column + (8 - (column % 8)) : ch === "\b" ? Math.max(0, column - 1) : column + 1;
index++;
continue;
}
let end = index;
while (end < text.length && text[end] === " ") end++;
let remaining = end - index;
while (remaining > 0) {
const toStop = 8 - (column % 8);
if ((toStop > 1 || (allowUnitTabs && remaining > 1)) && remaining >= toStop) {
out += "\t";
column += toStop;
remaining -= toStop;
} else {
out += " ";
column++;
remaining--;
}
}
index = end;
}
return out;
}
export function tabifyPrSpacesAtGnuOutput(text, startColumn = 0) {
let out = "";
let column = startColumn;
for (let index = 0; index < text.length;) {
const ch = text[index];
if (ch !== " ") {
out += ch;
column = ch === "\b" ? Math.max(0, column - 1) : column + 1;
index++;
continue;
}
let end = index;
while (end < text.length && text[end] === " ") end++;
let goal = column + end - index;
while (goal - column > 1) {
const next = column + (8 - (column % 8));
if (next > goal) break;
out += "\t";
column = next;
}
while (column < goal) {
out += " ";
column++;
}
index = end;
}
return out;
}
export function formatPrStoredJoinLine(parts, opts) {
let out = "";
let outputPosition = opts.margin.length;
let spacesNotPrinted = 0;
let separatorsNotPrinted = 0;
const separator = opts.columnSeparator;
const separatorLength = separator.length;
const printWhiteSpace = () => {
let current = outputPosition;
const goal = current + spacesNotPrinted;
while (goal - current > 1) {
const next = current + (8 - (current % 8));
if (next > goal) break;
out += "\t";
current = next;
}
while (current < goal) {
out += " ";
current++;
}
outputPosition = goal;
spacesNotPrinted = 0;
};
const printChar = (ch) => {
if (ch === " ") {
spacesNotPrinted++;
return;
}
if (spacesNotPrinted > 0) printWhiteSpace();
out += ch;
if (ch === "\b") outputPosition = Math.max(0, outputPosition - 1);
else if (isPrPrintable(ch)) outputPosition++;
};
const printSeparator = () => {
if (separatorsNotPrinted <= 0) {
if (spacesNotPrinted > 0) printWhiteSpace();
return;
}
for (; separatorsNotPrinted > 0; separatorsNotPrinted--) {
for (const ch of separator) {
if (ch === " ") {
spacesNotPrinted++;
continue;
}
if (spacesNotPrinted > 0) printWhiteSpace();
out += ch;
outputPosition++;
}
if (spacesNotPrinted > 0) printWhiteSpace();
}
};
parts.forEach((part, column) => {
const startPosition = column === 0 ? opts.margin.length + separatorLength : opts.margin.length;
if (separatorLength < startPosition) spacesNotPrinted = startPosition - separatorLength - outputPosition;
printSeparator();
for (const ch of part) printChar(ch);
if (spacesNotPrinted === 0) {
outputPosition = startPosition + prStoredEndPosition(part, opts.tabSize);
if (startPosition - separatorLength === opts.margin.length) outputPosition -= separatorLength;
}
separatorsNotPrinted++;
});
return out;
}
export function prStoredEndPosition(text, tabSize = 8) {
let position = 0;
for (const ch of text) {
if (ch === "\t") position += tabSize - (position % tabSize);
else if (ch === "\b") position = Math.max(0, position - 1);
else if (isPrPrintable(ch) || ch === " ") position++;
}
return position;
}
export function isPrPrintable(ch) {
const code = ch.codePointAt(0);
return code >= 0x20 && code !== 0x7f;
}
export function padPrMergeEnd(text, width) {
let out = text;
let displayWidth = prDisplayWidth(text);
while (displayWidth + (8 - (displayWidth % 8)) <= width) {
const spaces = 8 - (displayWidth % 8);
out += "\t";
displayWidth += spaces;
}
if (displayWidth < width) out += " ".repeat(width - displayWidth);
return out;
}
export function prDisplayWidth(text) {
let column = 0;
for (const ch of text) {
if (ch === "\t") column += 8 - (column % 8);
else if (ch === "\b") column = Math.max(0, column - 1);
else column++;
}
return column;
}
export function prColumnAfter(text, startColumn = 0) {
let column = startColumn;
for (const ch of text) {
if (ch === "\t") column += 8 - (column % 8);
else if (ch === "\b") column = Math.max(0, column - 1);
else column++;
}
return column;
}
export function formatPrLineNumber(value, width) {
const text = String(value);
const clipped = text.length > width ? text.slice(-width) : text;
return clipped.padStart(width);
}
const singleCall = defineCommand("pr", prCmd, prMetaOption);
export default singleCall;
if (import.meta.main) await runAsMain(singleCall);
|