@kaynooo/cli-view 0.2.1 → 0.4.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/README.md +1 -1
- package/dist/view.js +414 -96
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ bun add -g @kaynooo/cli-view
|
|
|
8
8
|
|
|
9
9
|
| Command | What it does |
|
|
10
10
|
| --- | --- |
|
|
11
|
-
| `view README.md` | headings with their `#` marks, emphasis, lists with
|
|
11
|
+
| `view README.md` | headings with their `#` marks shaded by level, emphasis, lists with dashes and numbers that copy as plain text, task boxes, quotes behind a mark, code blocks between rules, tables with wrapped cells, links as OSC 8 links |
|
|
12
12
|
| `view src/app.ts`, `view config.yaml` | source highlighted (comments, strings, numbers, keywords, keys) behind line numbers on a terminal; ts, js, json, yaml, toml, ini, py, sh, css, html, sql, rs, go, php, java, c, rb, lua, Dockerfile, Makefile |
|
|
13
13
|
| `view --lang ts`, `--no-lines` | read stdin as that language; leave the line numbers off |
|
|
14
14
|
| `git diff \| view` | file headers, hunk ranges, added and removed lines coloured; a `git log -p` or `git show` too |
|
package/dist/view.js
CHANGED
|
@@ -2,13 +2,34 @@
|
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
4
|
// src/view.ts
|
|
5
|
-
import
|
|
5
|
+
import process15 from "process";
|
|
6
6
|
// ../kit/src/args.ts
|
|
7
|
-
import
|
|
7
|
+
import process10 from "process";
|
|
8
8
|
import { parseArgs } from "util";
|
|
9
9
|
|
|
10
|
-
// ../kit/src/
|
|
10
|
+
// ../kit/src/format.ts
|
|
11
|
+
import process7 from "process";
|
|
12
|
+
|
|
13
|
+
// ../kit/src/mode.ts
|
|
11
14
|
import process2 from "process";
|
|
15
|
+
var MODES = new Set(["terminal", "ci", "agent", "pipe"]);
|
|
16
|
+
var AGENT_VARIABLES = ["CLAUDECODE", "AI_AGENT"];
|
|
17
|
+
function isSet(value) {
|
|
18
|
+
return value !== undefined && value !== "" && value !== "0" && value.toLowerCase() !== "false";
|
|
19
|
+
}
|
|
20
|
+
function outputMode(stream = process2.stdout, env = process2.env) {
|
|
21
|
+
const chosen = env.KIT_MODE;
|
|
22
|
+
if (chosen !== undefined && MODES.has(chosen))
|
|
23
|
+
return chosen;
|
|
24
|
+
if (AGENT_VARIABLES.some((name) => isSet(env[name])))
|
|
25
|
+
return "agent";
|
|
26
|
+
if (stream.isTTY === true)
|
|
27
|
+
return "terminal";
|
|
28
|
+
return isSet(env.CI) ? "ci" : "pipe";
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// ../kit/src/style.ts
|
|
32
|
+
import process6 from "process";
|
|
12
33
|
|
|
13
34
|
// ../../node_modules/.bun/@kaynooo+utils@2.31.2+d0769e28dc4dad16/node_modules/@kaynooo/utils/dist/chunk-mtetydvc.js
|
|
14
35
|
function x(s) {
|
|
@@ -271,32 +292,272 @@ function le(i = {}) {
|
|
|
271
292
|
}
|
|
272
293
|
var t = le();
|
|
273
294
|
|
|
295
|
+
// ../kit/src/theme.ts
|
|
296
|
+
import { existsSync as existsSync2, readFileSync, statSync } from "fs";
|
|
297
|
+
import { homedir as homedir2 } from "os";
|
|
298
|
+
import { join as join3, resolve as resolve2 } from "path";
|
|
299
|
+
import process5 from "process";
|
|
300
|
+
|
|
301
|
+
// ../kit/src/config.ts
|
|
302
|
+
import { existsSync } from "fs";
|
|
303
|
+
import { dirname, join, resolve } from "path";
|
|
304
|
+
import process3 from "process";
|
|
305
|
+
function findUp(names, from = process3.cwd(), stop = () => false) {
|
|
306
|
+
const wanted = typeof names === "string" ? [names] : names;
|
|
307
|
+
for (let dir = resolve(from);; dir = dirname(dir)) {
|
|
308
|
+
const found = wanted.map((name) => join(dir, name)).find((path) => existsSync(path));
|
|
309
|
+
if (found !== undefined)
|
|
310
|
+
return found;
|
|
311
|
+
if (stop(dir) || dirname(dir) === dir)
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// ../kit/src/user-config.ts
|
|
317
|
+
import { homedir } from "os";
|
|
318
|
+
import { join as join2 } from "path";
|
|
319
|
+
import process4 from "process";
|
|
320
|
+
function configBase(where = {}) {
|
|
321
|
+
const xdg = where.xdg ?? (where.home === undefined ? process4.env.XDG_CONFIG_HOME : undefined);
|
|
322
|
+
return xdg !== undefined && xdg !== "" ? xdg : join2(where.home ?? homedir(), ".config");
|
|
323
|
+
}
|
|
324
|
+
function userConfigPath(bin, where = {}) {
|
|
325
|
+
return join2(configBase(where), "kaynooo-cli", `${bin}.json`);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// ../kit/src/theme.ts
|
|
329
|
+
function toLinear(channel) {
|
|
330
|
+
const value = channel / 255;
|
|
331
|
+
return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
|
|
332
|
+
}
|
|
333
|
+
var fromLinear = (value) => Math.round(255 * Math.min(1, Math.max(0, value <= 0.0031308 ? value * 12.92 : 1.055 * value ** (1 / 2.4) - 0.055)));
|
|
334
|
+
function toOklab(hex) {
|
|
335
|
+
const [r, g, b] = [1, 3, 5].map((at) => toLinear(Number.parseInt(hex.slice(at, at + 2), 16)));
|
|
336
|
+
const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b);
|
|
337
|
+
const m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b);
|
|
338
|
+
const s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b);
|
|
339
|
+
return [0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s, 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s, 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s];
|
|
340
|
+
}
|
|
341
|
+
function fromOklab([lightness, a, b]) {
|
|
342
|
+
const l = (lightness + 0.3963377774 * a + 0.2158037573 * b) ** 3;
|
|
343
|
+
const m = (lightness - 0.1055613458 * a - 0.0638541728 * b) ** 3;
|
|
344
|
+
const s = (lightness - 0.0894841775 * a - 1.291485548 * b) ** 3;
|
|
345
|
+
const channels = [4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s, -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s, -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s];
|
|
346
|
+
return `#${channels.map((channel) => fromLinear(channel).toString(16).padStart(2, "0")).join("")}`;
|
|
347
|
+
}
|
|
348
|
+
var ENDS = new Map;
|
|
349
|
+
function gradientAt(theme, ratio) {
|
|
350
|
+
if (theme.gradient === undefined)
|
|
351
|
+
return;
|
|
352
|
+
const key = theme.gradient.join();
|
|
353
|
+
let ends = ENDS.get(key);
|
|
354
|
+
if (ends === undefined) {
|
|
355
|
+
ends = [toOklab(theme.gradient[0]), toOklab(theme.gradient[1])];
|
|
356
|
+
ENDS.set(key, ends);
|
|
357
|
+
}
|
|
358
|
+
const at = Math.min(1, Math.max(0, ratio));
|
|
359
|
+
const [from, to] = ends;
|
|
360
|
+
return fromOklab(from.map((start, index) => start + (to[index] - start) * at));
|
|
361
|
+
}
|
|
362
|
+
var DEFAULT = {
|
|
363
|
+
colors: { accent: "cyan", id: "yellow", path: "blue", label: "dim", value: "bold", ok: "green", warn: "yellow", problem: "red", note: "dim", passed: "green", failed: "red", running: "cyan", queued: "yellow", skipped: "dim" },
|
|
364
|
+
glyphs: { ok: "\u2713", warn: "\u25B2", problem: "\u2717", note: "\u203A", passed: "\u2713", failed: "\u2717", running: "\u25CF", queued: "\u25CB", skipped: "-", bar: "\u2501", rule: "\u2500", corner: "\u256D", open: "\u2022", leaf: "\xB7", guide: "\u2502" },
|
|
365
|
+
accents: ["cyan", "magenta", "green", "blue", "yellow", "redBright"],
|
|
366
|
+
bins: {},
|
|
367
|
+
gradient: ["#c084fc", "#67e8f9"]
|
|
368
|
+
};
|
|
369
|
+
var BRIGHT = { accent: "cyanBright", id: "yellowBright", path: "blueBright", label: "white", value: "bold whiteBright", ok: "greenBright", warn: "yellowBright", problem: "redBright", note: "white", passed: "greenBright", failed: "redBright", running: "cyanBright", queued: "yellowBright", skipped: "white" };
|
|
370
|
+
var MONO = { accent: "bold", id: "bold", path: "underline", label: "dim", value: "bold", ok: "bold", warn: "bold", problem: "bold", note: "dim", passed: "bold", failed: "bold", running: "bold", queued: "dim", skipped: "dim" };
|
|
371
|
+
var ASCII = { ok: "+", warn: "!", problem: "x", note: ">", passed: "+", failed: "x", running: "*", queued: "o", skipped: "-", bar: "#", rule: "-", corner: "+", open: "*", leaf: "-", guide: "|" };
|
|
372
|
+
var PRESETS = {
|
|
373
|
+
default: {},
|
|
374
|
+
"high-contrast": { colors: BRIGHT, accents: ["cyanBright", "magentaBright", "greenBright", "blueBright", "yellowBright", "redBright"], gradient: ["#d8b4fe", "#a5f3fc"] },
|
|
375
|
+
mono: { colors: MONO, accents: ["bold"], gradient: undefined },
|
|
376
|
+
ascii: { glyphs: ASCII }
|
|
377
|
+
};
|
|
378
|
+
var STYLES = new Set(["bold", "dim", "italic", "underline", "inverse", "strikethrough", "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white", "gray", "grey", "blackBright", "redBright", "greenBright", "yellowBright", "blueBright", "magentaBright", "cyanBright", "whiteBright"]);
|
|
379
|
+
var HEX = /^#[0-9a-f]{6}$/i;
|
|
380
|
+
var CONTROL = /\p{Cc}/u;
|
|
381
|
+
var MAX_FILE_BYTES = 64 * 1024;
|
|
382
|
+
var SPACES = /\s+/;
|
|
383
|
+
function isColorSpec(spec) {
|
|
384
|
+
if (typeof spec === "number")
|
|
385
|
+
return Number.isInteger(spec) && spec >= 0 && spec <= 255;
|
|
386
|
+
return typeof spec === "string" && spec.trim() !== "" && spec.trim().split(SPACES).every((token) => STYLES.has(token) || HEX.test(token));
|
|
387
|
+
}
|
|
388
|
+
var CUBE = [0, 95, 135, 175, 215, 255];
|
|
389
|
+
function cubeLevel(channel) {
|
|
390
|
+
return CUBE.reduce((best, value, level) => Math.abs(value - channel) < Math.abs(CUBE[best] - channel) ? level : best, 0);
|
|
391
|
+
}
|
|
392
|
+
function nearest256(hex) {
|
|
393
|
+
const rgb = [1, 3, 5].map((at) => Number.parseInt(hex.slice(at, at + 2), 16));
|
|
394
|
+
const levels = rgb.map(cubeLevel);
|
|
395
|
+
const cube = levels.map((level) => CUBE[level]);
|
|
396
|
+
const grey = Math.min(23, Math.max(0, Math.round((rgb.reduce((sum, channel) => sum + channel, 0) / 3 - 8) / 10)));
|
|
397
|
+
const greyValue = 8 + grey * 10;
|
|
398
|
+
const distance = (colour) => colour.reduce((sum, channel, at) => sum + (channel - rgb[at]) ** 2, 0);
|
|
399
|
+
return distance([greyValue, greyValue, greyValue]) < distance(cube) ? 232 + grey : 16 + 36 * levels[0] + 6 * levels[1] + levels[2];
|
|
400
|
+
}
|
|
401
|
+
function hasTrueColor(env) {
|
|
402
|
+
return env.COLORTERM === "truecolor" || env.COLORTERM === "24bit";
|
|
403
|
+
}
|
|
404
|
+
function tinted(colors, spec, env = process5.env) {
|
|
405
|
+
if (typeof spec === "number")
|
|
406
|
+
return colors.ansi256(spec);
|
|
407
|
+
return spec.trim().split(SPACES).reduce((chain, token) => {
|
|
408
|
+
if (HEX.test(token))
|
|
409
|
+
return hasTrueColor(env) ? chain.hex(token) : chain.ansi256(nearest256(token));
|
|
410
|
+
return chain[token] ?? chain;
|
|
411
|
+
}, colors);
|
|
412
|
+
}
|
|
413
|
+
function isRecord(value) {
|
|
414
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
415
|
+
}
|
|
416
|
+
function readThemeFile(path, warn) {
|
|
417
|
+
try {
|
|
418
|
+
const stat = statSync(path, { throwIfNoEntry: false });
|
|
419
|
+
if (stat === undefined)
|
|
420
|
+
return;
|
|
421
|
+
if (!stat.isFile() || stat.size > MAX_FILE_BYTES) {
|
|
422
|
+
warn(`${path} is not a regular file under ${MAX_FILE_BYTES / 1024} KiB, theme ignored`);
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
const parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
426
|
+
if (isRecord(parsed))
|
|
427
|
+
return parsed;
|
|
428
|
+
warn(`${path} is not a JSON object, theme ignored`);
|
|
429
|
+
} catch (error) {
|
|
430
|
+
warn(`${path} does not parse (${error instanceof Error ? error.message : String(error)}), theme ignored`);
|
|
431
|
+
}
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
function applyPreset(theme, name, warn, where) {
|
|
435
|
+
if (name === undefined)
|
|
436
|
+
return;
|
|
437
|
+
if (typeof name !== "string" || !Object.hasOwn(PRESETS, name)) {
|
|
438
|
+
warn(`${where}: no preset ${String(name)}, one of ${Object.keys(PRESETS).join(", ")}`);
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
const preset = PRESETS[name];
|
|
442
|
+
Object.assign(theme.colors, preset.colors);
|
|
443
|
+
Object.assign(theme.glyphs, preset.glyphs);
|
|
444
|
+
if (preset.accents !== undefined)
|
|
445
|
+
theme.accents = [...preset.accents];
|
|
446
|
+
if (Object.hasOwn(preset, "gradient"))
|
|
447
|
+
theme.gradient = preset.gradient;
|
|
448
|
+
return name;
|
|
449
|
+
}
|
|
450
|
+
function applyFile(theme, file, where, warn) {
|
|
451
|
+
if (isRecord(file.colors)) {
|
|
452
|
+
for (const [key, value] of Object.entries(file.colors)) {
|
|
453
|
+
if (!Object.hasOwn(DEFAULT.colors, key))
|
|
454
|
+
warn(`${where}: no colour ${key}, one of ${Object.keys(DEFAULT.colors).join(", ")}`);
|
|
455
|
+
else if (!isColorSpec(value))
|
|
456
|
+
warn(`${where}: colour ${key} is not an ANSI name, \`#rrggbb\` or a number up to 255`);
|
|
457
|
+
else
|
|
458
|
+
theme.colors[key] = value;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
if (isRecord(file.glyphs)) {
|
|
462
|
+
for (const [key, value] of Object.entries(file.glyphs)) {
|
|
463
|
+
if (!Object.hasOwn(DEFAULT.glyphs, key))
|
|
464
|
+
warn(`${where}: no glyph ${key}, one of ${Object.keys(DEFAULT.glyphs).join(", ")}`);
|
|
465
|
+
else if (typeof value !== "string" || CONTROL.test(value) || Bun.stringWidth(value) !== 1)
|
|
466
|
+
warn(`${where}: glyph ${key} must be one visible character wide, with no control character`);
|
|
467
|
+
else
|
|
468
|
+
theme.glyphs[key] = value;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
if (Array.isArray(file.accents) && file.accents.length > 0 && file.accents.every(isColorSpec))
|
|
472
|
+
theme.accents = [...file.accents];
|
|
473
|
+
else if (file.accents !== undefined)
|
|
474
|
+
warn(`${where}: accents must be a non-empty list of colours`);
|
|
475
|
+
if (file.gradient === null)
|
|
476
|
+
theme.gradient = undefined;
|
|
477
|
+
else if (Array.isArray(file.gradient) && file.gradient.length === 2 && file.gradient.every((end) => typeof end === "string" && HEX.test(end)))
|
|
478
|
+
theme.gradient = [file.gradient[0], file.gradient[1]];
|
|
479
|
+
else if (file.gradient !== undefined)
|
|
480
|
+
warn(`${where}: gradient must be two \`#rrggbb\` colours, or null for none`);
|
|
481
|
+
if (isRecord(file.bins)) {
|
|
482
|
+
for (const [bin, value] of Object.entries(file.bins)) {
|
|
483
|
+
if (isColorSpec(value))
|
|
484
|
+
theme.bins[bin] = value;
|
|
485
|
+
else
|
|
486
|
+
warn(`${where}: bin ${bin} has no valid colour`);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
function endsWalk(home) {
|
|
491
|
+
return (dir) => dir === resolve2(home) || existsSync2(join3(dir, ".git"));
|
|
492
|
+
}
|
|
493
|
+
var warned = new Set;
|
|
494
|
+
function warnOnce(message) {
|
|
495
|
+
if (warned.has(message))
|
|
496
|
+
return;
|
|
497
|
+
warned.add(message);
|
|
498
|
+
console.error(`warning: theme ${message}`);
|
|
499
|
+
}
|
|
500
|
+
function isPath(chosen) {
|
|
501
|
+
return chosen.includes("/") || chosen.endsWith(".json");
|
|
502
|
+
}
|
|
503
|
+
function loadTheme({ env = process5.env, from = process5.cwd(), home = homedir2(), warn = warnOnce } = {}) {
|
|
504
|
+
const theme = { colors: { ...DEFAULT.colors }, glyphs: { ...DEFAULT.glyphs }, accents: [...DEFAULT.accents], bins: Object.create(null), gradient: DEFAULT.gradient };
|
|
505
|
+
const sources = [];
|
|
506
|
+
const chosen = env.KIT_THEME ?? "";
|
|
507
|
+
const wanted = chosen === "" ? [userConfigPath("theme", { home, xdg: env.XDG_CONFIG_HOME }), findUp(join3(".kaynooo", "theme.json"), from, endsWalk(home))] : [];
|
|
508
|
+
if (isPath(chosen))
|
|
509
|
+
wanted.push(resolve2(from, chosen));
|
|
510
|
+
const files = [];
|
|
511
|
+
for (const path of wanted) {
|
|
512
|
+
const file = path === undefined ? undefined : readThemeFile(path, warn);
|
|
513
|
+
if (path !== undefined && file !== undefined)
|
|
514
|
+
files.push({ path, file });
|
|
515
|
+
}
|
|
516
|
+
let preset = "default";
|
|
517
|
+
if (chosen !== "" && !isPath(chosen)) {
|
|
518
|
+
preset = applyPreset(theme, chosen, warn, "KIT_THEME") ?? preset;
|
|
519
|
+
} else {
|
|
520
|
+
const named = files.findLast(({ file }) => file.preset !== undefined);
|
|
521
|
+
if (named !== undefined)
|
|
522
|
+
preset = applyPreset(theme, named.file.preset, warn, named.path) ?? preset;
|
|
523
|
+
}
|
|
524
|
+
for (const { path, file } of files) {
|
|
525
|
+
applyFile(theme, file, path, warn);
|
|
526
|
+
sources.push(path);
|
|
527
|
+
}
|
|
528
|
+
return { theme, sources, preset };
|
|
529
|
+
}
|
|
530
|
+
var current;
|
|
531
|
+
var currentKey = "";
|
|
532
|
+
function activeTheme() {
|
|
533
|
+
const key = `${process5.cwd()}\x00${process5.env.KIT_THEME ?? ""}\x00${process5.env.XDG_CONFIG_HOME ?? ""}`;
|
|
534
|
+
if (current === undefined || key !== currentKey) {
|
|
535
|
+
current = loadTheme();
|
|
536
|
+
currentKey = key;
|
|
537
|
+
}
|
|
538
|
+
return current;
|
|
539
|
+
}
|
|
540
|
+
|
|
274
541
|
// ../kit/src/style.ts
|
|
275
|
-
function takesControlCodes(stream, env =
|
|
276
|
-
return stream.isTTY === true && env.NO_COLOR === undefined && env.NODE_DISABLE_COLORS === undefined && env.TERM !== "dumb";
|
|
542
|
+
function takesControlCodes(stream, env = process6.env) {
|
|
543
|
+
return stream.isTTY === true && env.NO_COLOR === undefined && env.NODE_DISABLE_COLORS === undefined && env.TERM !== "dumb" && outputMode(stream, env) === "terminal";
|
|
277
544
|
}
|
|
278
|
-
function paint(stream, env =
|
|
545
|
+
function paint(stream, env = process6.env) {
|
|
279
546
|
return le({ stream: { isTTY: takesControlCodes(stream, env) }, env });
|
|
280
547
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
var STATUSES = {
|
|
292
|
-
passed: { symbol: "\u2713", words: ["passed", "success", "succeeded", "ok"], paint: (colors) => colors.green },
|
|
293
|
-
failed: { symbol: "\u2717", words: ["failed", "failure", "error", "errored"], paint: (colors) => colors.red },
|
|
294
|
-
running: { symbol: "\u25CF", words: ["running", "in_progress", "started"], paint: (colors) => colors.cyan },
|
|
295
|
-
queued: { symbol: "\u25CB", words: ["queued", "waiting", "pending", "created", "blocked", "scheduled"], paint: (colors) => colors.yellow },
|
|
296
|
-
skipped: { symbol: "-", words: ["skipped", "cancelled", "canceled", "manual"], paint: (colors) => colors.dim }
|
|
548
|
+
function role(name, stream, env = process6.env) {
|
|
549
|
+
const colors = paint(stream, env);
|
|
550
|
+
return takesControlCodes(stream, env) ? tinted(colors, activeTheme().theme.colors[name], env) : colors;
|
|
551
|
+
}
|
|
552
|
+
var STATUS_WORDS = {
|
|
553
|
+
passed: ["passed", "success", "succeeded", "ok"],
|
|
554
|
+
failed: ["failed", "failure", "error", "errored"],
|
|
555
|
+
running: ["running", "in_progress", "started", "holding"],
|
|
556
|
+
queued: ["queued", "waiting", "pending", "created", "blocked", "scheduled"],
|
|
557
|
+
skipped: ["skipped", "cancelled", "canceled", "manual"]
|
|
297
558
|
};
|
|
298
|
-
var STATUS_OF = new Map(Object.entries(
|
|
299
|
-
function link(text, url, stream, env =
|
|
559
|
+
var STATUS_OF = new Map(Object.entries(STATUS_WORDS).flatMap(([status, words]) => words.map((word) => [word, status])));
|
|
560
|
+
function link(text, url, stream, env = process6.env) {
|
|
300
561
|
if (takesControlCodes(stream, env))
|
|
301
562
|
return `\x1B]8;;${url}\x1B\\${text}\x1B]8;;\x1B\\`;
|
|
302
563
|
return text === url ? text : `${text} (${url})`;
|
|
@@ -311,29 +572,35 @@ var ESCAPE = String.fromCharCode(27);
|
|
|
311
572
|
function textWidth(text) {
|
|
312
573
|
return Bun.stringWidth(text);
|
|
313
574
|
}
|
|
575
|
+
function jsonText(value, replacer, { stream = process7.stdout, env = process7.env } = {}) {
|
|
576
|
+
return JSON.stringify(value, replacer, outputMode(stream, env) === "agent" ? undefined : 2);
|
|
577
|
+
}
|
|
578
|
+
function printJson(value) {
|
|
579
|
+
console.log(jsonText(value));
|
|
580
|
+
}
|
|
314
581
|
|
|
315
582
|
// ../kit/src/help.ts
|
|
316
|
-
import
|
|
583
|
+
import process8 from "process";
|
|
317
584
|
var OPTIONAL = /\[[^\]]+\]/g;
|
|
318
585
|
var USAGE_PREFIX = /^usage:\s*/i;
|
|
319
586
|
var FULL_STOP = /\.$/;
|
|
320
|
-
function usageLine(usage, colors = paint(
|
|
587
|
+
function usageLine(usage, colors = paint(process8.stdout)) {
|
|
321
588
|
return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
|
|
322
589
|
}
|
|
323
|
-
function helpText(usage, options, colors = paint(
|
|
590
|
+
function helpText(usage, options, colors = paint(process8.stdout), { examples = [], replaces, exits = [] } = {}) {
|
|
324
591
|
return [usageLine(usage, colors), ...replacesRows(replaces, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exitRows(exits, colors), ...exampleRows(examples, colors)].join(`
|
|
325
592
|
`);
|
|
326
593
|
}
|
|
327
|
-
function exitRows(exits, colors = paint(
|
|
594
|
+
function exitRows(exits, colors = paint(process8.stdout)) {
|
|
328
595
|
if (exits.length === 0)
|
|
329
596
|
return [];
|
|
330
597
|
const width = Math.max(...exits.map(([code]) => String(code).length));
|
|
331
598
|
return ["", colors.bold("Exits:"), ...exits.map(([code, meaning]) => ` ${colors.cyan(String(code).padEnd(width))} ${meaning}`)];
|
|
332
599
|
}
|
|
333
|
-
function replacesRows(replaces, colors = paint(
|
|
334
|
-
return replaces === undefined ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
600
|
+
function replacesRows(replaces, colors = paint(process8.stdout)) {
|
|
601
|
+
return replaces === undefined || outputMode() === "agent" ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
335
602
|
}
|
|
336
|
-
function wantsHelpJson(env =
|
|
603
|
+
function wantsHelpJson(env = process8.env) {
|
|
337
604
|
return env.KIT_HELP === "json";
|
|
338
605
|
}
|
|
339
606
|
function description(usage, { examples = [], replaces, exits } = {}, options) {
|
|
@@ -352,12 +619,12 @@ function description(usage, { examples = [], replaces, exits } = {}, options) {
|
|
|
352
619
|
...options === undefined ? {} : { options: Object.entries(options).map(([name, { short, description }]) => ({ name, ...short === undefined ? {} : { short }, ...description === undefined ? {} : { description } })) }
|
|
353
620
|
};
|
|
354
621
|
}
|
|
355
|
-
function exampleRows(examples, colors = paint(
|
|
356
|
-
if (examples.length === 0)
|
|
622
|
+
function exampleRows(examples, colors = paint(process8.stdout)) {
|
|
623
|
+
if (examples.length === 0 || outputMode() === "agent")
|
|
357
624
|
return [];
|
|
358
625
|
return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
|
|
359
626
|
}
|
|
360
|
-
function optionRows(options, colors = paint(
|
|
627
|
+
function optionRows(options, colors = paint(process8.stdout)) {
|
|
361
628
|
const rows = Object.entries(options).map(([name, option]) => {
|
|
362
629
|
const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
|
|
363
630
|
const named = option.choices === undefined ? "<value>" : `<${option.choices.join("|")}>`;
|
|
@@ -392,7 +659,7 @@ function closest(name, names) {
|
|
|
392
659
|
return best !== undefined && best.far <= (name.length < 4 ? 1 : 2) ? best.known : undefined;
|
|
393
660
|
}
|
|
394
661
|
// ../kit/src/input.ts
|
|
395
|
-
import
|
|
662
|
+
import process9 from "process";
|
|
396
663
|
var WHOLE = /^\d+$/;
|
|
397
664
|
function wholeNumber(value, name, fallback, min = 1) {
|
|
398
665
|
if (value === undefined)
|
|
@@ -404,7 +671,7 @@ function wholeNumber(value, name, fallback, min = 1) {
|
|
|
404
671
|
}
|
|
405
672
|
async function readInput(path, { noStdin = "nothing is piped on stdin" } = {}) {
|
|
406
673
|
if (path === "-") {
|
|
407
|
-
if (
|
|
674
|
+
if (process9.stdin.isTTY)
|
|
408
675
|
fail(noStdin);
|
|
409
676
|
return Bun.stdin.text();
|
|
410
677
|
}
|
|
@@ -416,6 +683,9 @@ async function readInput(path, { noStdin = "nothing is piped on stdin" } = {}) {
|
|
|
416
683
|
|
|
417
684
|
// ../kit/src/args.ts
|
|
418
685
|
var DASHES = /^-+/;
|
|
686
|
+
function jsonOption(holds) {
|
|
687
|
+
return { type: "boolean", description: holds === undefined ? "print JSON" : `print JSON: ${holds}` };
|
|
688
|
+
}
|
|
419
689
|
class Exit extends Error {
|
|
420
690
|
code;
|
|
421
691
|
hint;
|
|
@@ -459,7 +729,7 @@ var OUTPUT_OPTIONS = {
|
|
|
459
729
|
color: { type: "string", description: "auto (on a terminal alone), always or never" },
|
|
460
730
|
plain: { type: "boolean", description: "plain text, no colour or spinner: --color never" }
|
|
461
731
|
};
|
|
462
|
-
function applyOutput(color, plain, env =
|
|
732
|
+
function applyOutput(color, plain, env = process10.env) {
|
|
463
733
|
const when = plain === true ? "never" : color ?? "auto";
|
|
464
734
|
if (when === "auto")
|
|
465
735
|
return;
|
|
@@ -634,15 +904,15 @@ var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x
|
|
|
634
904
|
var RESET_STYLE = `${ESC}[0m`;
|
|
635
905
|
var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
636
906
|
// ../kit/src/cli.ts
|
|
637
|
-
import { readFileSync, realpathSync } from "fs";
|
|
638
|
-
import { dirname as
|
|
639
|
-
import
|
|
907
|
+
import { readFileSync as readFileSync2, realpathSync } from "fs";
|
|
908
|
+
import { dirname as dirname3, join as join5 } from "path";
|
|
909
|
+
import process12 from "process";
|
|
640
910
|
|
|
641
911
|
// ../kit/src/usage.ts
|
|
642
912
|
import { appendFileSync, mkdirSync } from "fs";
|
|
643
|
-
import { homedir } from "os";
|
|
644
|
-
import { dirname, isAbsolute, join } from "path";
|
|
645
|
-
import
|
|
913
|
+
import { homedir as homedir3 } from "os";
|
|
914
|
+
import { dirname as dirname2, isAbsolute, join as join4 } from "path";
|
|
915
|
+
import process11 from "process";
|
|
646
916
|
|
|
647
917
|
// ../kit/src/logs.ts
|
|
648
918
|
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
@@ -656,14 +926,14 @@ var usageError;
|
|
|
656
926
|
function noteUsageError(message) {
|
|
657
927
|
usageError = message;
|
|
658
928
|
}
|
|
659
|
-
function stateHome(env =
|
|
929
|
+
function stateHome(env = process11.env) {
|
|
660
930
|
const dir = env.XDG_STATE_HOME;
|
|
661
|
-
return dir !== undefined && isAbsolute(dir) ? dir :
|
|
931
|
+
return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
|
|
662
932
|
}
|
|
663
|
-
function usagePath(env =
|
|
664
|
-
return
|
|
933
|
+
function usagePath(env = process11.env) {
|
|
934
|
+
return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
665
935
|
}
|
|
666
|
-
function usageRecorded(env =
|
|
936
|
+
function usageRecorded(env = process11.env) {
|
|
667
937
|
const off = env.KAYNOOO_CLI_NO_USAGE;
|
|
668
938
|
return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
|
|
669
939
|
}
|
|
@@ -677,12 +947,12 @@ function usageEntry(bin, command, argv, code, now = new Date) {
|
|
|
677
947
|
usageError = undefined;
|
|
678
948
|
return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
|
|
679
949
|
}
|
|
680
|
-
function recordUsage(entry, env =
|
|
950
|
+
function recordUsage(entry, env = process11.env) {
|
|
681
951
|
if (!usageRecorded(env))
|
|
682
952
|
return;
|
|
683
953
|
try {
|
|
684
954
|
const path = usagePath(env);
|
|
685
|
-
mkdirSync(
|
|
955
|
+
mkdirSync(dirname2(path), { recursive: true, mode: 448 });
|
|
686
956
|
appendFileSync(path, `${JSON.stringify(entry)}
|
|
687
957
|
`, { mode: 384 });
|
|
688
958
|
} catch {}
|
|
@@ -695,8 +965,8 @@ var LEVEL_OPTIONS = {
|
|
|
695
965
|
};
|
|
696
966
|
var TOP_OPTIONS = { ...LEVEL_OPTIONS, version: { type: "boolean", short: "v", description: "print the version" } };
|
|
697
967
|
function printError(prefix, message, hint, cause) {
|
|
698
|
-
const colors = paint(
|
|
699
|
-
const styled = takesControlCodes(
|
|
968
|
+
const colors = paint(process12.stderr);
|
|
969
|
+
const styled = takesControlCodes(process12.stderr);
|
|
700
970
|
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
701
971
|
const under = (label, text, tint) => text === undefined ? "" : `
|
|
702
972
|
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
@@ -715,7 +985,7 @@ function reportThrown(prefix, error, redact = (text) => text, json = false) {
|
|
|
715
985
|
const help = error instanceof UsageError ? `run ${prefix} --help for its options` : undefined;
|
|
716
986
|
const named = error instanceof Error && !(error instanceof Exit) && error.name !== "Error" ? `${error.name}: ${error.message}` : undefined;
|
|
717
987
|
const cause = error instanceof Error && error.cause instanceof Error ? redact(error.cause.message) : undefined;
|
|
718
|
-
if (error instanceof
|
|
988
|
+
if (error instanceof Exit && error.code === 2)
|
|
719
989
|
noteUsageError(error.message);
|
|
720
990
|
const report = json ? printJsonError : (message, hint, causeText) => printError(prefix, message, hint, causeText);
|
|
721
991
|
report(redact(named ?? (error instanceof Error ? error.message : String(error))), error instanceof Exit ? error.hint ?? help : undefined, cause);
|
|
@@ -779,17 +1049,17 @@ async function printCandidates(found) {
|
|
|
779
1049
|
return 0;
|
|
780
1050
|
}
|
|
781
1051
|
function packageVersion(entry = Bun.main) {
|
|
782
|
-
for (let dir =
|
|
1052
|
+
for (let dir = dirname3(realpathSync(entry));; dir = dirname3(dir)) {
|
|
783
1053
|
try {
|
|
784
|
-
const { version } = JSON.parse(
|
|
1054
|
+
const { version } = JSON.parse(readFileSync2(join5(dir, "package.json"), "utf8"));
|
|
785
1055
|
if (typeof version === "string")
|
|
786
1056
|
return version;
|
|
787
1057
|
} catch {}
|
|
788
|
-
if (
|
|
1058
|
+
if (dirname3(dir) === dir)
|
|
789
1059
|
return "0.0.0";
|
|
790
1060
|
}
|
|
791
1061
|
}
|
|
792
|
-
async function runMain({ name, version, run, redact, usage }, argv =
|
|
1062
|
+
async function runMain({ name, version, run, redact, usage }, argv = process12.argv.slice(2)) {
|
|
793
1063
|
if (argv[0] === COMPLETE)
|
|
794
1064
|
return printCandidates(leafCandidates(run, argv.slice(1)));
|
|
795
1065
|
if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
|
|
@@ -809,6 +1079,8 @@ async function runMain({ name, version, run, redact, usage }, argv = process7.ar
|
|
|
809
1079
|
}
|
|
810
1080
|
return code;
|
|
811
1081
|
}
|
|
1082
|
+
// ../kit/src/folders.ts
|
|
1083
|
+
var SKIPPED_FOLDERS = new Set(["node_modules", "vendor", "dist", "build", "target", "coverage", "venv", "__pycache__"]);
|
|
812
1084
|
// ../kit/src/secrets.ts
|
|
813
1085
|
var SECRET_WORDS = new Set(["key", "pass", "pwd", "auth", "salt", "cookie", "session", "jwt", "ssn", "iban", "card", "cvv", "otp"]);
|
|
814
1086
|
// ../kit/src/sessions.ts
|
|
@@ -824,6 +1096,10 @@ var AFTER_KEY = /^\s*:/;
|
|
|
824
1096
|
var YAML_KEY = /^(\s*(?:- )?)([\w.\-/]+)(?=:(?:\s|$))/;
|
|
825
1097
|
var SETTING_KEY = /^(\s*)([\w.-]+)(?=\s*[=:])/;
|
|
826
1098
|
var SECTION = /^\s*\[.*\]\s*$/;
|
|
1099
|
+
var WHITESPACE = /\s/;
|
|
1100
|
+
var FLAG = /^--?[a-z][\w-]*/i;
|
|
1101
|
+
var COMMAND_END = /[|&;(]\s*$/;
|
|
1102
|
+
var PROMPT = /^\s*(?:[$>]\s*)?$/;
|
|
827
1103
|
var DIGIT = /\d/;
|
|
828
1104
|
var TRAILING_NEWLINE = /\n$/;
|
|
829
1105
|
var LEADING_DOT = /^\./;
|
|
@@ -841,7 +1117,7 @@ var LANGUAGES = {
|
|
|
841
1117
|
json: { line: [], quotes: '"', keywords: words("true false null"), quotedKeys: true },
|
|
842
1118
|
jsonc: { ...SLASHES, quotes: '"', keywords: words("true false null"), quotedKeys: true },
|
|
843
1119
|
python: { ...HASH, quotes: `"'`, keywords: words("def class return if elif else for while in not and or is import from as with try except finally raise pass break continue lambda yield async await global nonlocal del assert None True False self") },
|
|
844
|
-
shell: { ...HASH, quotes: "\"'`", keywords: words("if then else elif fi for while until do done case esac in function return exit local export set unset source alias echo cd end switch begin and or not") },
|
|
1120
|
+
shell: { ...HASH, commands: true, quotes: "\"'`", keywords: words("if then else elif fi for while until do done case esac in function return exit local export set unset source alias echo cd end switch begin and or not") },
|
|
845
1121
|
yaml: { ...HASH, quotes: `"'`, keywords: words("true false null yes no"), key: YAML_KEY, quotedKeys: true },
|
|
846
1122
|
toml: { ...HASH, quotes: `"'`, keywords: words("true false"), key: SETTING_KEY, section: SECTION },
|
|
847
1123
|
ini: { line: ["#", ";"], quotes: `"'`, keywords: words("true false yes no on off"), key: SETTING_KEY, section: SECTION },
|
|
@@ -966,9 +1242,19 @@ function highlight(source, name, theme) {
|
|
|
966
1242
|
const number = NUMBER.exec(text.slice(at))?.[0] ?? char;
|
|
967
1243
|
out += theme.number(number);
|
|
968
1244
|
at += number.length;
|
|
1245
|
+
} else if (language.commands === true && char === "-" && (at === 0 || WHITESPACE.test(text.charAt(at - 1))) && FLAG.test(text.slice(at))) {
|
|
1246
|
+
const flag = FLAG.exec(text.slice(at))?.[0] ?? char;
|
|
1247
|
+
out += theme.number(flag);
|
|
1248
|
+
at += flag.length;
|
|
969
1249
|
} else if (WORD_START.test(char)) {
|
|
970
1250
|
const word = WORD.exec(text.slice(at))?.[0] ?? char;
|
|
971
|
-
|
|
1251
|
+
const before = text.slice(text.lastIndexOf(`
|
|
1252
|
+
`, at - 1) + 1, at);
|
|
1253
|
+
const command = language.commands === true && (PROMPT.test(before) || COMMAND_END.test(before));
|
|
1254
|
+
if (language.keywords.has(word))
|
|
1255
|
+
out += theme.keyword(word);
|
|
1256
|
+
else
|
|
1257
|
+
out += command && !word.startsWith("$") ? theme.accent(word) : word;
|
|
972
1258
|
at += word.length;
|
|
973
1259
|
} else {
|
|
974
1260
|
out += char;
|
|
@@ -1055,10 +1341,13 @@ var NESTED = "\x05";
|
|
|
1055
1341
|
var CELL = "\x06";
|
|
1056
1342
|
var ROW = "\x07";
|
|
1057
1343
|
var BLOCK = "\b";
|
|
1058
|
-
var BULLETS = ["\u2022", "\u25E6", "\u25AA"];
|
|
1059
1344
|
var MAX_RULE = 60;
|
|
1345
|
+
var MAX_CODE_RULE = 5;
|
|
1346
|
+
var MIN_CODE_RULE = 4;
|
|
1347
|
+
var HEADING_SHADES = [0, 0.35, 0.7, 1];
|
|
1060
1348
|
var MIN_ROOM = 20;
|
|
1061
1349
|
var MIN_COLUMN = 8;
|
|
1350
|
+
var TYPICAL = 1.4;
|
|
1062
1351
|
var COLUMN_GAP = 2;
|
|
1063
1352
|
var TRAILING_BREAKS = /\n+$/;
|
|
1064
1353
|
function block(text) {
|
|
@@ -1098,8 +1387,23 @@ function finish(block, width) {
|
|
|
1098
1387
|
`);
|
|
1099
1388
|
}
|
|
1100
1389
|
function columnWidths(rows, width) {
|
|
1101
|
-
const
|
|
1102
|
-
const
|
|
1390
|
+
const longest = rows[0]?.map((_, column) => Math.max(...rows.map((row) => textWidth(row[column] ?? "")))) ?? [];
|
|
1391
|
+
const gaps = COLUMN_GAP * Math.max(0, longest.length - 1);
|
|
1392
|
+
const sum = (list) => list.reduce((total, each) => total + each, 0);
|
|
1393
|
+
if (sum(longest) + gaps <= width)
|
|
1394
|
+
return longest;
|
|
1395
|
+
const typical = longest.map((most, column) => Math.min(most, Math.max(MIN_COLUMN, Math.ceil(TYPICAL * sum(rows.map((row) => textWidth(row[column] ?? ""))) / rows.length))));
|
|
1396
|
+
const widths = [...typical];
|
|
1397
|
+
const room = width - gaps - sum(typical);
|
|
1398
|
+
const wanting = longest.map((most, column) => most - typical[column]);
|
|
1399
|
+
if (room > 0 && sum(wanting) > 0) {
|
|
1400
|
+
for (const [column, want] of wanting.entries())
|
|
1401
|
+
widths[column] += Math.min(want, Math.floor(room * want / sum(wanting)));
|
|
1402
|
+
const left = width - gaps - sum(widths);
|
|
1403
|
+
const hungriest = wanting.indexOf(Math.max(...wanting));
|
|
1404
|
+
widths[hungriest] += Math.max(0, Math.min(left, longest[hungriest] - widths[hungriest]));
|
|
1405
|
+
}
|
|
1406
|
+
const total = () => sum(widths) + gaps;
|
|
1103
1407
|
while (total() > width) {
|
|
1104
1408
|
const widest = widths.indexOf(Math.max(...widths));
|
|
1105
1409
|
if ((widths[widest] ?? 0) <= MIN_COLUMN)
|
|
@@ -1110,17 +1414,21 @@ function columnWidths(rows, width) {
|
|
|
1110
1414
|
}
|
|
1111
1415
|
function renderTable(rows, aligns, width, theme) {
|
|
1112
1416
|
const widths = columnWidths(rows, width);
|
|
1113
|
-
const
|
|
1114
|
-
|
|
1115
|
-
|
|
1417
|
+
const wrapped = rows.map((row) => widths.map((each, column) => Bun.wrapAnsi(row[column] ?? "", each, { hard: true }).split(`
|
|
1418
|
+
`)));
|
|
1419
|
+
const spaced = wrapped.some((cells) => cells.some((cell) => cell.length > 1));
|
|
1420
|
+
const lines = rows.flatMap((_, index) => {
|
|
1421
|
+
const cells = wrapped[index];
|
|
1116
1422
|
const height = Math.max(...cells.map((cell) => cell.length));
|
|
1117
1423
|
const drawn = Array.from({ length: height }, (_, at) => cells.map((cell, column) => {
|
|
1118
1424
|
const text = cell[at] ?? "";
|
|
1119
1425
|
const gap = " ".repeat(Math.max(0, (widths[column] ?? 0) - textWidth(text)));
|
|
1120
1426
|
const aligned = aligns[column] === "right" ? `${gap}${text}` : `${text}${gap}`;
|
|
1121
|
-
return index === 0 ? theme.
|
|
1427
|
+
return index === 0 ? theme.heading(aligned) : aligned;
|
|
1122
1428
|
}).join(" ".repeat(COLUMN_GAP)).trimEnd());
|
|
1123
|
-
|
|
1429
|
+
if (index === 0)
|
|
1430
|
+
return [...drawn, theme.dim("\u2500".repeat(Math.min(width, widths.reduce((sum, each) => sum + each, 0) + COLUMN_GAP * Math.max(0, widths.length - 1))))];
|
|
1431
|
+
return spaced && index > 1 ? ["", ...drawn] : drawn;
|
|
1124
1432
|
});
|
|
1125
1433
|
return lines.join(`
|
|
1126
1434
|
`);
|
|
@@ -1132,21 +1440,20 @@ function printable(source) {
|
|
|
1132
1440
|
` || char === "\t").join("");
|
|
1133
1441
|
}
|
|
1134
1442
|
function renderMarkdown(source, { theme, width }) {
|
|
1135
|
-
const
|
|
1136
|
-
const quote = theme.dim("\u258E ");
|
|
1443
|
+
const quote = [`${theme.shade("\u275D", 0)} `, " "];
|
|
1137
1444
|
let aligns = [];
|
|
1138
1445
|
const rendered = Bun.markdown.render(printable(source), {
|
|
1139
|
-
heading: (children, { level }) => block(`${reflow(theme.
|
|
1446
|
+
heading: (children, { level }) => block(`${reflow(theme.shade(theme.bold(children), HEADING_SHADES[Math.min(level, HEADING_SHADES.length) - 1] ?? 1), theme.dim(`${"#".repeat(level)} `), " ".repeat(level + 1))}
|
|
1140
1447
|
|
|
1141
1448
|
`),
|
|
1142
1449
|
paragraph: (children) => block(`${reflow(children.replaceAll(`
|
|
1143
1450
|
`, " "))}
|
|
1144
1451
|
|
|
1145
1452
|
`),
|
|
1146
|
-
strong: theme.bold,
|
|
1453
|
+
strong: (text) => theme.shade(theme.bold(text), 0),
|
|
1147
1454
|
emphasis: theme.italic,
|
|
1148
1455
|
strikethrough: theme.strike,
|
|
1149
|
-
codespan: theme.
|
|
1456
|
+
codespan: (text) => `${theme.dim("`")}${theme.accent(text)}${theme.dim("`")}`,
|
|
1150
1457
|
link: (children, { href }) => theme.link(children, href),
|
|
1151
1458
|
image: (children, { src }) => theme.dim(`[image: ${children === "" ? src : children}]`),
|
|
1152
1459
|
html: (children) => block(`${theme.dim(trimmed(children))}
|
|
@@ -1155,13 +1462,18 @@ function renderMarkdown(source, { theme, width }) {
|
|
|
1155
1462
|
code: (children, meta) => {
|
|
1156
1463
|
const language = languageOf(meta?.language ?? "");
|
|
1157
1464
|
const body = trimmed(children);
|
|
1158
|
-
|
|
1159
|
-
`)
|
|
1465
|
+
const lines = (language === undefined ? body : highlight(body, language, theme)).split(`
|
|
1466
|
+
`);
|
|
1467
|
+
const rule = Math.min(width, MAX_CODE_RULE, Math.max(MIN_CODE_RULE, ...lines.map((line) => textWidth(line))));
|
|
1468
|
+
const label = meta?.language === undefined || meta.language === "" ? "" : `${meta.language} `;
|
|
1469
|
+
return block(`${theme.dim(`${label}${"\u2500".repeat(Math.max(0, rule - textWidth(label)))}`)}
|
|
1470
|
+
${lines.join(`
|
|
1160
1471
|
`)}
|
|
1472
|
+
${theme.dim("\u2500".repeat(rule))}
|
|
1161
1473
|
|
|
1162
1474
|
`);
|
|
1163
1475
|
},
|
|
1164
|
-
blockquote: (children) => block(`${indent(trimmed(children.replaceAll(BLOCK, "")), quote, quote)}
|
|
1476
|
+
blockquote: (children) => block(`${indent(trimmed(children.replaceAll(BLOCK, "")), quote[0], quote[1])}
|
|
1165
1477
|
|
|
1166
1478
|
`),
|
|
1167
1479
|
hr: () => block(`${theme.dim("\u2500".repeat(Math.min(width, MAX_RULE)))}
|
|
@@ -1172,7 +1484,7 @@ function renderMarkdown(source, { theme, width }) {
|
|
|
1172
1484
|
` : block(`${trimmed(children)}
|
|
1173
1485
|
|
|
1174
1486
|
`),
|
|
1175
|
-
listItem: (children, { index, ordered, start,
|
|
1487
|
+
listItem: (children, { index, ordered, start, checked }) => {
|
|
1176
1488
|
const [head = "", ...nested] = children.split(NESTED);
|
|
1177
1489
|
const blocksAt = head.indexOf(BLOCK);
|
|
1178
1490
|
const inline = (blocksAt === -1 ? head : head.slice(0, blocksAt)).trim();
|
|
@@ -1180,11 +1492,11 @@ function renderMarkdown(source, { theme, width }) {
|
|
|
1180
1492
|
const text = [inline === "" ? "" : reflow(inline.replaceAll(`
|
|
1181
1493
|
`, " ")), blocks].filter((part) => part !== "").join(`
|
|
1182
1494
|
`) || reflow("");
|
|
1183
|
-
let marker = theme.
|
|
1495
|
+
let marker = theme.dim("-");
|
|
1184
1496
|
if (checked !== undefined)
|
|
1185
1497
|
marker = checked ? theme.added("\u2713") : theme.dim("\u25CB");
|
|
1186
1498
|
else if (ordered)
|
|
1187
|
-
marker = theme.
|
|
1499
|
+
marker = theme.dim(`${(start ?? 1) + index}.`);
|
|
1188
1500
|
return `${indent([text, ...nested.map(trimmed)].join(`
|
|
1189
1501
|
`), `${marker} `, " ".repeat(textWidth(marker) + 1))}
|
|
1190
1502
|
`;
|
|
@@ -1209,8 +1521,8 @@ function renderMarkdown(source, { theme, width }) {
|
|
|
1209
1521
|
}
|
|
1210
1522
|
|
|
1211
1523
|
// src/theme.ts
|
|
1212
|
-
import
|
|
1213
|
-
function themeFor(stream =
|
|
1524
|
+
import process14 from "process";
|
|
1525
|
+
function themeFor(stream = process14.stdout, env = process14.env) {
|
|
1214
1526
|
const colors = paint(stream, env);
|
|
1215
1527
|
const accent = role("accent", stream, env);
|
|
1216
1528
|
const path = role("path", stream, env);
|
|
@@ -1229,7 +1541,8 @@ function themeFor(stream = process9.stdout, env = process9.env) {
|
|
|
1229
1541
|
keyword: colors.magenta,
|
|
1230
1542
|
string: colors.green,
|
|
1231
1543
|
number: colors.yellow,
|
|
1232
|
-
link: (text, url) => path(colors.underline(link(text, url, stream, env)))
|
|
1544
|
+
link: (text, url) => path(colors.underline(link(text, url, stream, env))),
|
|
1545
|
+
shade: (text, ratio) => takesControlCodes(stream, env) ? tinted(colors, gradientAt(activeTheme().theme, ratio) ?? activeTheme().theme.colors.accent, env)(text) : text
|
|
1233
1546
|
};
|
|
1234
1547
|
}
|
|
1235
1548
|
|
|
@@ -1249,7 +1562,8 @@ var OPTIONS = {
|
|
|
1249
1562
|
lang: { type: "string", short: "l", description: "show the text as code in this language (ts, json, yaml, sh...)" },
|
|
1250
1563
|
lines: { type: "boolean", description: "number the lines of code (--no-lines turns it off; on a terminal by default)" },
|
|
1251
1564
|
width: { type: "string", short: "w", description: "columns to wrap to, instead of the terminal's" },
|
|
1252
|
-
pager: { type: "boolean", description: "page a long output on a terminal (--no-pager turns it off)" }
|
|
1565
|
+
pager: { type: "boolean", description: "page a long output on a terminal (--no-pager turns it off)" },
|
|
1566
|
+
json: jsonOption("{kind: code, diff or markdown, language, text}, the text rendered without colour")
|
|
1253
1567
|
};
|
|
1254
1568
|
async function page(text) {
|
|
1255
1569
|
try {
|
|
@@ -1260,7 +1574,7 @@ async function page(text) {
|
|
|
1260
1574
|
} catch {}
|
|
1261
1575
|
await pager.exited;
|
|
1262
1576
|
} catch {
|
|
1263
|
-
|
|
1577
|
+
process15.stdout.write(text);
|
|
1264
1578
|
}
|
|
1265
1579
|
}
|
|
1266
1580
|
async function main(args) {
|
|
@@ -1273,26 +1587,30 @@ async function main(args) {
|
|
|
1273
1587
|
const text = await readInput(path, { noStdin: "give a file, or pipe the text on stdin" });
|
|
1274
1588
|
if (text.trim() === "")
|
|
1275
1589
|
fail("nothing to show", 1);
|
|
1276
|
-
const width = wholeNumber(values.width, "width",
|
|
1277
|
-
const theme = themeFor(
|
|
1590
|
+
const width = wholeNumber(values.width, "width", process15.stdout.isTTY === true ? process15.stdout.columns || DEFAULT_WIDTH : DEFAULT_WIDTH, 20);
|
|
1591
|
+
const theme = themeFor(process15.stdout);
|
|
1278
1592
|
const language = values.lang === undefined ? languageOf(path) : languageOf(values.lang);
|
|
1279
1593
|
if (values.lang !== undefined && language === undefined)
|
|
1280
1594
|
badUsage(`unknown language ${values.lang}`);
|
|
1281
1595
|
const forced = values.diff === true || values.markdown === true;
|
|
1282
1596
|
const isCode = values.lang !== undefined || !forced && language !== undefined && !MARKDOWN_FILE.test(path) && !DIFF_FILE.test(path);
|
|
1283
1597
|
const isDiff = !isCode && (values.diff === true || values.markdown !== true && !MARKDOWN_FILE.test(path) && (DIFF_FILE.test(path) || looksLikeDiff(text)));
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1598
|
+
const kind = isDiff ? "diff" : "markdown";
|
|
1599
|
+
function render(as, numbered) {
|
|
1600
|
+
if (isCode)
|
|
1601
|
+
return renderCode(text, language, as, numbered);
|
|
1602
|
+
return isDiff ? renderDiff(text, as) : renderMarkdown(text, { theme: as, width });
|
|
1603
|
+
}
|
|
1604
|
+
if (values.json === true) {
|
|
1605
|
+
printJson({ kind: isCode ? "code" : kind, language: isCode ? language ?? null : null, text: stripAnsi(render(themeFor({ isTTY: false }), values.lines ?? false)) });
|
|
1606
|
+
return 0;
|
|
1607
|
+
}
|
|
1608
|
+
const shown = render(theme, values.lines ?? process15.stdout.isTTY === true);
|
|
1609
|
+
if (process15.stdout.isTTY === true && values.pager !== false && shown.split(`
|
|
1610
|
+
`).length > (process15.stdout.rows || 24))
|
|
1293
1611
|
await page(shown);
|
|
1294
1612
|
else
|
|
1295
|
-
|
|
1613
|
+
process15.stdout.write(shown);
|
|
1296
1614
|
return 0;
|
|
1297
1615
|
}
|
|
1298
|
-
|
|
1616
|
+
process15.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.4.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.35.0"
|
|
39
39
|
}
|
|
40
40
|
}
|