@kaynooo/cli-view 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +17 -2
- package/dist/view.js +1009 -0
- package/package.json +38 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kaynooo <contact@kaynooo.fr>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @kaynooo/cli-view
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`view` shows a markdown file or a git diff in the colours of the other @kaynooo CLIs: the kit roles, wrapped to the terminal, paged through `less -FRX` when it is long.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
bun add -g @kaynooo/cli-view
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
| Command | What it does |
|
|
10
|
+
| --- | --- |
|
|
11
|
+
| `view README.md` | headings with their `#` marks, emphasis, lists with bullets by depth, numbers and task boxes, quotes and code blocks behind a bar, tables with wrapped cells, links as OSC 8 links |
|
|
12
|
+
| `git diff \| view` | file headers, hunk ranges, added and removed lines coloured; a `git log -p` or `git show` too |
|
|
13
|
+
| `view --diff`, `--markdown` | force the reading when the first line or the file name does not say |
|
|
14
|
+
| `--width <n>`, `--no-pager` | wrap to a width instead of the terminal's, never page |
|
|
15
|
+
|
|
16
|
+
The text is a file, or what is piped on stdin. A diff is recognised by its first line (`diff --git`, `--- `, `@@`, `commit <sha>`) or a `.diff` or `.patch` name; anything else is markdown. With `NO_COLOR`, a pipe or `--plain` it prints without colour: links as `text (url)`, images as `[image: alt]`.
|
|
17
|
+
|
|
18
|
+
Not in v1: word-level diff highlighting and syntax highlighting of code blocks.
|
package/dist/view.js
ADDED
|
@@ -0,0 +1,1009 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
// @bun
|
|
3
|
+
|
|
4
|
+
// src/view.ts
|
|
5
|
+
import process6 from "process";
|
|
6
|
+
|
|
7
|
+
// ../kit/src/args.ts
|
|
8
|
+
import process2 from "process";
|
|
9
|
+
import { parseArgs } from "util";
|
|
10
|
+
|
|
11
|
+
// ../kit/src/style.ts
|
|
12
|
+
import process from "process";
|
|
13
|
+
|
|
14
|
+
// ../../node_modules/.bun/@kaynooo+utils@2.31.2+d0769e28dc4dad16/node_modules/@kaynooo/utils/dist/chunk-mtetydvc.js
|
|
15
|
+
function x(s) {
|
|
16
|
+
let r = s.startsWith("#") ? s.slice(1).toLowerCase() : s.toLowerCase();
|
|
17
|
+
if (!/^[a-f0-9]+$/.test(r))
|
|
18
|
+
return [0, 0, 0];
|
|
19
|
+
let t = r.length, n = (c) => Number.parseInt(c, 16);
|
|
20
|
+
if (t === 3 || t === 4) {
|
|
21
|
+
let c = n(r[0] + r[0]), u = n(r[1] + r[1]), h = n(r[2] + r[2]), g = t === 4 ? n(r[3] + r[3]) : undefined;
|
|
22
|
+
return g !== undefined ? [c, u, h, g] : [c, u, h];
|
|
23
|
+
}
|
|
24
|
+
if (t !== 6 && t !== 8)
|
|
25
|
+
throw Error(`Invalid hex color length: ${t}`);
|
|
26
|
+
let a = n(r.slice(0, 2)), b = n(r.slice(2, 4)), o = n(r.slice(4, 6)), y = t === 8 ? n(r.slice(6, 8)) : undefined;
|
|
27
|
+
if ([a, b, o].some(Number.isNaN) || y !== undefined && Number.isNaN(y))
|
|
28
|
+
throw Error(`Invalid hex color value: ${s}`);
|
|
29
|
+
return y !== undefined ? [a, b, o, y] : [a, b, o];
|
|
30
|
+
}
|
|
31
|
+
function A(s) {
|
|
32
|
+
let [r, t, n, a] = s;
|
|
33
|
+
if (a)
|
|
34
|
+
return `#${(r << 24 | t << 16 | n << 8 | Math.round((a ?? 1) * 255)).toString(16).padStart(8, "0").toUpperCase()}`;
|
|
35
|
+
return `#${(r << 16 | t << 8 | n).toString(16).padStart(6, "0").toUpperCase()}`;
|
|
36
|
+
}
|
|
37
|
+
function R(s, r, t) {
|
|
38
|
+
s = (s % 360 + 360) % 360, t /= 100;
|
|
39
|
+
let n = r * Math.min(t, 1 - t) / 100, a = (b) => {
|
|
40
|
+
let o = (b + s / 30) % 12, y = t - n * Math.max(Math.min(o - 3, 9 - o, 1), -1);
|
|
41
|
+
return Math.round(255 * y);
|
|
42
|
+
};
|
|
43
|
+
return [a(0), a(8), a(4)];
|
|
44
|
+
}
|
|
45
|
+
function k(s) {
|
|
46
|
+
let [r, t, n] = s;
|
|
47
|
+
r /= 255, t /= 255, n /= 255;
|
|
48
|
+
let a = Math.max(r, t, n), b = Math.min(r, t, n), o = 0, y = 0, c = (a + b) / 2;
|
|
49
|
+
if (a !== b) {
|
|
50
|
+
let u = a - b;
|
|
51
|
+
if (y = c > 0 && c < 1 ? u / (1 - Math.abs(2 * c - 1)) : 0, a === r)
|
|
52
|
+
o = (t - n) / u + (t < n ? 6 : 0);
|
|
53
|
+
else if (a === t)
|
|
54
|
+
o = (n - r) / u + 2;
|
|
55
|
+
else
|
|
56
|
+
o = (r - t) / u + 4;
|
|
57
|
+
o /= 6;
|
|
58
|
+
}
|
|
59
|
+
return [Math.round(o * 360), Math.round(y * 100), Math.round(c * 100)];
|
|
60
|
+
}
|
|
61
|
+
function H(s, r, t) {
|
|
62
|
+
s = (s % 360 + 360) % 360, r /= 100, t /= 100;
|
|
63
|
+
let n = (a) => {
|
|
64
|
+
let b = (a + s / 60) % 6, o = t - t * r * Math.max(Math.min(b, 4 - b, 1), 0);
|
|
65
|
+
return Math.round(255 * o);
|
|
66
|
+
};
|
|
67
|
+
return [n(5), n(3), n(1)];
|
|
68
|
+
}
|
|
69
|
+
function f(s) {
|
|
70
|
+
let [r, t, n] = s;
|
|
71
|
+
r /= 255, t /= 255, n /= 255;
|
|
72
|
+
let a = Math.max(r, t, n), b = Math.min(r, t, n), o = a - b, y = 0, c = a === 0 ? 0 : o / a, u = a;
|
|
73
|
+
if (o !== 0) {
|
|
74
|
+
if (a === r)
|
|
75
|
+
y = (t - n) / o + (t < n ? 6 : 0);
|
|
76
|
+
else if (a === t)
|
|
77
|
+
y = (n - r) / o + 2;
|
|
78
|
+
else
|
|
79
|
+
y = (r - t) / o + 4;
|
|
80
|
+
y /= 6;
|
|
81
|
+
}
|
|
82
|
+
return [Math.round(y * 360), Math.round(c * 100), Math.round(u * 100)];
|
|
83
|
+
}
|
|
84
|
+
function O(s) {
|
|
85
|
+
let r = s[0] / 255, t = s[1] / 255, n = s[2] / 255, a = 1 - Math.max(r, t, n);
|
|
86
|
+
if (a === 1)
|
|
87
|
+
return [0, 0, 0, 100];
|
|
88
|
+
let b = (1 - r - a) / (1 - a), o = (1 - t - a) / (1 - a), y = (1 - n - a) / (1 - a);
|
|
89
|
+
return [Math.round(b * 100), Math.round(o * 100), Math.round(y * 100), Math.round(a * 100)];
|
|
90
|
+
}
|
|
91
|
+
function v(s, r, t, n) {
|
|
92
|
+
return s /= 100, r /= 100, t /= 100, n /= 100, [Math.round(255 * (1 - s) * (1 - n)), Math.round(255 * (1 - r) * (1 - n)), Math.round(255 * (1 - t) * (1 - n))];
|
|
93
|
+
}
|
|
94
|
+
var m = (s) => s <= 0.04045 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
|
|
95
|
+
var p = (s) => s <= 0.0031308 ? 12.92 * s : 1.055 * s ** 0.4166666666666667 - 0.055;
|
|
96
|
+
var i = (s, r) => Math.round(s * 10 ** r) / 10 ** r;
|
|
97
|
+
function T(s) {
|
|
98
|
+
let r = m(s[0] / 255), t = m(s[1] / 255), n = m(s[2] / 255), a = Math.cbrt(0.4122214708 * r + 0.5363325363 * t + 0.0514459929 * n), b = Math.cbrt(0.2119034982 * r + 0.6806995451 * t + 0.1073969566 * n), o = Math.cbrt(0.0883024619 * r + 0.2817188376 * t + 0.6299787005 * n);
|
|
99
|
+
return [i(0.2104542553 * a + 0.793617785 * b - 0.0040720468 * o, 4), i(1.9779984951 * a - 2.428592205 * b + 0.4505937099 * o, 4), i(0.0259040371 * a + 0.7827717662 * b - 0.808675766 * o, 4)];
|
|
100
|
+
}
|
|
101
|
+
function M(s, r, t) {
|
|
102
|
+
let n = (s + 0.3963377774 * r + 0.2158037573 * t) ** 3, a = (s - 0.1055613458 * r - 0.0638541728 * t) ** 3, b = (s - 0.0894841775 * r - 1.291485548 * t) ** 3, o = p(4.0767416621 * n - 3.3077115913 * a + 0.2309699292 * b), y = p(-1.2684380046 * n + 2.6097574011 * a - 0.3413193965 * b), c = p(-0.0041960863 * n - 0.7034186147 * a + 1.707614701 * b), u = (h) => Math.min(255, Math.max(0, Math.round(h * 255)));
|
|
103
|
+
return [u(o), u(y), u(c)];
|
|
104
|
+
}
|
|
105
|
+
function d(s) {
|
|
106
|
+
let [r, t, n] = T(s), a = Math.sqrt(t * t + n * n), b = (Math.atan2(n, t) * 180 / Math.PI + 360) % 360;
|
|
107
|
+
return [i(r, 4), i(a, 4), i(b, 2)];
|
|
108
|
+
}
|
|
109
|
+
function C(s, r, t) {
|
|
110
|
+
let n = t * Math.PI / 180;
|
|
111
|
+
return M(s, r * Math.cos(n), r * Math.sin(n));
|
|
112
|
+
}
|
|
113
|
+
function l(s) {
|
|
114
|
+
let r = s / 255;
|
|
115
|
+
return r <= 0.03928 ? r / 12.92 : ((r + 0.055) / 1.055) ** 2.4;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
class e {
|
|
119
|
+
r;
|
|
120
|
+
g;
|
|
121
|
+
b;
|
|
122
|
+
a;
|
|
123
|
+
constructor(s, r, t, n = 1) {
|
|
124
|
+
this.r = s, this.g = r, this.b = t, this.a = n;
|
|
125
|
+
}
|
|
126
|
+
static from(s, r) {
|
|
127
|
+
if (s === "hex") {
|
|
128
|
+
let [t, n, a, b] = x(r);
|
|
129
|
+
return new e(t, n, a, b);
|
|
130
|
+
}
|
|
131
|
+
if (s === "rgb") {
|
|
132
|
+
if (Array.isArray(r)) {
|
|
133
|
+
let [o, y, c, u] = r;
|
|
134
|
+
return new e(o, y, c, u);
|
|
135
|
+
}
|
|
136
|
+
let { r: t, g: n, b: a, a: b } = r;
|
|
137
|
+
return new e(t, n, a, b);
|
|
138
|
+
}
|
|
139
|
+
if (s === "hsl") {
|
|
140
|
+
let t, n, a, b;
|
|
141
|
+
if (Array.isArray(r))
|
|
142
|
+
[t, n, a, b] = r;
|
|
143
|
+
else
|
|
144
|
+
({ h: t, s: n, l: a, a: b } = r);
|
|
145
|
+
let [o, y, c] = R(t, n, a);
|
|
146
|
+
return new e(o, y, c, b);
|
|
147
|
+
}
|
|
148
|
+
if (s === "hsv") {
|
|
149
|
+
let t, n, a, b;
|
|
150
|
+
if (Array.isArray(r))
|
|
151
|
+
[t, n, a, b] = r;
|
|
152
|
+
else
|
|
153
|
+
({ h: t, s: n, v: a, a: b } = r);
|
|
154
|
+
let [o, y, c] = H(t, n, a);
|
|
155
|
+
return new e(o, y, c, b);
|
|
156
|
+
}
|
|
157
|
+
if (s === "cmyk") {
|
|
158
|
+
let t, n, a, b, o;
|
|
159
|
+
if (Array.isArray(r))
|
|
160
|
+
[t, n, a, b, o] = r;
|
|
161
|
+
else
|
|
162
|
+
({ c: t, m: n, y: a, k: b, a: o } = r);
|
|
163
|
+
let [y, c, u] = v(t, n, a, b);
|
|
164
|
+
return new e(y, c, u, o);
|
|
165
|
+
}
|
|
166
|
+
if (s === "oklch") {
|
|
167
|
+
let t, n, a, b;
|
|
168
|
+
if (Array.isArray(r))
|
|
169
|
+
[t, n, a, b] = r;
|
|
170
|
+
else
|
|
171
|
+
({ l: t, c: n, h: a, a: b } = r);
|
|
172
|
+
let [o, y, c] = C(t, n, a);
|
|
173
|
+
return new e(o, y, c, b);
|
|
174
|
+
}
|
|
175
|
+
if (s === "oklab") {
|
|
176
|
+
let t, n, a, b;
|
|
177
|
+
if (Array.isArray(r))
|
|
178
|
+
[t, n, a, b] = r;
|
|
179
|
+
else
|
|
180
|
+
({ l: t, a: n, b: a, alpha: b } = r);
|
|
181
|
+
let [o, y, c] = M(t, n, a);
|
|
182
|
+
return new e(o, y, c, b);
|
|
183
|
+
}
|
|
184
|
+
throw Error("Invalid input");
|
|
185
|
+
}
|
|
186
|
+
to(s) {
|
|
187
|
+
switch (s) {
|
|
188
|
+
case "rgb":
|
|
189
|
+
return [this.r, this.g, this.b];
|
|
190
|
+
case "rgba":
|
|
191
|
+
return [this.r, this.g, this.b, this.a];
|
|
192
|
+
case "hex":
|
|
193
|
+
return A([this.r, this.g, this.b]);
|
|
194
|
+
case "hexa":
|
|
195
|
+
return A([this.r, this.g, this.b, this.a]);
|
|
196
|
+
case "hsl":
|
|
197
|
+
return k([this.r, this.g, this.b]);
|
|
198
|
+
case "hsla":
|
|
199
|
+
return [...k([this.r, this.g, this.b]), this.a];
|
|
200
|
+
case "hsv":
|
|
201
|
+
return f([this.r, this.g, this.b]);
|
|
202
|
+
case "hsva":
|
|
203
|
+
return [...f([this.r, this.g, this.b]), this.a];
|
|
204
|
+
case "cmyk":
|
|
205
|
+
return O([this.r, this.g, this.b]);
|
|
206
|
+
case "cmyka":
|
|
207
|
+
return [...O([this.r, this.g, this.b]), this.a];
|
|
208
|
+
case "oklch":
|
|
209
|
+
return d([this.r, this.g, this.b]);
|
|
210
|
+
case "oklcha":
|
|
211
|
+
return [...d([this.r, this.g, this.b]), this.a];
|
|
212
|
+
case "oklab":
|
|
213
|
+
return T([this.r, this.g, this.b]);
|
|
214
|
+
case "oklaba":
|
|
215
|
+
return [...T([this.r, this.g, this.b]), this.a];
|
|
216
|
+
default:
|
|
217
|
+
return [this.r, this.g, this.b];
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
getLuminance() {
|
|
221
|
+
let s = l(this.r), r = l(this.g), t = l(this.b);
|
|
222
|
+
return 0.2126 * s + 0.7152 * r + 0.0722 * t;
|
|
223
|
+
}
|
|
224
|
+
contrastRatio(s) {
|
|
225
|
+
let r = this.getLuminance(), t = s.getLuminance(), [n, a] = r >= t ? [r, t] : [t, r];
|
|
226
|
+
return (n + 0.05) / (a + 0.05);
|
|
227
|
+
}
|
|
228
|
+
bestContrastForeground() {
|
|
229
|
+
let s = new e(255, 255, 255, 1), r = new e(0, 0, 0, 1), t = this.contrastRatio(s), n = this.contrastRatio(r);
|
|
230
|
+
return t >= n ? s : r;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// ../../node_modules/.bun/@kaynooo+utils@2.31.2+d0769e28dc4dad16/node_modules/@kaynooo/utils/dist/chunk-y1msbssb.js
|
|
235
|
+
var r = 39;
|
|
236
|
+
var o = 49;
|
|
237
|
+
var f2 = "\x1B[";
|
|
238
|
+
var C2 = { reset: [0, 0], bold: [1, 22], dim: [2, 22], italic: [3, 23], underline: [4, 24], inverse: [7, 27], hidden: [8, 28], strikethrough: [9, 29], black: [30, r], red: [31, r], green: [32, r], yellow: [33, r], blue: [34, r], magenta: [35, r], cyan: [36, r], white: [37, r], gray: [90, r], grey: [90, r], blackBright: [90, r], redBright: [91, r], greenBright: [92, r], yellowBright: [93, r], blueBright: [94, r], magentaBright: [95, r], cyanBright: [96, r], whiteBright: [97, r], bgBlack: [40, o], bgRed: [41, o], bgGreen: [42, o], bgYellow: [43, o], bgBlue: [44, o], bgMagenta: [45, o], bgCyan: [46, o], bgWhite: [47, o], bgBlackBright: [100, o], bgRedBright: [101, o], bgGreenBright: [102, o], bgYellowBright: [103, o], bgBlueBright: [104, o], bgMagentaBright: [105, o], bgCyanBright: [106, o], bgWhiteBright: [107, o], rgb: (i, n, a, l = "fg") => [`${l === "fg" ? 38 : 48};2;${i};${n};${a}`, l === "fg" ? r : o], hex: (i, n = "fg") => {
|
|
239
|
+
let [a, l, s] = e.from("hex", i).to("rgb");
|
|
240
|
+
return [`${n === "fg" ? 38 : 48};2;${a};${l};${s}`, n === "fg" ? r : o];
|
|
241
|
+
}, ansi256: (i, n = "fg") => [`${n === "fg" ? 38 : 48};5;${i}`, n === "fg" ? r : o] };
|
|
242
|
+
var b = new Set(["", "1", "2", "3", "true"]);
|
|
243
|
+
function h(i, n = globalThis.process?.env ?? {}) {
|
|
244
|
+
if (n.FORCE_COLOR !== undefined)
|
|
245
|
+
return b.has(n.FORCE_COLOR);
|
|
246
|
+
if (n.NO_COLOR !== undefined || n.NODE_DISABLE_COLORS !== undefined || n.TERM === "dumb")
|
|
247
|
+
return false;
|
|
248
|
+
return i?.isTTY === true;
|
|
249
|
+
}
|
|
250
|
+
function d2(i, n, a) {
|
|
251
|
+
if (!h(a.stream ?? globalThis.process?.stdout, a.env))
|
|
252
|
+
return n;
|
|
253
|
+
let l = "", s = "";
|
|
254
|
+
for (let g = 0, y = i.length;g < y; g++) {
|
|
255
|
+
let [c, p] = i[g];
|
|
256
|
+
l += `${f2}${c}m`, s = `${f2}${p}m${s}`;
|
|
257
|
+
}
|
|
258
|
+
return l + n + s;
|
|
259
|
+
}
|
|
260
|
+
function u(i, n = []) {
|
|
261
|
+
return new Proxy((l) => d2(n, l, i), { get(l, s) {
|
|
262
|
+
let g = C2[s];
|
|
263
|
+
if (Array.isArray(g))
|
|
264
|
+
return u(i, [...n, g]);
|
|
265
|
+
if (typeof g === "function")
|
|
266
|
+
return (...y) => u(i, [...n, g(...y)]);
|
|
267
|
+
throw Error(`Unknown style: ${String(s)}`);
|
|
268
|
+
} });
|
|
269
|
+
}
|
|
270
|
+
function le(i = {}) {
|
|
271
|
+
return u(i);
|
|
272
|
+
}
|
|
273
|
+
var t = le();
|
|
274
|
+
|
|
275
|
+
// ../kit/src/style.ts
|
|
276
|
+
function takesControlCodes(stream, env = process.env) {
|
|
277
|
+
return stream.isTTY === true && env.NO_COLOR === undefined && env.NODE_DISABLE_COLORS === undefined && env.TERM !== "dumb";
|
|
278
|
+
}
|
|
279
|
+
function paint(stream, env = process.env) {
|
|
280
|
+
return le({ stream: { isTTY: takesControlCodes(stream, env) }, env });
|
|
281
|
+
}
|
|
282
|
+
var ROLES = {
|
|
283
|
+
accent: (colors) => colors.cyan,
|
|
284
|
+
id: (colors) => colors.yellow,
|
|
285
|
+
path: (colors) => colors.blue,
|
|
286
|
+
label: (colors) => colors.dim,
|
|
287
|
+
value: (colors) => colors.bold
|
|
288
|
+
};
|
|
289
|
+
function role(name, stream, env = process.env) {
|
|
290
|
+
return ROLES[name](paint(stream, env));
|
|
291
|
+
}
|
|
292
|
+
var STATUSES = {
|
|
293
|
+
passed: { symbol: "\u2713", words: ["passed", "success", "succeeded", "ok"], paint: (colors) => colors.green },
|
|
294
|
+
failed: { symbol: "\u2717", words: ["failed", "failure", "error", "errored"], paint: (colors) => colors.red },
|
|
295
|
+
running: { symbol: "\u25CF", words: ["running", "in_progress", "started"], paint: (colors) => colors.cyan },
|
|
296
|
+
queued: { symbol: "\u25CB", words: ["queued", "waiting", "pending", "created", "blocked", "scheduled"], paint: (colors) => colors.yellow },
|
|
297
|
+
skipped: { symbol: "-", words: ["skipped", "cancelled", "canceled", "manual"], paint: (colors) => colors.dim }
|
|
298
|
+
};
|
|
299
|
+
var STATUS_OF = new Map(Object.entries(STATUSES).flatMap(([status, { words }]) => words.map((word) => [word, status])));
|
|
300
|
+
function link(text, url, stream, env = process.env) {
|
|
301
|
+
if (takesControlCodes(stream, env))
|
|
302
|
+
return `\x1B]8;;${url}\x1B\\${text}\x1B]8;;\x1B\\`;
|
|
303
|
+
return text === url ? text : `${text} (${url})`;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// ../kit/src/format.ts
|
|
307
|
+
function upTo(text, separator) {
|
|
308
|
+
const end = text.indexOf(separator);
|
|
309
|
+
return end === -1 ? text : text.slice(0, end);
|
|
310
|
+
}
|
|
311
|
+
var ESCAPE = String.fromCharCode(27);
|
|
312
|
+
function textWidth(text) {
|
|
313
|
+
return Bun.stringWidth(text);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// ../kit/src/text.ts
|
|
317
|
+
var ANSI = new RegExp(`${String.fromCharCode(27)}\\[[\\d;?]*[a-z]`, "gi");
|
|
318
|
+
function stripAnsi(text) {
|
|
319
|
+
return text.replace(ANSI, "");
|
|
320
|
+
}
|
|
321
|
+
function distance(a, b) {
|
|
322
|
+
let previous = Array.from({ length: b.length + 1 }, (_, index) => index);
|
|
323
|
+
for (const [i, left] of [...a].entries()) {
|
|
324
|
+
const current = [i + 1];
|
|
325
|
+
for (const [j, right] of [...b].entries())
|
|
326
|
+
current.push(Math.min((previous[j + 1] ?? 0) + 1, (current[j] ?? 0) + 1, (previous[j] ?? 0) + (left === right ? 0 : 1)));
|
|
327
|
+
previous = current;
|
|
328
|
+
}
|
|
329
|
+
return previous[b.length] ?? 0;
|
|
330
|
+
}
|
|
331
|
+
function closest(name, names) {
|
|
332
|
+
const started = names.find((known) => known.startsWith(name));
|
|
333
|
+
if (started !== undefined)
|
|
334
|
+
return started;
|
|
335
|
+
const [best] = names.map((known) => ({ known, far: distance(name, known) })).sort((a, b) => a.far - b.far);
|
|
336
|
+
return best !== undefined && best.far <= (name.length < 4 ? 1 : 2) ? best.known : undefined;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// ../kit/src/args.ts
|
|
340
|
+
class Exit extends Error {
|
|
341
|
+
code;
|
|
342
|
+
hint;
|
|
343
|
+
constructor(message, code, hint) {
|
|
344
|
+
super(message);
|
|
345
|
+
this.code = code;
|
|
346
|
+
this.hint = hint;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
function fail(message, code = 2, hint) {
|
|
350
|
+
throw new Exit(message, code, hint);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
class UsageError extends Exit {
|
|
354
|
+
constructor(message, hint) {
|
|
355
|
+
super(message, 2, hint);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
function badUsage(message) {
|
|
359
|
+
throw new UsageError(message);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
class HelpShown extends Exit {
|
|
363
|
+
constructor() {
|
|
364
|
+
super("help shown", 0);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
class Completing {
|
|
369
|
+
options;
|
|
370
|
+
positionals;
|
|
371
|
+
candidates;
|
|
372
|
+
constructor(options, positionals = [], candidates = {}) {
|
|
373
|
+
this.options = options;
|
|
374
|
+
this.positionals = positionals;
|
|
375
|
+
this.candidates = candidates;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
var completion = { active: false };
|
|
379
|
+
var OPTIONAL = /\[[^\]]+\]/g;
|
|
380
|
+
var USAGE_PREFIX = /^usage:\s*/i;
|
|
381
|
+
var FULL_STOP = /\.$/;
|
|
382
|
+
var DASHES = /^-+/;
|
|
383
|
+
function usageLine(usage, colors = paint(process2.stdout)) {
|
|
384
|
+
return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
|
|
385
|
+
}
|
|
386
|
+
function helpText(usage, options, colors = paint(process2.stdout), { examples = [], replaces } = {}) {
|
|
387
|
+
return [usageLine(usage, colors), ...replacesRows(replaces, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exampleRows(examples, colors)].join(`
|
|
388
|
+
`);
|
|
389
|
+
}
|
|
390
|
+
function replacesRows(replaces, colors = paint(process2.stdout)) {
|
|
391
|
+
return replaces === undefined ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
392
|
+
}
|
|
393
|
+
function wantsHelpJson(env = process2.env) {
|
|
394
|
+
return env.KIT_HELP === "json";
|
|
395
|
+
}
|
|
396
|
+
function description(usage, { examples = [], replaces } = {}) {
|
|
397
|
+
const lines = usage.replace(USAGE_PREFIX, "").split(`
|
|
398
|
+
`);
|
|
399
|
+
const first = lines[0];
|
|
400
|
+
const colon = first.indexOf(": ");
|
|
401
|
+
const prose = lines[lines.indexOf("") + 1] ?? "";
|
|
402
|
+
const fallback = lines.includes("") ? `${prose.charAt(0).toLowerCase()}${prose.slice(1)}`.replace(FULL_STOP, "") : "";
|
|
403
|
+
return {
|
|
404
|
+
synopsis: colon === -1 ? first : first.slice(0, colon),
|
|
405
|
+
summary: colon === -1 ? fallback : first.slice(colon + 2),
|
|
406
|
+
...replaces === undefined ? {} : { replaces },
|
|
407
|
+
examples: examples.map((example) => typeof example === "string" ? { line: example } : { line: example[0], note: example[1] })
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
function exampleRows(examples, colors = paint(process2.stdout)) {
|
|
411
|
+
if (examples.length === 0)
|
|
412
|
+
return [];
|
|
413
|
+
return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
|
|
414
|
+
}
|
|
415
|
+
function optionRows(options, colors = paint(process2.stdout)) {
|
|
416
|
+
const rows = Object.entries(options).map(([name, option]) => {
|
|
417
|
+
const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
|
|
418
|
+
const named = option.choices === undefined ? "<value>" : `<${option.choices.join("|")}>`;
|
|
419
|
+
const placeholder = option.optional === undefined ? named : `[${named}]`;
|
|
420
|
+
const value = option.type === "string" ? ` ${colors.dim(placeholder)}` : "";
|
|
421
|
+
return { flag: `${short}${colors.cyan(`--${name}`)}${value}`, width: `-x, --${name}`.length + (option.type === "string" ? placeholder.length + 1 : 0), description: option.description ?? "" };
|
|
422
|
+
});
|
|
423
|
+
const width = Math.max(0, ...rows.map((row) => row.width));
|
|
424
|
+
return rows.map((row) => row.description === "" ? ` ${row.flag}` : ` ${row.flag}${" ".repeat(width - row.width)} ${colors.dim(row.description)}`);
|
|
425
|
+
}
|
|
426
|
+
var OUTPUT_OPTIONS = {
|
|
427
|
+
color: { type: "string", description: "auto (on a terminal alone), always or never" },
|
|
428
|
+
plain: { type: "boolean", description: "plain text, no colour or spinner: --color never" }
|
|
429
|
+
};
|
|
430
|
+
function applyOutput(color, plain, env = process2.env) {
|
|
431
|
+
const when = plain === true ? "never" : color ?? "auto";
|
|
432
|
+
if (when === "auto")
|
|
433
|
+
return;
|
|
434
|
+
if (when !== "always" && when !== "never")
|
|
435
|
+
badUsage(`--color takes auto, always or never, not ${when}`);
|
|
436
|
+
if (when === "always") {
|
|
437
|
+
env.FORCE_COLOR = "1";
|
|
438
|
+
delete env.NO_COLOR;
|
|
439
|
+
} else {
|
|
440
|
+
env.NO_COLOR = "1";
|
|
441
|
+
delete env.FORCE_COLOR;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
function parse(args, usage, options, settings = {}) {
|
|
445
|
+
const output = Object.fromEntries(Object.entries(OUTPUT_OPTIONS).filter(([name]) => !(name in options)));
|
|
446
|
+
const all = { ...options, ...output, help: { type: "boolean", short: "h", description: "show this help" } };
|
|
447
|
+
const listing = listed(all, settings.allowNegative === true);
|
|
448
|
+
if (completion.active)
|
|
449
|
+
throw new Completing(listing, settings.positionals, settings.candidates);
|
|
450
|
+
const { own, rest } = settings.trailing === true ? splitTrailing(args, all, settings.positionals?.length ?? 0) : splitRest(args, settings.rest === true);
|
|
451
|
+
let parsed;
|
|
452
|
+
try {
|
|
453
|
+
parsed = parseArgs({ args: joinDashValues(withBare(own, all), all), options: all, allowPositionals: true, allowNegative: settings.allowNegative === true, strict: true });
|
|
454
|
+
} catch (error) {
|
|
455
|
+
const message = parseError(error);
|
|
456
|
+
throw new UsageError(message, nearestOption(message, all, settings.allowNegative === true));
|
|
457
|
+
}
|
|
458
|
+
const values = parsed.values;
|
|
459
|
+
applyOutput("color" in output ? values.color : undefined, "plain" in output ? values.plain : undefined);
|
|
460
|
+
if (values.help === true) {
|
|
461
|
+
console.log(wantsHelpJson() ? JSON.stringify(description(usage, settings)) : helpText(usage, listing, undefined, settings));
|
|
462
|
+
throw new HelpShown;
|
|
463
|
+
}
|
|
464
|
+
checkChoices(parsed.values, all);
|
|
465
|
+
checkConflicts(parsed.values, all);
|
|
466
|
+
if (settings.positionals !== undefined)
|
|
467
|
+
checkPositionals(parsed.positionals, settings.positionals);
|
|
468
|
+
return { ...parsed, values: parsed.values, positionals: parsed.positionals, rest };
|
|
469
|
+
}
|
|
470
|
+
function splitRest(args, passes) {
|
|
471
|
+
const separator = passes ? args.indexOf("--") : -1;
|
|
472
|
+
return { own: separator === -1 ? args : args.slice(0, separator), rest: separator === -1 ? undefined : args.slice(separator + 1) };
|
|
473
|
+
}
|
|
474
|
+
function splitTrailing(args, options, count) {
|
|
475
|
+
const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
|
|
476
|
+
let seen = 0;
|
|
477
|
+
if (count === 0)
|
|
478
|
+
return { own: args, rest: undefined };
|
|
479
|
+
for (let at = 0;at < args.length; at++) {
|
|
480
|
+
const word = args[at];
|
|
481
|
+
if (word === "--")
|
|
482
|
+
break;
|
|
483
|
+
if (word.startsWith("-")) {
|
|
484
|
+
if (takesValue(word))
|
|
485
|
+
at++;
|
|
486
|
+
continue;
|
|
487
|
+
}
|
|
488
|
+
seen++;
|
|
489
|
+
if (seen === count)
|
|
490
|
+
return { own: args.slice(0, at + 1), rest: args.slice(at + 1) };
|
|
491
|
+
}
|
|
492
|
+
return { own: args, rest: undefined };
|
|
493
|
+
}
|
|
494
|
+
function listed(options, allowNegative) {
|
|
495
|
+
if (!allowNegative)
|
|
496
|
+
return options;
|
|
497
|
+
return Object.fromEntries(Object.entries(options).flatMap(([name, option]) => {
|
|
498
|
+
if (option.type !== "boolean" || option.negative === undefined)
|
|
499
|
+
return [[name, option]];
|
|
500
|
+
return [...option.description === undefined ? [] : [[name, option]], [`no-${name}`, { type: "boolean", description: option.negative }]];
|
|
501
|
+
}));
|
|
502
|
+
}
|
|
503
|
+
function withBare(args, options) {
|
|
504
|
+
const bare = new Map(Object.entries(options).flatMap(([name, { short, optional }]) => optional === undefined ? [] : [[`--${name}`, optional], ...short === undefined ? [] : [[`-${short}`, optional]]]));
|
|
505
|
+
if (bare.size === 0)
|
|
506
|
+
return args;
|
|
507
|
+
const separator = args.indexOf("--");
|
|
508
|
+
const own = separator === -1 ? args : args.slice(0, separator);
|
|
509
|
+
return [...own.flatMap((arg, index) => {
|
|
510
|
+
const optional = bare.get(arg);
|
|
511
|
+
const next = own[index + 1];
|
|
512
|
+
const value = next !== undefined && (optional?.accepts === undefined ? !next.startsWith("-") : optional.accepts.test(next));
|
|
513
|
+
return optional === undefined || value ? [arg] : [arg, optional.bare];
|
|
514
|
+
}), ...separator === -1 ? [] : args.slice(separator)];
|
|
515
|
+
}
|
|
516
|
+
function checkConflicts(values, options) {
|
|
517
|
+
const given = (name) => values[name] !== undefined && values[name] !== false;
|
|
518
|
+
for (const [name, { conflicts = [] }] of Object.entries(options)) {
|
|
519
|
+
const other = given(name) ? conflicts.find(given) : undefined;
|
|
520
|
+
if (other !== undefined)
|
|
521
|
+
badUsage(`--${name} cannot be used with --${other}`);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
function checkChoices(values, options) {
|
|
525
|
+
for (const [name, option] of Object.entries(options)) {
|
|
526
|
+
if (option.choices === undefined)
|
|
527
|
+
continue;
|
|
528
|
+
const given = values[name];
|
|
529
|
+
for (const value of Array.isArray(given) ? given : [given]) {
|
|
530
|
+
if (typeof value === "string")
|
|
531
|
+
oneOf(value, name, option.choices);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
function oneOf(value, name, choices) {
|
|
536
|
+
if (!choices.includes(value))
|
|
537
|
+
badUsage(`${name.startsWith("<") ? name : `--${name}`} takes one of ${choices.join(", ")}, not ${value}`);
|
|
538
|
+
return value;
|
|
539
|
+
}
|
|
540
|
+
function checkPositionals(given, names) {
|
|
541
|
+
const variadic = names.at(-1)?.replace("]", "").endsWith("...") === true;
|
|
542
|
+
const required = names.filter((name) => !name.startsWith("[")).length;
|
|
543
|
+
if (given.length < required)
|
|
544
|
+
badUsage(`missing ${names[given.length]}`);
|
|
545
|
+
if (!variadic && given.length > names.length)
|
|
546
|
+
badUsage(`unexpected argument ${given[names.length]}`);
|
|
547
|
+
}
|
|
548
|
+
function joinDashValues(args, options) {
|
|
549
|
+
const longOf = new Map(Object.entries(options).flatMap(([name, option]) => option.short === undefined ? [[`--${name}`, name]] : [[`--${name}`, name], [`-${option.short}`, name]]));
|
|
550
|
+
const isOption = (arg) => arg === "--" || longOf.has(arg.includes("=") ? arg.slice(0, arg.indexOf("=")) : arg);
|
|
551
|
+
const joined = [];
|
|
552
|
+
for (let i = 0;i < args.length; i++) {
|
|
553
|
+
const arg = args[i] ?? "";
|
|
554
|
+
if (arg === "--")
|
|
555
|
+
return [...joined, ...args.slice(i)];
|
|
556
|
+
const name = longOf.get(arg);
|
|
557
|
+
const next = args[i + 1];
|
|
558
|
+
if (name !== undefined && options[name]?.type === "string" && next !== undefined && next.startsWith("-") && next !== "-" && !isOption(next)) {
|
|
559
|
+
joined.push(`--${name}=${next}`);
|
|
560
|
+
i++;
|
|
561
|
+
} else {
|
|
562
|
+
joined.push(arg);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
return joined;
|
|
566
|
+
}
|
|
567
|
+
function parseError(error) {
|
|
568
|
+
const message = upTo(error instanceof Error ? error.message : String(error), ". ");
|
|
569
|
+
return `${message.charAt(0).toLowerCase()}${message.slice(1)}`;
|
|
570
|
+
}
|
|
571
|
+
var UNKNOWN_OPTION = /^unknown option '(-[^']+)'/;
|
|
572
|
+
function nearestOption(message, options, allowNegative) {
|
|
573
|
+
const typed = UNKNOWN_OPTION.exec(message)?.[1];
|
|
574
|
+
if (typed === undefined)
|
|
575
|
+
return;
|
|
576
|
+
const word = typed.replace(DASHES, "");
|
|
577
|
+
if (word.length < 2 && !typed.startsWith("--"))
|
|
578
|
+
return;
|
|
579
|
+
const known = Object.entries(options).flatMap(([name, option]) => [
|
|
580
|
+
...option.short === undefined ? [] : [`-${option.short}`],
|
|
581
|
+
`--${name}`,
|
|
582
|
+
...allowNegative && option.type === "boolean" ? [`--no-${name}`] : []
|
|
583
|
+
]);
|
|
584
|
+
const meant = closest(word, known.map((flag) => flag.replace(DASHES, "")));
|
|
585
|
+
return meant === undefined ? undefined : `did you mean ${known.find((flag) => flag.replace(DASHES, "") === meant)}?`;
|
|
586
|
+
}
|
|
587
|
+
var WHOLE = /^\d+$/;
|
|
588
|
+
function wholeNumber(value, name, fallback, min = 1) {
|
|
589
|
+
if (value === undefined)
|
|
590
|
+
return fallback;
|
|
591
|
+
const number = Number(value);
|
|
592
|
+
if (!WHOLE.test(value) || !Number.isSafeInteger(number) || number < min)
|
|
593
|
+
badUsage(`${name.startsWith("<") ? name : `--${name}`} takes a whole number from ${min}, not ${value}`);
|
|
594
|
+
return number;
|
|
595
|
+
}
|
|
596
|
+
async function readInput(path, { noStdin = "nothing is piped on stdin" } = {}) {
|
|
597
|
+
if (path === "-") {
|
|
598
|
+
if (process2.stdin.isTTY)
|
|
599
|
+
fail(noStdin);
|
|
600
|
+
return Bun.stdin.text();
|
|
601
|
+
}
|
|
602
|
+
const file = Bun.file(path);
|
|
603
|
+
if (!await file.exists())
|
|
604
|
+
fail(`no file ${path}`);
|
|
605
|
+
return file.text();
|
|
606
|
+
}
|
|
607
|
+
// ../kit/src/progress.ts
|
|
608
|
+
var ESC = String.fromCharCode(27);
|
|
609
|
+
var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x7E]|\\][^\\u0007${ESC}]*(?:\\u0007|${ESC}\\\\))`, "y");
|
|
610
|
+
var RESET_STYLE = `${ESC}[0m`;
|
|
611
|
+
var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
612
|
+
// ../kit/src/cli.ts
|
|
613
|
+
import process3 from "process";
|
|
614
|
+
var LEVEL_OPTIONS = {
|
|
615
|
+
...OUTPUT_OPTIONS,
|
|
616
|
+
help: { type: "boolean", short: "h", description: "show this help" }
|
|
617
|
+
};
|
|
618
|
+
var TOP_OPTIONS = { ...LEVEL_OPTIONS, version: { type: "boolean", short: "v", description: "print the version" } };
|
|
619
|
+
function printError(prefix, message, hint, cause) {
|
|
620
|
+
const colors = paint(process3.stderr);
|
|
621
|
+
const styled = takesControlCodes(process3.stderr);
|
|
622
|
+
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
623
|
+
const under = (label, text, tint) => text === undefined ? "" : `
|
|
624
|
+
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
625
|
+
console.error(`${head} ${message}${under("cause", cause, colors.dim)}${under("hint", hint, colors.yellow)}`);
|
|
626
|
+
}
|
|
627
|
+
function printJsonError(message, hint, cause) {
|
|
628
|
+
console.error(JSON.stringify({ error: message, ...hint === undefined ? {} : { hint }, ...cause === undefined ? {} : { cause } }));
|
|
629
|
+
}
|
|
630
|
+
function wantsJson(argv) {
|
|
631
|
+
const end = argv.indexOf("--");
|
|
632
|
+
return (end === -1 ? argv : argv.slice(0, end)).includes("--json");
|
|
633
|
+
}
|
|
634
|
+
function reportThrown(prefix, error, redact = (text) => text, json = false) {
|
|
635
|
+
if (error instanceof HelpShown)
|
|
636
|
+
return 0;
|
|
637
|
+
const help = error instanceof UsageError ? `run ${prefix} --help for its options` : undefined;
|
|
638
|
+
const named = error instanceof Error && !(error instanceof Exit) && error.name !== "Error" ? `${error.name}: ${error.message}` : undefined;
|
|
639
|
+
const cause = error instanceof Error && error.cause instanceof Error ? redact(error.cause.message) : undefined;
|
|
640
|
+
const report = json ? printJsonError : (message, hint, causeText) => printError(prefix, message, hint, causeText);
|
|
641
|
+
report(redact(named ?? (error instanceof Error ? error.message : String(error))), error instanceof Exit ? error.hint ?? help : undefined, cause);
|
|
642
|
+
return error instanceof Exit ? error.code : 1;
|
|
643
|
+
}
|
|
644
|
+
var COMPLETE = "__complete";
|
|
645
|
+
var FILES = "__files__";
|
|
646
|
+
async function completingOf(run) {
|
|
647
|
+
completion.active = true;
|
|
648
|
+
try {
|
|
649
|
+
await run([]);
|
|
650
|
+
} catch (error) {
|
|
651
|
+
if (error instanceof Completing)
|
|
652
|
+
return error;
|
|
653
|
+
} finally {
|
|
654
|
+
completion.active = false;
|
|
655
|
+
}
|
|
656
|
+
return new Completing({});
|
|
657
|
+
}
|
|
658
|
+
function candidateLine(candidate) {
|
|
659
|
+
return typeof candidate === "string" ? candidate : candidate.join("\t");
|
|
660
|
+
}
|
|
661
|
+
function positionalBeingTyped(before, { options, positionals }) {
|
|
662
|
+
const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
|
|
663
|
+
let index = 0;
|
|
664
|
+
let at = 0;
|
|
665
|
+
while (at < before.length) {
|
|
666
|
+
const word = before[at];
|
|
667
|
+
if (word === "--") {
|
|
668
|
+
index += before.length - at - 1;
|
|
669
|
+
break;
|
|
670
|
+
}
|
|
671
|
+
if (!word.startsWith("-"))
|
|
672
|
+
index++;
|
|
673
|
+
else if (takesValue(word))
|
|
674
|
+
at++;
|
|
675
|
+
at++;
|
|
676
|
+
}
|
|
677
|
+
const last = positionals.at(-1);
|
|
678
|
+
if (index < positionals.length)
|
|
679
|
+
return positionals[index];
|
|
680
|
+
return last?.replace("]", "").endsWith("...") === true ? last : undefined;
|
|
681
|
+
}
|
|
682
|
+
async function leafCandidates(run, words) {
|
|
683
|
+
const completing = await completingOf(run);
|
|
684
|
+
if ((words.at(-1) ?? "").startsWith("-"))
|
|
685
|
+
return Object.entries(completing.options).map(([name, option]) => option.description === undefined ? `--${name}` : `--${name} ${option.description}`);
|
|
686
|
+
const flag = words.at(-2);
|
|
687
|
+
const option = flag?.startsWith("-") === true ? Object.entries(completing.options).find(([name, { short }]) => flag === `--${name}` || flag === `-${short}`)?.[1] : undefined;
|
|
688
|
+
if (option?.type === "string") {
|
|
689
|
+
const values = option.choices === undefined ? await option.candidates?.() : option.choices;
|
|
690
|
+
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
691
|
+
}
|
|
692
|
+
const name = positionalBeingTyped(words.slice(0, -1), completing);
|
|
693
|
+
const values = name === undefined ? undefined : await completing.candidates[name]?.();
|
|
694
|
+
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
695
|
+
}
|
|
696
|
+
async function printCandidates(found) {
|
|
697
|
+
for (const line of await found)
|
|
698
|
+
console.log(line);
|
|
699
|
+
return 0;
|
|
700
|
+
}
|
|
701
|
+
async function runMain({ name, version, run, redact }, argv = process3.argv.slice(2)) {
|
|
702
|
+
if (argv[0] === COMPLETE)
|
|
703
|
+
return printCandidates(leafCandidates(run, argv.slice(1)));
|
|
704
|
+
if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
|
|
705
|
+
console.log(version);
|
|
706
|
+
return 0;
|
|
707
|
+
}
|
|
708
|
+
try {
|
|
709
|
+
return await run(argv);
|
|
710
|
+
} catch (error) {
|
|
711
|
+
return reportThrown(name, error, redact, wantsJson(argv));
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
// ../kit/src/logs.ts
|
|
715
|
+
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
716
|
+
// ../kit/src/secrets.ts
|
|
717
|
+
var SECRET_WORDS = new Set(["key", "pass", "pwd", "auth", "salt", "cookie", "session", "jwt", "ssn", "iban", "card", "cvv", "otp"]);
|
|
718
|
+
// ../kit/src/sessions.ts
|
|
719
|
+
var {Glob } = globalThis.Bun;
|
|
720
|
+
var SESSION_FILES = new Glob("*.json");
|
|
721
|
+
var ACCOUNT_DIRS = new Glob("*/sessions");
|
|
722
|
+
// package.json
|
|
723
|
+
var version = "0.1.0";
|
|
724
|
+
|
|
725
|
+
// src/diff.ts
|
|
726
|
+
var TRAILING_NEWLINE = /\n$/;
|
|
727
|
+
var HUNK = /^@@ -\d+(?:,(\d+))? \+\d+(?:,(\d+))? @@(.*)$/;
|
|
728
|
+
var FILE_HEADER = /^diff --git a\/(.*) b\/(.*)$/;
|
|
729
|
+
var COMMIT = /^(commit )([0-9a-f]{7,40})(.*)$/;
|
|
730
|
+
var HUNK_LINE = new Set([" ", "+", "-", "\\"]);
|
|
731
|
+
var MARKERS = /^(?:--- |\+\+\+ )/;
|
|
732
|
+
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)/;
|
|
733
|
+
var COMMIT_META = /^(?:Author|Date|Merge|AuthorDate|Commit|CommitDate):/;
|
|
734
|
+
function looksLikeDiff(text) {
|
|
735
|
+
const first = stripAnsi(text).split(`
|
|
736
|
+
`).find((line) => line.trim() !== "") ?? "";
|
|
737
|
+
return first.startsWith("diff --git ") || first.startsWith("--- ") || first.startsWith("@@ ") || first.startsWith("Index: ") || COMMIT.test(first);
|
|
738
|
+
}
|
|
739
|
+
function renderDiff(text, theme) {
|
|
740
|
+
let oldLeft = 0;
|
|
741
|
+
let newLeft = 0;
|
|
742
|
+
const lines = stripAnsi(text).replaceAll("\r", "").replace(TRAILING_NEWLINE, "").split(`
|
|
743
|
+
`).map((line) => {
|
|
744
|
+
if ((oldLeft > 0 || newLeft > 0) && line !== "" && !HUNK_LINE.has(line[0] ?? ""))
|
|
745
|
+
oldLeft = newLeft = 0;
|
|
746
|
+
if (oldLeft > 0 || newLeft > 0) {
|
|
747
|
+
const mark = line[0];
|
|
748
|
+
if (mark === "+") {
|
|
749
|
+
newLeft--;
|
|
750
|
+
return theme.added(line);
|
|
751
|
+
}
|
|
752
|
+
if (mark === "-") {
|
|
753
|
+
oldLeft--;
|
|
754
|
+
return theme.removed(line);
|
|
755
|
+
}
|
|
756
|
+
if (mark === "\\")
|
|
757
|
+
return theme.dim(line);
|
|
758
|
+
oldLeft--;
|
|
759
|
+
newLeft--;
|
|
760
|
+
return line;
|
|
761
|
+
}
|
|
762
|
+
const hunk = HUNK.exec(line);
|
|
763
|
+
if (hunk !== null) {
|
|
764
|
+
oldLeft = Number(hunk[1] ?? 1);
|
|
765
|
+
newLeft = Number(hunk[2] ?? 1);
|
|
766
|
+
return `${theme.accent(line.slice(0, line.length - (hunk[3] ?? "").length))}${theme.dim(hunk[3] ?? "")}`;
|
|
767
|
+
}
|
|
768
|
+
const file = FILE_HEADER.exec(line);
|
|
769
|
+
if (file !== null)
|
|
770
|
+
return `${theme.dim("diff")} ${theme.bold(theme.path(file[2] ?? ""))}`;
|
|
771
|
+
const commit = COMMIT.exec(line);
|
|
772
|
+
if (commit !== null)
|
|
773
|
+
return `${theme.dim(commit[1] ?? "")}${theme.id(commit[2] ?? "")}${theme.dim(commit[3] ?? "")}`;
|
|
774
|
+
return MARKERS.test(line) || META.test(line) || COMMIT_META.test(line) ? theme.dim(line) : line;
|
|
775
|
+
});
|
|
776
|
+
return `${lines.join(`
|
|
777
|
+
`)}
|
|
778
|
+
`;
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
// src/markdown.ts
|
|
782
|
+
var FIRST = "\x01";
|
|
783
|
+
var REST = "\x02";
|
|
784
|
+
var BODY = "\x03";
|
|
785
|
+
var NESTED = "\x05";
|
|
786
|
+
var CELL = "\x06";
|
|
787
|
+
var ROW = "\x07";
|
|
788
|
+
var BLOCK = "\b";
|
|
789
|
+
var BULLETS = ["\u2022", "\u25E6", "\u25AA"];
|
|
790
|
+
var MAX_RULE = 60;
|
|
791
|
+
var MIN_ROOM = 20;
|
|
792
|
+
var MIN_COLUMN = 8;
|
|
793
|
+
var COLUMN_GAP = 2;
|
|
794
|
+
var TRAILING_BREAKS = /\n+$/;
|
|
795
|
+
function block(text) {
|
|
796
|
+
return `${BLOCK}${text}`;
|
|
797
|
+
}
|
|
798
|
+
function reflow(text, first = "", rest = "") {
|
|
799
|
+
return `${FIRST}${first}${REST}${rest}${BODY}${text}`;
|
|
800
|
+
}
|
|
801
|
+
function trimmed(block) {
|
|
802
|
+
return block.replace(TRAILING_BREAKS, "");
|
|
803
|
+
}
|
|
804
|
+
function indent(block, first, rest) {
|
|
805
|
+
return block.split(`
|
|
806
|
+
`).map((line, index) => {
|
|
807
|
+
const lead = index === 0 ? first : rest;
|
|
808
|
+
if (!line.startsWith(FIRST))
|
|
809
|
+
return `${lead}${line}`;
|
|
810
|
+
const middle = line.indexOf(REST);
|
|
811
|
+
const body = line.indexOf(BODY);
|
|
812
|
+
return `${FIRST}${lead}${line.slice(1, middle)}${REST}${rest}${line.slice(middle + 1, body)}${BODY}${line.slice(body + 1)}`;
|
|
813
|
+
}).join(`
|
|
814
|
+
`);
|
|
815
|
+
}
|
|
816
|
+
function finish(block, width) {
|
|
817
|
+
return block.split(`
|
|
818
|
+
`).flatMap((line) => {
|
|
819
|
+
if (!line.startsWith(FIRST))
|
|
820
|
+
return [line.trimEnd()];
|
|
821
|
+
const middle = line.indexOf(REST);
|
|
822
|
+
const body = line.indexOf(BODY);
|
|
823
|
+
const first = line.slice(1, middle);
|
|
824
|
+
const rest = line.slice(middle + 1, body);
|
|
825
|
+
const room = Math.max(width - Math.max(textWidth(first), textWidth(rest)), MIN_ROOM);
|
|
826
|
+
return Bun.wrapAnsi(line.slice(body + 1), room).split(`
|
|
827
|
+
`).map((wrapped, index) => `${index === 0 ? first : rest}${wrapped}`.trimEnd());
|
|
828
|
+
}).join(`
|
|
829
|
+
`);
|
|
830
|
+
}
|
|
831
|
+
function columnWidths(rows, width) {
|
|
832
|
+
const widths = rows[0]?.map((_, column) => Math.max(...rows.map((row) => textWidth(row[column] ?? "")))) ?? [];
|
|
833
|
+
const total = () => widths.reduce((sum, each) => sum + each, 0) + COLUMN_GAP * Math.max(0, widths.length - 1);
|
|
834
|
+
while (total() > width) {
|
|
835
|
+
const widest = widths.indexOf(Math.max(...widths));
|
|
836
|
+
if ((widths[widest] ?? 0) <= MIN_COLUMN)
|
|
837
|
+
break;
|
|
838
|
+
widths[widest]--;
|
|
839
|
+
}
|
|
840
|
+
return widths;
|
|
841
|
+
}
|
|
842
|
+
function renderTable(rows, aligns, width, theme) {
|
|
843
|
+
const widths = columnWidths(rows, width);
|
|
844
|
+
const lines = rows.flatMap((row, index) => {
|
|
845
|
+
const cells = widths.map((each, column) => Bun.wrapAnsi(row[column] ?? "", each, { hard: true }).split(`
|
|
846
|
+
`));
|
|
847
|
+
const height = Math.max(...cells.map((cell) => cell.length));
|
|
848
|
+
const drawn = Array.from({ length: height }, (_, at) => cells.map((cell, column) => {
|
|
849
|
+
const text = cell[at] ?? "";
|
|
850
|
+
const gap = " ".repeat(Math.max(0, (widths[column] ?? 0) - textWidth(text)));
|
|
851
|
+
const aligned = aligns[column] === "right" ? `${gap}${text}` : `${text}${gap}`;
|
|
852
|
+
return index === 0 ? theme.bold(aligned) : aligned;
|
|
853
|
+
}).join(" ".repeat(COLUMN_GAP)).trimEnd());
|
|
854
|
+
return index === 0 ? [...drawn, theme.dim("\u2500".repeat(Math.min(width, widths.reduce((sum, each) => sum + each, 0) + COLUMN_GAP * Math.max(0, widths.length - 1))))] : drawn;
|
|
855
|
+
});
|
|
856
|
+
return lines.join(`
|
|
857
|
+
`);
|
|
858
|
+
}
|
|
859
|
+
function printable(source) {
|
|
860
|
+
return Array.from(stripAnsi(source.replaceAll(`\r
|
|
861
|
+
`, `
|
|
862
|
+
`))).filter((char) => char >= " " || char === `
|
|
863
|
+
` || char === "\t").join("");
|
|
864
|
+
}
|
|
865
|
+
function renderMarkdown(source, { theme, width }) {
|
|
866
|
+
const bar = theme.dim("\u2502 ");
|
|
867
|
+
const quote = theme.dim("\u258E ");
|
|
868
|
+
let aligns = [];
|
|
869
|
+
const rendered = Bun.markdown.render(printable(source), {
|
|
870
|
+
heading: (children, { level }) => block(`${reflow(theme.heading(children), theme.dim(`${"#".repeat(level)} `), " ".repeat(level + 1))}
|
|
871
|
+
|
|
872
|
+
`),
|
|
873
|
+
paragraph: (children) => block(`${reflow(children.replaceAll(`
|
|
874
|
+
`, " "))}
|
|
875
|
+
|
|
876
|
+
`),
|
|
877
|
+
strong: theme.bold,
|
|
878
|
+
emphasis: theme.italic,
|
|
879
|
+
strikethrough: theme.strike,
|
|
880
|
+
codespan: theme.code,
|
|
881
|
+
link: (children, { href }) => theme.link(children, href),
|
|
882
|
+
image: (children, { src }) => theme.dim(`[image: ${children === "" ? src : children}]`),
|
|
883
|
+
html: (children) => block(`${theme.dim(trimmed(children))}
|
|
884
|
+
|
|
885
|
+
`),
|
|
886
|
+
code: (children) => block(`${trimmed(children).split(`
|
|
887
|
+
`).map((line) => `${bar}${line}`).join(`
|
|
888
|
+
`)}
|
|
889
|
+
|
|
890
|
+
`),
|
|
891
|
+
blockquote: (children) => block(`${indent(trimmed(children.replaceAll(BLOCK, "")), quote, quote)}
|
|
892
|
+
|
|
893
|
+
`),
|
|
894
|
+
hr: () => block(`${theme.dim("\u2500".repeat(Math.min(width, MAX_RULE)))}
|
|
895
|
+
|
|
896
|
+
`),
|
|
897
|
+
list: (children, { depth }) => depth > 0 ? `${NESTED}${trimmed(children)}
|
|
898
|
+
|
|
899
|
+
` : block(`${trimmed(children)}
|
|
900
|
+
|
|
901
|
+
`),
|
|
902
|
+
listItem: (children, { index, ordered, start, depth, checked }) => {
|
|
903
|
+
const [head = "", ...nested] = children.split(NESTED);
|
|
904
|
+
const blocksAt = head.indexOf(BLOCK);
|
|
905
|
+
const inline = (blocksAt === -1 ? head : head.slice(0, blocksAt)).trim();
|
|
906
|
+
const blocks = blocksAt === -1 ? "" : trimmed(head.slice(blocksAt).replaceAll(BLOCK, ""));
|
|
907
|
+
const text = [inline === "" ? "" : reflow(inline.replaceAll(`
|
|
908
|
+
`, " ")), blocks].filter((part) => part !== "").join(`
|
|
909
|
+
`) || reflow("");
|
|
910
|
+
let marker = theme.accent(BULLETS[depth % BULLETS.length] ?? "\u2022");
|
|
911
|
+
if (checked !== undefined)
|
|
912
|
+
marker = checked ? theme.added("\u2713") : theme.dim("\u25CB");
|
|
913
|
+
else if (ordered)
|
|
914
|
+
marker = theme.accent(`${(start ?? 1) + index}.`);
|
|
915
|
+
return `${indent([text, ...nested.map(trimmed)].join(`
|
|
916
|
+
`), `${marker} `, " ".repeat(textWidth(marker) + 1))}
|
|
917
|
+
`;
|
|
918
|
+
},
|
|
919
|
+
th: (children, meta) => {
|
|
920
|
+
aligns.push(meta?.align === "right" ? "right" : "left");
|
|
921
|
+
return `${children}${CELL}`;
|
|
922
|
+
},
|
|
923
|
+
td: (children) => `${children}${CELL}`,
|
|
924
|
+
tr: (children) => `${children}${ROW}`,
|
|
925
|
+
table: (children) => {
|
|
926
|
+
const rows = children.split(ROW).filter((row) => row !== "").map((row) => row.split(CELL).slice(0, -1));
|
|
927
|
+
const drawn = renderTable(rows, aligns, width, theme);
|
|
928
|
+
aligns = [];
|
|
929
|
+
return block(`${drawn}
|
|
930
|
+
|
|
931
|
+
`);
|
|
932
|
+
}
|
|
933
|
+
});
|
|
934
|
+
return `${finish(trimmed(rendered.replaceAll(BLOCK, "")), width)}
|
|
935
|
+
`;
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
// src/theme.ts
|
|
939
|
+
import process5 from "process";
|
|
940
|
+
function themeFor(stream = process5.stdout, env = process5.env) {
|
|
941
|
+
const colors = paint(stream, env);
|
|
942
|
+
const accent = role("accent", stream, env);
|
|
943
|
+
const path = role("path", stream, env);
|
|
944
|
+
return {
|
|
945
|
+
heading: (text) => accent(colors.bold(text)),
|
|
946
|
+
code: role("id", stream, env),
|
|
947
|
+
path,
|
|
948
|
+
id: role("id", stream, env),
|
|
949
|
+
dim: role("label", stream, env),
|
|
950
|
+
bold: colors.bold,
|
|
951
|
+
italic: colors.italic,
|
|
952
|
+
strike: colors.strikethrough,
|
|
953
|
+
added: colors.green,
|
|
954
|
+
removed: colors.red,
|
|
955
|
+
accent,
|
|
956
|
+
link: (text, url) => path(colors.underline(link(text, url, stream, env)))
|
|
957
|
+
};
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
// src/view.ts
|
|
961
|
+
var DEFAULT_WIDTH = 80;
|
|
962
|
+
var MARKDOWN_FILE = /\.(?:md|markdown|mdx)$/i;
|
|
963
|
+
var DIFF_FILE = /\.(?:diff|patch)$/i;
|
|
964
|
+
var USAGE = `view [options] [file]: shows a markdown file or a git diff in the colours of the other @kaynooo CLIs
|
|
965
|
+
|
|
966
|
+
A file, or what is piped on stdin (or - for stdin). 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.
|
|
967
|
+
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.
|
|
968
|
+
|
|
969
|
+
Exits 0 when it showed the text, 1 when there is nothing to show, 2 on a usage error or a file that is missing.`;
|
|
970
|
+
var OPTIONS = {
|
|
971
|
+
diff: { type: "boolean", description: "show the text as a diff" },
|
|
972
|
+
markdown: { type: "boolean", description: "show the text as markdown" },
|
|
973
|
+
width: { type: "string", short: "w", description: "columns to wrap to, instead of the terminal's" },
|
|
974
|
+
pager: { type: "boolean", description: "page a long output on a terminal (--no-pager turns it off)" }
|
|
975
|
+
};
|
|
976
|
+
async function page(text) {
|
|
977
|
+
try {
|
|
978
|
+
const pager = Bun.spawn(["less", "-FRX"], { stdin: "pipe", stdout: "inherit", stderr: "inherit" });
|
|
979
|
+
try {
|
|
980
|
+
pager.stdin.write(text);
|
|
981
|
+
await pager.stdin.end();
|
|
982
|
+
} catch {}
|
|
983
|
+
await pager.exited;
|
|
984
|
+
} catch {
|
|
985
|
+
process6.stdout.write(text);
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
async function main(args) {
|
|
989
|
+
const { values, positionals } = parse(args, USAGE, OPTIONS, { positionals: ["[file]"], allowNegative: true, replaces: "cat, bat or less on a markdown file, delta on a diff", examples: ["view README.md", "git diff | view"] });
|
|
990
|
+
if (positionals.length > 1)
|
|
991
|
+
badUsage("give one file");
|
|
992
|
+
if (values.diff === true && values.markdown === true)
|
|
993
|
+
badUsage("--diff and --markdown exclude each other");
|
|
994
|
+
const path = positionals[0] ?? "-";
|
|
995
|
+
const text = await readInput(path, { noStdin: "give a file, or pipe the text on stdin" });
|
|
996
|
+
if (text.trim() === "")
|
|
997
|
+
fail("nothing to show", 1);
|
|
998
|
+
const width = wholeNumber(values.width, "width", process6.stdout.isTTY === true ? process6.stdout.columns || DEFAULT_WIDTH : DEFAULT_WIDTH, 20);
|
|
999
|
+
const theme = themeFor(process6.stdout);
|
|
1000
|
+
const isDiff = values.diff === true || values.markdown !== true && !MARKDOWN_FILE.test(path) && (DIFF_FILE.test(path) || looksLikeDiff(text));
|
|
1001
|
+
const shown = isDiff ? renderDiff(text, theme) : renderMarkdown(text, { theme, width });
|
|
1002
|
+
if (process6.stdout.isTTY === true && values.pager !== false && shown.split(`
|
|
1003
|
+
`).length > (process6.stdout.rows || 24))
|
|
1004
|
+
await page(shown);
|
|
1005
|
+
else
|
|
1006
|
+
process6.stdout.write(shown);
|
|
1007
|
+
return 0;
|
|
1008
|
+
}
|
|
1009
|
+
process6.exitCode = await runMain({ name: "view", version, run: main });
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaynooo/cli-view",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"description": "view: markdown files and git diffs shown in the colours of the other @kaynooo CLIs, paged when long.",
|
|
6
|
+
"author": "Kaynooo <contact@kaynooo.fr>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://git.kaynooo.fr/kaynooo/cli/src/branch/main/packages/view#readme",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://git.kaynooo.fr/kaynooo/cli.git",
|
|
12
|
+
"directory": "packages/view"
|
|
13
|
+
},
|
|
14
|
+
"bugs": "https://git.kaynooo.fr/kaynooo/cli/issues",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"cli",
|
|
17
|
+
"markdown",
|
|
18
|
+
"diff",
|
|
19
|
+
"terminal"
|
|
20
|
+
],
|
|
21
|
+
"bin": {
|
|
22
|
+
"view": "./dist/view.js"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"engines": {
|
|
28
|
+
"bun": ">=1.4.3"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"registry": "https://git.kaynooo.fr/api/packages/kaynooo/npm/"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "$npm_execpath build src/view.ts --target bun --outdir dist",
|
|
35
|
+
"view": "$npm_execpath src/view.ts"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@kaynooo/cli-kit": "0.19.0"
|
|
39
|
+
}
|
|
40
|
+
}
|