@kaynooo/cli-view 0.6.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/view.js +189 -35
- package/package.json +2 -2
package/dist/view.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
4
|
// src/view.ts
|
|
5
|
-
import
|
|
5
|
+
import process17 from "process";
|
|
6
6
|
// ../kit/src/args.ts
|
|
7
7
|
import process10 from "process";
|
|
8
8
|
import { parseArgs } from "util";
|
|
@@ -358,6 +358,23 @@ function link(text, url, stream, env = process6.env) {
|
|
|
358
358
|
}
|
|
359
359
|
|
|
360
360
|
// ../kit/src/format.ts
|
|
361
|
+
var UNITS = [["d", 86400], ["h", 3600], ["m", 60], ["s", 1]];
|
|
362
|
+
function formatSeconds(total) {
|
|
363
|
+
let rest = Math.max(0, Math.round(total));
|
|
364
|
+
const amounts = UNITS.map(([unit, size]) => {
|
|
365
|
+
const amount = Math.floor(rest / size);
|
|
366
|
+
rest -= amount * size;
|
|
367
|
+
return `${amount}${unit}`;
|
|
368
|
+
});
|
|
369
|
+
const first = amounts.findIndex((amount) => !amount.startsWith("0"));
|
|
370
|
+
if (first === -1)
|
|
371
|
+
return "0s";
|
|
372
|
+
const next = amounts[first + 1];
|
|
373
|
+
return next === undefined || next.startsWith("0") ? amounts[first] ?? "0s" : `${amounts[first]} ${next}`;
|
|
374
|
+
}
|
|
375
|
+
function formatDuration(ms) {
|
|
376
|
+
return ms < 60000 ? `${(Math.max(0, ms) / 1000).toFixed(1)}s` : formatSeconds(ms / 1000);
|
|
377
|
+
}
|
|
361
378
|
function upTo(text, separator) {
|
|
362
379
|
const end = text.indexOf(separator);
|
|
363
380
|
return end === -1 ? text : text.slice(0, end);
|
|
@@ -375,7 +392,7 @@ function printJson(value) {
|
|
|
375
392
|
|
|
376
393
|
// ../kit/src/help.ts
|
|
377
394
|
import process8 from "process";
|
|
378
|
-
var OPTIONAL =
|
|
395
|
+
var OPTIONAL = /(?<!\x1B)\[[^\]]+\]/g;
|
|
379
396
|
var USAGE_PREFIX = /^usage:\s*/i;
|
|
380
397
|
var FULL_STOP = /\.$/;
|
|
381
398
|
function usageLine(usage, colors = paint(process8.stdout)) {
|
|
@@ -414,32 +431,44 @@ function exampleRows(examples, colors = paint(process8.stdout)) {
|
|
|
414
431
|
return [];
|
|
415
432
|
return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
|
|
416
433
|
}
|
|
434
|
+
var MAX_INLINE_CHOICES = 24;
|
|
417
435
|
function optionRows(options, colors = paint(process8.stdout)) {
|
|
418
436
|
const rows = Object.entries(options).map(([name, option]) => {
|
|
419
437
|
const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
|
|
420
|
-
const
|
|
438
|
+
const choices = option.choices?.join("|");
|
|
439
|
+
const inline = choices !== undefined && choices.length <= MAX_INLINE_CHOICES;
|
|
440
|
+
const named = inline ? `<${choices}>` : "<value>";
|
|
421
441
|
const placeholder = option.optional === undefined ? named : `[${named}]`;
|
|
422
442
|
const value = option.type === "string" ? ` ${colors.dim(placeholder)}` : "";
|
|
423
|
-
|
|
443
|
+
const described = choices === undefined || inline ? option.description ?? "" : [option.description, `one of ${option.choices.join(", ")}`].filter(Boolean).join("; ");
|
|
444
|
+
return { flag: `${short}${colors.cyan(`--${name}`)}${value}`, width: `-x, --${name}`.length + (option.type === "string" ? placeholder.length + 1 : 0), description: described };
|
|
424
445
|
});
|
|
425
446
|
const width = Math.max(0, ...rows.map((row) => row.width));
|
|
426
447
|
return rows.map((row) => row.description === "" ? ` ${row.flag}` : ` ${row.flag}${" ".repeat(width - row.width)} ${colors.dim(row.description)}`);
|
|
427
448
|
}
|
|
428
449
|
|
|
429
450
|
// ../kit/src/text.ts
|
|
430
|
-
var ANSI =
|
|
451
|
+
var ANSI = /\x1B(?:\[[\x30-\x3F]*[\x20-\x2F]*[\x40-\x7E]|\][^\x07\x1B]*(?:\x07|\x1B\\)|[\x40-\x5A\x5C-\x5F])/g;
|
|
431
452
|
function stripAnsi(text) {
|
|
432
453
|
return text.replace(ANSI, "");
|
|
433
454
|
}
|
|
434
455
|
function distance(a, b) {
|
|
435
|
-
|
|
436
|
-
|
|
456
|
+
const left = [...a];
|
|
457
|
+
const right = [...b];
|
|
458
|
+
let before = [];
|
|
459
|
+
let previous = Array.from({ length: right.length + 1 }, (_, index) => index);
|
|
460
|
+
for (const [i, x] of left.entries()) {
|
|
437
461
|
const current = [i + 1];
|
|
438
|
-
for (const [j,
|
|
439
|
-
|
|
462
|
+
for (const [j, y] of right.entries()) {
|
|
463
|
+
let cost = Math.min((previous[j + 1] ?? 0) + 1, (current[j] ?? 0) + 1, (previous[j] ?? 0) + (x === y ? 0 : 1));
|
|
464
|
+
if (i > 0 && j > 0 && x === right[j - 1] && left[i - 1] === y)
|
|
465
|
+
cost = Math.min(cost, (before[j - 1] ?? 0) + 1);
|
|
466
|
+
current.push(cost);
|
|
467
|
+
}
|
|
468
|
+
before = previous;
|
|
440
469
|
previous = current;
|
|
441
470
|
}
|
|
442
|
-
return previous[
|
|
471
|
+
return previous[right.length] ?? 0;
|
|
443
472
|
}
|
|
444
473
|
function closest(name, names) {
|
|
445
474
|
const started = names.find((known) => known.startsWith(name));
|
|
@@ -720,13 +749,31 @@ var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
|
720
749
|
// ../kit/src/cli.ts
|
|
721
750
|
import { readFileSync as readFileSync3, realpathSync as realpathSync2 } from "fs";
|
|
722
751
|
import { dirname as dirname3, join as join5 } from "path";
|
|
723
|
-
import
|
|
752
|
+
import process14 from "process";
|
|
753
|
+
|
|
754
|
+
// ../kit/src/notify.ts
|
|
755
|
+
import process12 from "process";
|
|
756
|
+
var NOTIFY_AFTER_MS = 1e4;
|
|
757
|
+
var CANCELLED = 130;
|
|
758
|
+
function base64(text) {
|
|
759
|
+
return Buffer.from(text).toString("base64");
|
|
760
|
+
}
|
|
761
|
+
function notification(title, body, env = process12.env) {
|
|
762
|
+
const id = `kit${process12.pid}`;
|
|
763
|
+
const codes = [`\x1B]99;i=${id}:d=0:o=unfocused:e=1;${base64(title)}\x1B\\`, `\x1B]99;i=${id}:p=body:e=1;${base64(body)}\x1B\\`];
|
|
764
|
+
return codes.map((code) => env.TMUX === undefined || env.TMUX === "" ? code : `\x1BPtmux;${code.replaceAll("\x1B", "\x1B\x1B")}\x1B\\`).join("");
|
|
765
|
+
}
|
|
766
|
+
function notifyEnd(command, code, ms, { stream = process12.stderr, env = process12.env } = {}) {
|
|
767
|
+
if (ms < NOTIFY_AFTER_MS || code === CANCELLED || outputMode(stream, env) !== "terminal")
|
|
768
|
+
return;
|
|
769
|
+
stream.write(notification(command, code === 0 ? `done in ${formatDuration(ms)}` : `failed with exit ${code} after ${formatDuration(ms)}`, env));
|
|
770
|
+
}
|
|
724
771
|
|
|
725
772
|
// ../kit/src/usage.ts
|
|
726
773
|
import { appendFileSync, mkdirSync } from "fs";
|
|
727
774
|
import { homedir as homedir3 } from "os";
|
|
728
775
|
import { dirname as dirname2, isAbsolute, join as join4 } from "path";
|
|
729
|
-
import
|
|
776
|
+
import process13 from "process";
|
|
730
777
|
|
|
731
778
|
// ../kit/src/logs.ts
|
|
732
779
|
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
@@ -740,14 +787,14 @@ var usageError;
|
|
|
740
787
|
function noteUsageError(message) {
|
|
741
788
|
usageError = message;
|
|
742
789
|
}
|
|
743
|
-
function stateHome(env =
|
|
790
|
+
function stateHome(env = process13.env) {
|
|
744
791
|
const dir = env.XDG_STATE_HOME;
|
|
745
792
|
return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
|
|
746
793
|
}
|
|
747
|
-
function usagePath(env =
|
|
794
|
+
function usagePath(env = process13.env) {
|
|
748
795
|
return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
749
796
|
}
|
|
750
|
-
function usageRecorded(env =
|
|
797
|
+
function usageRecorded(env = process13.env) {
|
|
751
798
|
const off = env.KAYNOOO_CLI_NO_USAGE;
|
|
752
799
|
return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
|
|
753
800
|
}
|
|
@@ -761,7 +808,7 @@ function usageEntry(bin, command, argv, code, now = new Date) {
|
|
|
761
808
|
usageError = undefined;
|
|
762
809
|
return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
|
|
763
810
|
}
|
|
764
|
-
function recordUsage(entry, env =
|
|
811
|
+
function recordUsage(entry, env = process13.env) {
|
|
765
812
|
if (!usageRecorded(env))
|
|
766
813
|
return;
|
|
767
814
|
try {
|
|
@@ -779,8 +826,8 @@ var LEVEL_OPTIONS = {
|
|
|
779
826
|
};
|
|
780
827
|
var TOP_OPTIONS = { ...LEVEL_OPTIONS, version: { type: "boolean", short: "v", description: "print the version" } };
|
|
781
828
|
function printError(prefix, message, hint, cause) {
|
|
782
|
-
const colors = paint(
|
|
783
|
-
const styled = takesControlCodes(
|
|
829
|
+
const colors = paint(process14.stderr);
|
|
830
|
+
const styled = takesControlCodes(process14.stderr);
|
|
784
831
|
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
785
832
|
const under = (label, text, tint) => text === undefined ? "" : `
|
|
786
833
|
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
@@ -885,11 +932,13 @@ async function runMain({ name, version, run, redact, usage }, argv = scriptArgs(
|
|
|
885
932
|
return 0;
|
|
886
933
|
}
|
|
887
934
|
let code;
|
|
935
|
+
const started = performance.now();
|
|
888
936
|
try {
|
|
889
937
|
code = await run(argv);
|
|
890
938
|
} catch (error) {
|
|
891
939
|
code = reportThrown(name, error, redact, wantsJson(argv));
|
|
892
940
|
}
|
|
941
|
+
notifyEnd(name, code, performance.now() - started);
|
|
893
942
|
if (usage !== false) {
|
|
894
943
|
try {
|
|
895
944
|
recordUsage(usageEntry(name, undefined, argv, code));
|
|
@@ -1099,19 +1148,88 @@ function renderCode(source, name, theme, lineNumbers) {
|
|
|
1099
1148
|
var TRAILING_NEWLINE2 = /\n$/;
|
|
1100
1149
|
var HUNK = /^@@ -\d+(?:,(\d+))? \+\d+(?:,(\d+))? @@(.*)$/;
|
|
1101
1150
|
var FILE_HEADER = /^diff --git a\/(.*) b\/(.*)$/;
|
|
1151
|
+
var NEW_FILE = /^\+\+\+ (?:b\/)?([^\t]*)/;
|
|
1102
1152
|
var COMMIT = /^(commit )([0-9a-f]{7,40})(.*)$/;
|
|
1103
1153
|
var HUNK_LINE = new Set([" ", "+", "-", "\\"]);
|
|
1104
1154
|
var MARKERS = /^(?:--- |\+\+\+ )/;
|
|
1105
1155
|
var META = /^(?:index |old mode |new mode |deleted file mode |new file mode |similarity index |dissimilarity index |rename from |rename to |copy from |copy to |Binary files |GIT binary patch)/;
|
|
1106
1156
|
var COMMIT_META = /^(?:Author|Date|Merge|AuthorDate|Commit|CommitDate):/;
|
|
1157
|
+
var TOKEN = /\w+|\s+|[^\w\s]/g;
|
|
1158
|
+
var WORD_CHAR = /\w/;
|
|
1159
|
+
var ESCAPE2 = /\x1B\[([0-9;]*)m/g;
|
|
1160
|
+
var CLOSING = new Set(["0", "22", "23", "24", "27", "29", "39", "49"]);
|
|
1161
|
+
var MAX_TOKENS = 300;
|
|
1107
1162
|
function looksLikeDiff(text) {
|
|
1108
1163
|
const first = stripAnsi(text).split(`
|
|
1109
1164
|
`).find((line) => line.trim() !== "") ?? "";
|
|
1110
1165
|
return first.startsWith("diff --git ") || first.startsWith("--- ") || first.startsWith("@@ ") || first.startsWith("Index: ") || COMMIT.test(first);
|
|
1111
1166
|
}
|
|
1167
|
+
function changedChars(a, b) {
|
|
1168
|
+
const left = a.match(TOKEN) ?? [];
|
|
1169
|
+
const right = b.match(TOKEN) ?? [];
|
|
1170
|
+
if (left.length > MAX_TOKENS || right.length > MAX_TOKENS)
|
|
1171
|
+
return;
|
|
1172
|
+
const common = Array.from({ length: left.length + 1 }, () => Array.from({ length: right.length + 1 }).fill(0));
|
|
1173
|
+
for (let i = left.length - 1;i >= 0; i--) {
|
|
1174
|
+
for (let j = right.length - 1;j >= 0; j--)
|
|
1175
|
+
common[i][j] = left[i] === right[j] ? common[i + 1][j + 1] + 1 : Math.max(common[i + 1][j], common[i][j + 1]);
|
|
1176
|
+
}
|
|
1177
|
+
const flags = (tokens) => tokens.flatMap((token) => Array.from({ length: token.length }).fill(true));
|
|
1178
|
+
const changedLeft = flags(left);
|
|
1179
|
+
const changedRight = flags(right);
|
|
1180
|
+
let shared = false;
|
|
1181
|
+
let i = 0;
|
|
1182
|
+
let j = 0;
|
|
1183
|
+
let at = 0;
|
|
1184
|
+
let to = 0;
|
|
1185
|
+
while (i < left.length && j < right.length) {
|
|
1186
|
+
if (left[i] === right[j]) {
|
|
1187
|
+
shared ||= WORD_CHAR.test(left[i]);
|
|
1188
|
+
changedLeft.fill(false, at, at + left[i].length);
|
|
1189
|
+
changedRight.fill(false, to, to + right[j].length);
|
|
1190
|
+
at += left[i].length;
|
|
1191
|
+
to += right[j].length;
|
|
1192
|
+
i++;
|
|
1193
|
+
j++;
|
|
1194
|
+
} else if (common[i + 1][j] >= common[i][j + 1]) {
|
|
1195
|
+
at += left[i++].length;
|
|
1196
|
+
} else {
|
|
1197
|
+
to += right[j++].length;
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
return shared ? [changedLeft, changedRight] : undefined;
|
|
1201
|
+
}
|
|
1202
|
+
function paintContent({ mark, text, language }, theme, changed) {
|
|
1203
|
+
const tone = mark === "+" ? theme.added : theme.removed;
|
|
1204
|
+
const code = text.slice(1);
|
|
1205
|
+
const source = language === undefined ? code : highlight(code, language, theme);
|
|
1206
|
+
const cells = [{ char: mark, style: "", changed: false }];
|
|
1207
|
+
let style = "";
|
|
1208
|
+
let from = 0;
|
|
1209
|
+
for (const escape of source.matchAll(ESCAPE2)) {
|
|
1210
|
+
for (const char of source.slice(from, escape.index).split(""))
|
|
1211
|
+
cells.push({ char, style, changed: changed?.[cells.length - 1] ?? false });
|
|
1212
|
+
style = CLOSING.has(escape[1]) ? "" : style + escape[0];
|
|
1213
|
+
from = escape.index + escape[0].length;
|
|
1214
|
+
}
|
|
1215
|
+
for (const char of source.slice(from).split(""))
|
|
1216
|
+
cells.push({ char, style, changed: changed?.[cells.length - 1] ?? false });
|
|
1217
|
+
let out = "";
|
|
1218
|
+
for (let at = 0;at < cells.length; ) {
|
|
1219
|
+
let end = at + 1;
|
|
1220
|
+
while (end < cells.length && cells[end].style === cells[at].style && cells[end].changed === cells[at].changed)
|
|
1221
|
+
end++;
|
|
1222
|
+
const run = cells.slice(at, end).map((cell) => cell.char).join("");
|
|
1223
|
+
const shown = cells[at].style === "" ? tone(run) : `${cells[at].style}${run}\x1B[0m`;
|
|
1224
|
+
out += cells[at].changed ? theme.changed(shown) : shown;
|
|
1225
|
+
at = end;
|
|
1226
|
+
}
|
|
1227
|
+
return out;
|
|
1228
|
+
}
|
|
1112
1229
|
function renderDiff(text, theme) {
|
|
1113
1230
|
let oldLeft = 0;
|
|
1114
1231
|
let newLeft = 0;
|
|
1232
|
+
let language;
|
|
1115
1233
|
const lines = stripAnsi(text).replaceAll("\r", "").replace(TRAILING_NEWLINE2, "").split(`
|
|
1116
1234
|
`).map((line) => {
|
|
1117
1235
|
if ((oldLeft > 0 || newLeft > 0) && line !== "" && !HUNK_LINE.has(line[0] ?? ""))
|
|
@@ -1120,11 +1238,11 @@ function renderDiff(text, theme) {
|
|
|
1120
1238
|
const mark = line[0];
|
|
1121
1239
|
if (mark === "+") {
|
|
1122
1240
|
newLeft--;
|
|
1123
|
-
return
|
|
1241
|
+
return { mark, text: line, language };
|
|
1124
1242
|
}
|
|
1125
1243
|
if (mark === "-") {
|
|
1126
1244
|
oldLeft--;
|
|
1127
|
-
return
|
|
1245
|
+
return { mark, text: line, language };
|
|
1128
1246
|
}
|
|
1129
1247
|
if (mark === "\\")
|
|
1130
1248
|
return theme.dim(line);
|
|
@@ -1139,14 +1257,42 @@ function renderDiff(text, theme) {
|
|
|
1139
1257
|
return `${theme.accent(line.slice(0, line.length - (hunk[3] ?? "").length))}${theme.dim(hunk[3] ?? "")}`;
|
|
1140
1258
|
}
|
|
1141
1259
|
const file = FILE_HEADER.exec(line);
|
|
1142
|
-
if (file !== null)
|
|
1260
|
+
if (file !== null) {
|
|
1261
|
+
language = languageOf(file[2] ?? "");
|
|
1143
1262
|
return `${theme.dim("diff")} ${theme.bold(theme.path(file[2] ?? ""))}`;
|
|
1263
|
+
}
|
|
1144
1264
|
const commit = COMMIT.exec(line);
|
|
1145
1265
|
if (commit !== null)
|
|
1146
1266
|
return `${theme.dim(commit[1] ?? "")}${theme.id(commit[2] ?? "")}${theme.dim(commit[3] ?? "")}`;
|
|
1267
|
+
const target = NEW_FILE.exec(line)?.[1];
|
|
1268
|
+
if (target !== undefined && target !== "/dev/null")
|
|
1269
|
+
language = languageOf(target);
|
|
1147
1270
|
return MARKERS.test(line) || META.test(line) || COMMIT_META.test(line) ? theme.dim(line) : line;
|
|
1148
1271
|
});
|
|
1149
|
-
|
|
1272
|
+
const changes = new Map;
|
|
1273
|
+
for (let at = 0;at < lines.length; ) {
|
|
1274
|
+
const first = lines[at];
|
|
1275
|
+
const isRemoved = typeof first === "object" && first.mark === "-";
|
|
1276
|
+
if (!isRemoved) {
|
|
1277
|
+
at++;
|
|
1278
|
+
continue;
|
|
1279
|
+
}
|
|
1280
|
+
let split = at;
|
|
1281
|
+
while (typeof lines[split] === "object" && lines[split].mark === "-")
|
|
1282
|
+
split++;
|
|
1283
|
+
let end = split;
|
|
1284
|
+
while (typeof lines[end] === "object" && lines[end].mark === "+")
|
|
1285
|
+
end++;
|
|
1286
|
+
for (let pair = 0;pair < Math.min(split - at, end - split); pair++) {
|
|
1287
|
+
const flags = changedChars(lines[at + pair].text.slice(1), lines[split + pair].text.slice(1));
|
|
1288
|
+
if (flags !== undefined) {
|
|
1289
|
+
changes.set(at + pair, flags[0]);
|
|
1290
|
+
changes.set(split + pair, flags[1]);
|
|
1291
|
+
}
|
|
1292
|
+
}
|
|
1293
|
+
at = Math.max(end, at + 1);
|
|
1294
|
+
}
|
|
1295
|
+
return `${lines.map((line, index) => typeof line === "string" ? line : paintContent(line, theme, changes.get(index))).join(`
|
|
1150
1296
|
`)}
|
|
1151
1297
|
`;
|
|
1152
1298
|
}
|
|
@@ -1339,8 +1485,8 @@ ${theme.dim("\u2500".repeat(rule))}
|
|
|
1339
1485
|
}
|
|
1340
1486
|
|
|
1341
1487
|
// src/theme.ts
|
|
1342
|
-
import
|
|
1343
|
-
function themeFor(stream =
|
|
1488
|
+
import process16 from "process";
|
|
1489
|
+
function themeFor(stream = process16.stdout, env = process16.env) {
|
|
1344
1490
|
const colors = paint(stream, env);
|
|
1345
1491
|
const accent = role("accent", stream, env);
|
|
1346
1492
|
const path = role("path", stream, env);
|
|
@@ -1353,6 +1499,7 @@ function themeFor(stream = process15.stdout, env = process15.env) {
|
|
|
1353
1499
|
bold: colors.bold,
|
|
1354
1500
|
italic: colors.italic,
|
|
1355
1501
|
strike: colors.strikethrough,
|
|
1502
|
+
changed: colors.inverse,
|
|
1356
1503
|
added: colors.green,
|
|
1357
1504
|
removed: colors.red,
|
|
1358
1505
|
accent,
|
|
@@ -1370,8 +1517,8 @@ var MARKDOWN_FILE = /\.(?:md|markdown|mdx)$/i;
|
|
|
1370
1517
|
var DIFF_FILE = /\.(?:diff|patch)$/i;
|
|
1371
1518
|
var USAGE = `view [options] [file]: shows a markdown file, a source file or a git diff in the colours of the other @kaynooo CLIs
|
|
1372
1519
|
|
|
1373
|
-
A file, or what is piped on stdin (or - for stdin). A source file (ts, js, json, yaml, toml, py, sh, css, sql, rs, go and more) is highlighted behind line numbers, by its extension or --lang. A diff, a \`git log -p\` or a \`git show\` is recognised by its first line or a .diff or .patch name, anything else is markdown; --diff and --markdown force one. Code blocks in markdown are highlighted by their language.
|
|
1374
|
-
Lines wrap to the terminal width (80 when it is not a terminal) and a long output on a terminal goes through \`less -FRX\` unless --no-pager. No colour with NO_COLOR, a pipe or --plain.`;
|
|
1520
|
+
A file, or what is piped on stdin (or - for stdin). A source file (ts, js, json, yaml, toml, py, sh, css, sql, rs, go and more) is highlighted behind line numbers, by its extension or --lang. A diff, a \`git log -p\` or a \`git show\` is recognised by its first line or a .diff or .patch name, in a diff the code is highlighted by the file's extension and the changed words of a paired removed and added line are reversed; anything else is markdown; --diff and --markdown force one. Code blocks in markdown are highlighted by their language.
|
|
1521
|
+
Lines wrap to the terminal width (COLUMNS or 80 when it is not a terminal) and a long output on a terminal goes through $PAGER, or \`less -FRX\` when it is unset or less, unless --no-pager. No colour with NO_COLOR, a pipe or --plain.`;
|
|
1375
1522
|
var EXITS = [[0, "it showed the text"], [1, "there is nothing to show"], [2, "a usage error or a file that is missing"]];
|
|
1376
1523
|
var OPTIONS = {
|
|
1377
1524
|
diff: { type: "boolean", description: "show the text as a diff" },
|
|
@@ -1382,16 +1529,23 @@ var OPTIONS = {
|
|
|
1382
1529
|
pager: { type: "boolean", description: "page a long output on a terminal (--no-pager turns it off)" },
|
|
1383
1530
|
json: jsonOption("{kind: code, diff or markdown, language, text}, the text rendered without colour")
|
|
1384
1531
|
};
|
|
1532
|
+
function columnsOf(variable) {
|
|
1533
|
+
const columns = Number(variable);
|
|
1534
|
+
return Number.isInteger(columns) && columns >= 20 ? columns : DEFAULT_WIDTH;
|
|
1535
|
+
}
|
|
1536
|
+
function pagerCommand(pager = process17.env.PAGER) {
|
|
1537
|
+
return pager === undefined || pager.trim() === "" || pager.trim() === "less" ? ["less", "-FRX"] : ["sh", "-c", pager];
|
|
1538
|
+
}
|
|
1385
1539
|
async function page(text) {
|
|
1386
1540
|
try {
|
|
1387
|
-
const pager = Bun.spawn(
|
|
1541
|
+
const pager = Bun.spawn(pagerCommand(), { stdin: "pipe", stdout: "inherit", stderr: "inherit" });
|
|
1388
1542
|
try {
|
|
1389
1543
|
pager.stdin.write(text);
|
|
1390
1544
|
await pager.stdin.end();
|
|
1391
1545
|
} catch {}
|
|
1392
1546
|
await pager.exited;
|
|
1393
1547
|
} catch {
|
|
1394
|
-
|
|
1548
|
+
process17.stdout.write(text);
|
|
1395
1549
|
}
|
|
1396
1550
|
}
|
|
1397
1551
|
async function main(args) {
|
|
@@ -1404,8 +1558,8 @@ async function main(args) {
|
|
|
1404
1558
|
const text = await readInput(path, { noStdin: "give a file, or pipe the text on stdin" });
|
|
1405
1559
|
if (text.trim() === "")
|
|
1406
1560
|
fail("nothing to show", 1);
|
|
1407
|
-
const width = wholeNumber(values.width, "width",
|
|
1408
|
-
const theme = themeFor(
|
|
1561
|
+
const width = wholeNumber(values.width, "width", process17.stdout.isTTY === true ? process17.stdout.columns || DEFAULT_WIDTH : columnsOf(process17.env.COLUMNS), 20);
|
|
1562
|
+
const theme = themeFor(process17.stdout);
|
|
1409
1563
|
const language = values.lang === undefined ? languageOf(path) : languageOf(values.lang);
|
|
1410
1564
|
if (values.lang !== undefined && language === undefined)
|
|
1411
1565
|
badUsage(`unknown language ${values.lang}`);
|
|
@@ -1422,12 +1576,12 @@ async function main(args) {
|
|
|
1422
1576
|
printJson({ kind: isCode ? "code" : kind, language: isCode ? language ?? null : null, text: stripAnsi(render(themeFor({ isTTY: false }), values.lines ?? false)) });
|
|
1423
1577
|
return 0;
|
|
1424
1578
|
}
|
|
1425
|
-
const shown = render(theme, values.lines ??
|
|
1426
|
-
if (
|
|
1427
|
-
`).length > (
|
|
1579
|
+
const shown = render(theme, values.lines ?? process17.stdout.isTTY === true);
|
|
1580
|
+
if (process17.stdout.isTTY === true && values.pager !== false && shown.split(`
|
|
1581
|
+
`).length > (process17.stdout.rows || 24))
|
|
1428
1582
|
await page(shown);
|
|
1429
1583
|
else
|
|
1430
|
-
|
|
1584
|
+
process17.stdout.write(shown);
|
|
1431
1585
|
return 0;
|
|
1432
1586
|
}
|
|
1433
|
-
|
|
1587
|
+
process17.exitCode = await runMain({ name: "view", run: main });
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaynooo/cli-view",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.7.0",
|
|
5
5
|
"description": "view: markdown files and git diffs shown in the colours of the other @kaynooo CLIs, paged when long.",
|
|
6
6
|
"author": "Kaynooo <contact@kaynooo.fr>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -35,6 +35,6 @@
|
|
|
35
35
|
"view": "$npm_execpath src/view.ts"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@kaynooo/cli-kit": "0.
|
|
38
|
+
"@kaynooo/cli-kit": "0.40.0"
|
|
39
39
|
}
|
|
40
40
|
}
|