@kaynooo/cli-git 0.7.0 → 0.8.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 +2 -2
- package/dist/gb.js +884 -116
- package/dist/gc.js +237 -49
- package/dist/gst.js +250 -54
- package/package.json +3 -3
package/dist/gb.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
1
|
+
#!/usr/bin/env -S bun --no-env-file
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
4
|
// src/gb.ts
|
|
5
|
-
import
|
|
5
|
+
import process15 from "process";
|
|
6
6
|
// ../kit/src/args.ts
|
|
7
|
-
import
|
|
7
|
+
import process7 from "process";
|
|
8
8
|
import { parseArgs } from "util";
|
|
9
9
|
|
|
10
10
|
// ../kit/src/style.ts
|
|
11
|
-
import
|
|
11
|
+
import process5 from "process";
|
|
12
12
|
|
|
13
13
|
// ../../node_modules/.bun/@kaynooo+utils@2.31.2+d0769e28dc4dad16/node_modules/@kaynooo/utils/dist/chunk-mtetydvc.js
|
|
14
14
|
function x(s) {
|
|
@@ -271,21 +271,209 @@ function le(i = {}) {
|
|
|
271
271
|
}
|
|
272
272
|
var t = le();
|
|
273
273
|
|
|
274
|
+
// ../kit/src/theme.ts
|
|
275
|
+
import { existsSync as existsSync2, readFileSync, statSync } from "fs";
|
|
276
|
+
import { homedir as homedir2 } from "os";
|
|
277
|
+
import { join as join3, resolve as resolve2 } from "path";
|
|
278
|
+
import process4 from "process";
|
|
279
|
+
|
|
280
|
+
// ../kit/src/config.ts
|
|
281
|
+
import { existsSync } from "fs";
|
|
282
|
+
import { dirname, join, resolve } from "path";
|
|
283
|
+
import process2 from "process";
|
|
284
|
+
function findUp(names, from = process2.cwd(), stop = () => false) {
|
|
285
|
+
const wanted = typeof names === "string" ? [names] : names;
|
|
286
|
+
for (let dir = resolve(from);; dir = dirname(dir)) {
|
|
287
|
+
const found = wanted.map((name) => join(dir, name)).find((path) => existsSync(path));
|
|
288
|
+
if (found !== undefined)
|
|
289
|
+
return found;
|
|
290
|
+
if (stop(dir) || dirname(dir) === dir)
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// ../kit/src/user-config.ts
|
|
296
|
+
import { homedir } from "os";
|
|
297
|
+
import { join as join2 } from "path";
|
|
298
|
+
import process3 from "process";
|
|
299
|
+
function configBase(where = {}) {
|
|
300
|
+
const xdg = where.xdg ?? (where.home === undefined ? process3.env.XDG_CONFIG_HOME : undefined);
|
|
301
|
+
return xdg !== undefined && xdg !== "" ? xdg : join2(where.home ?? homedir(), ".config");
|
|
302
|
+
}
|
|
303
|
+
function userConfigPath(bin, where = {}) {
|
|
304
|
+
return join2(configBase(where), "kaynooo-cli", `${bin}.json`);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// ../kit/src/theme.ts
|
|
308
|
+
var DEFAULT = {
|
|
309
|
+
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" },
|
|
310
|
+
glyphs: { ok: "\u2713", warn: "\u25B2", problem: "\u2717", note: "\u203A", passed: "\u2713", failed: "\u2717", running: "\u25CF", queued: "\u25CB", skipped: "-", bar: "\u2501" },
|
|
311
|
+
accents: ["cyan", "magenta", "green", "blue", "yellow", "redBright"],
|
|
312
|
+
bins: {}
|
|
313
|
+
};
|
|
314
|
+
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" };
|
|
315
|
+
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" };
|
|
316
|
+
var ASCII = { ok: "+", warn: "!", problem: "x", note: ">", passed: "+", failed: "x", running: "*", queued: "o", skipped: "-", bar: "#" };
|
|
317
|
+
var PRESETS = {
|
|
318
|
+
default: {},
|
|
319
|
+
"high-contrast": { colors: BRIGHT, accents: ["cyanBright", "magentaBright", "greenBright", "blueBright", "yellowBright", "redBright"] },
|
|
320
|
+
mono: { colors: MONO, accents: ["bold"] },
|
|
321
|
+
ascii: { glyphs: ASCII }
|
|
322
|
+
};
|
|
323
|
+
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"]);
|
|
324
|
+
var HEX = /^#[0-9a-f]{6}$/i;
|
|
325
|
+
var CONTROL = /\p{Cc}/u;
|
|
326
|
+
var MAX_FILE_BYTES = 64 * 1024;
|
|
327
|
+
var SPACES = /\s+/;
|
|
328
|
+
function isColorSpec(spec) {
|
|
329
|
+
if (typeof spec === "number")
|
|
330
|
+
return Number.isInteger(spec) && spec >= 0 && spec <= 255;
|
|
331
|
+
return typeof spec === "string" && spec.trim() !== "" && spec.trim().split(SPACES).every((token) => STYLES.has(token) || HEX.test(token));
|
|
332
|
+
}
|
|
333
|
+
function tinted(colors, spec) {
|
|
334
|
+
if (typeof spec === "number")
|
|
335
|
+
return colors.ansi256(spec);
|
|
336
|
+
return spec.trim().split(SPACES).reduce((chain, token) => HEX.test(token) ? chain.hex(token) : chain[token] ?? chain, colors);
|
|
337
|
+
}
|
|
338
|
+
function isRecord(value) {
|
|
339
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
340
|
+
}
|
|
341
|
+
function readThemeFile(path, warn) {
|
|
342
|
+
try {
|
|
343
|
+
const stat = statSync(path, { throwIfNoEntry: false });
|
|
344
|
+
if (stat === undefined)
|
|
345
|
+
return;
|
|
346
|
+
if (!stat.isFile() || stat.size > MAX_FILE_BYTES) {
|
|
347
|
+
warn(`${path} is not a regular file under ${MAX_FILE_BYTES / 1024} KiB, theme ignored`);
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
const parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
351
|
+
if (isRecord(parsed))
|
|
352
|
+
return parsed;
|
|
353
|
+
warn(`${path} is not a JSON object, theme ignored`);
|
|
354
|
+
} catch (error) {
|
|
355
|
+
warn(`${path} does not parse (${error instanceof Error ? error.message : String(error)}), theme ignored`);
|
|
356
|
+
}
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
function applyPreset(theme, name, warn, where) {
|
|
360
|
+
if (name === undefined)
|
|
361
|
+
return;
|
|
362
|
+
if (typeof name !== "string" || !Object.hasOwn(PRESETS, name)) {
|
|
363
|
+
warn(`${where}: no preset ${String(name)}, one of ${Object.keys(PRESETS).join(", ")}`);
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
const preset = PRESETS[name];
|
|
367
|
+
Object.assign(theme.colors, preset.colors);
|
|
368
|
+
Object.assign(theme.glyphs, preset.glyphs);
|
|
369
|
+
if (preset.accents !== undefined)
|
|
370
|
+
theme.accents = [...preset.accents];
|
|
371
|
+
return name;
|
|
372
|
+
}
|
|
373
|
+
function applyFile(theme, file, where, warn) {
|
|
374
|
+
if (isRecord(file.colors)) {
|
|
375
|
+
for (const [key, value] of Object.entries(file.colors)) {
|
|
376
|
+
if (!Object.hasOwn(DEFAULT.colors, key))
|
|
377
|
+
warn(`${where}: no colour ${key}, one of ${Object.keys(DEFAULT.colors).join(", ")}`);
|
|
378
|
+
else if (!isColorSpec(value))
|
|
379
|
+
warn(`${where}: colour ${key} is not an ANSI name, \`#rrggbb\` or a number up to 255`);
|
|
380
|
+
else
|
|
381
|
+
theme.colors[key] = value;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
if (isRecord(file.glyphs)) {
|
|
385
|
+
for (const [key, value] of Object.entries(file.glyphs)) {
|
|
386
|
+
if (!Object.hasOwn(DEFAULT.glyphs, key))
|
|
387
|
+
warn(`${where}: no glyph ${key}, one of ${Object.keys(DEFAULT.glyphs).join(", ")}`);
|
|
388
|
+
else if (typeof value !== "string" || CONTROL.test(value) || Bun.stringWidth(value) !== 1)
|
|
389
|
+
warn(`${where}: glyph ${key} must be one visible character wide, with no control character`);
|
|
390
|
+
else
|
|
391
|
+
theme.glyphs[key] = value;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
if (Array.isArray(file.accents) && file.accents.length > 0 && file.accents.every(isColorSpec))
|
|
395
|
+
theme.accents = [...file.accents];
|
|
396
|
+
else if (file.accents !== undefined)
|
|
397
|
+
warn(`${where}: accents must be a non-empty list of colours`);
|
|
398
|
+
if (isRecord(file.bins)) {
|
|
399
|
+
for (const [bin, value] of Object.entries(file.bins)) {
|
|
400
|
+
if (isColorSpec(value))
|
|
401
|
+
theme.bins[bin] = value;
|
|
402
|
+
else
|
|
403
|
+
warn(`${where}: bin ${bin} has no valid colour`);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
function endsWalk(home) {
|
|
408
|
+
return (dir) => dir === resolve2(home) || existsSync2(join3(dir, ".git"));
|
|
409
|
+
}
|
|
410
|
+
var warned = new Set;
|
|
411
|
+
function warnOnce(message) {
|
|
412
|
+
if (warned.has(message))
|
|
413
|
+
return;
|
|
414
|
+
warned.add(message);
|
|
415
|
+
console.error(`warning: theme ${message}`);
|
|
416
|
+
}
|
|
417
|
+
function isPath(chosen) {
|
|
418
|
+
return chosen.includes("/") || chosen.endsWith(".json");
|
|
419
|
+
}
|
|
420
|
+
function loadTheme({ env = process4.env, from = process4.cwd(), home = homedir2(), warn = warnOnce } = {}) {
|
|
421
|
+
const theme = { colors: { ...DEFAULT.colors }, glyphs: { ...DEFAULT.glyphs }, accents: [...DEFAULT.accents], bins: Object.create(null) };
|
|
422
|
+
const sources = [];
|
|
423
|
+
const chosen = env.KIT_THEME ?? "";
|
|
424
|
+
const wanted = chosen === "" ? [userConfigPath("theme", { home, xdg: env.XDG_CONFIG_HOME }), findUp(join3(".kaynooo", "theme.json"), from, endsWalk(home))] : [];
|
|
425
|
+
if (isPath(chosen))
|
|
426
|
+
wanted.push(resolve2(from, chosen));
|
|
427
|
+
const files = [];
|
|
428
|
+
for (const path of wanted) {
|
|
429
|
+
const file = path === undefined ? undefined : readThemeFile(path, warn);
|
|
430
|
+
if (path !== undefined && file !== undefined)
|
|
431
|
+
files.push({ path, file });
|
|
432
|
+
}
|
|
433
|
+
let preset = "default";
|
|
434
|
+
if (chosen !== "" && !isPath(chosen)) {
|
|
435
|
+
preset = applyPreset(theme, chosen, warn, "KIT_THEME") ?? preset;
|
|
436
|
+
} else {
|
|
437
|
+
const named = files.findLast(({ file }) => file.preset !== undefined);
|
|
438
|
+
if (named !== undefined)
|
|
439
|
+
preset = applyPreset(theme, named.file.preset, warn, named.path) ?? preset;
|
|
440
|
+
}
|
|
441
|
+
for (const { path, file } of files) {
|
|
442
|
+
applyFile(theme, file, path, warn);
|
|
443
|
+
sources.push(path);
|
|
444
|
+
}
|
|
445
|
+
return { theme, sources, preset };
|
|
446
|
+
}
|
|
447
|
+
var current;
|
|
448
|
+
var currentKey = "";
|
|
449
|
+
function activeTheme() {
|
|
450
|
+
const key = `${process4.cwd()}\x00${process4.env.KIT_THEME ?? ""}\x00${process4.env.XDG_CONFIG_HOME ?? ""}`;
|
|
451
|
+
if (current === undefined || key !== currentKey) {
|
|
452
|
+
current = loadTheme();
|
|
453
|
+
currentKey = key;
|
|
454
|
+
}
|
|
455
|
+
return current;
|
|
456
|
+
}
|
|
457
|
+
|
|
274
458
|
// ../kit/src/style.ts
|
|
275
|
-
function takesControlCodes(stream, env =
|
|
459
|
+
function takesControlCodes(stream, env = process5.env) {
|
|
276
460
|
return stream.isTTY === true && env.NO_COLOR === undefined && env.NODE_DISABLE_COLORS === undefined && env.TERM !== "dumb";
|
|
277
461
|
}
|
|
278
|
-
function paint(stream, env =
|
|
462
|
+
function paint(stream, env = process5.env) {
|
|
279
463
|
return le({ stream: { isTTY: takesControlCodes(stream, env) }, env });
|
|
280
464
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
465
|
+
function role(name, stream, env = process5.env) {
|
|
466
|
+
const colors = paint(stream, env);
|
|
467
|
+
return takesControlCodes(stream, env) ? tinted(colors, activeTheme().theme.colors[name]) : colors;
|
|
468
|
+
}
|
|
469
|
+
var STATUS_WORDS = {
|
|
470
|
+
passed: ["passed", "success", "succeeded", "ok"],
|
|
471
|
+
failed: ["failed", "failure", "error", "errored"],
|
|
472
|
+
running: ["running", "in_progress", "started"],
|
|
473
|
+
queued: ["queued", "waiting", "pending", "created", "blocked", "scheduled"],
|
|
474
|
+
skipped: ["skipped", "cancelled", "canceled", "manual"]
|
|
287
475
|
};
|
|
288
|
-
var STATUS_OF = new Map(Object.entries(
|
|
476
|
+
var STATUS_OF = new Map(Object.entries(STATUS_WORDS).flatMap(([status, words]) => words.map((word) => [word, status])));
|
|
289
477
|
|
|
290
478
|
// ../kit/src/format.ts
|
|
291
479
|
function upTo(text, separator) {
|
|
@@ -298,27 +486,27 @@ function printJson(value) {
|
|
|
298
486
|
}
|
|
299
487
|
|
|
300
488
|
// ../kit/src/help.ts
|
|
301
|
-
import
|
|
489
|
+
import process6 from "process";
|
|
302
490
|
var OPTIONAL = /\[[^\]]+\]/g;
|
|
303
491
|
var USAGE_PREFIX = /^usage:\s*/i;
|
|
304
492
|
var FULL_STOP = /\.$/;
|
|
305
|
-
function usageLine(usage, colors = paint(
|
|
493
|
+
function usageLine(usage, colors = paint(process6.stdout)) {
|
|
306
494
|
return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
|
|
307
495
|
}
|
|
308
|
-
function helpText(usage, options, colors = paint(
|
|
496
|
+
function helpText(usage, options, colors = paint(process6.stdout), { examples = [], replaces, exits = [] } = {}) {
|
|
309
497
|
return [usageLine(usage, colors), ...replacesRows(replaces, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exitRows(exits, colors), ...exampleRows(examples, colors)].join(`
|
|
310
498
|
`);
|
|
311
499
|
}
|
|
312
|
-
function exitRows(exits, colors = paint(
|
|
500
|
+
function exitRows(exits, colors = paint(process6.stdout)) {
|
|
313
501
|
if (exits.length === 0)
|
|
314
502
|
return [];
|
|
315
503
|
const width = Math.max(...exits.map(([code]) => String(code).length));
|
|
316
504
|
return ["", colors.bold("Exits:"), ...exits.map(([code, meaning]) => ` ${colors.cyan(String(code).padEnd(width))} ${meaning}`)];
|
|
317
505
|
}
|
|
318
|
-
function replacesRows(replaces, colors = paint(
|
|
506
|
+
function replacesRows(replaces, colors = paint(process6.stdout)) {
|
|
319
507
|
return replaces === undefined ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
320
508
|
}
|
|
321
|
-
function wantsHelpJson(env =
|
|
509
|
+
function wantsHelpJson(env = process6.env) {
|
|
322
510
|
return env.KIT_HELP === "json";
|
|
323
511
|
}
|
|
324
512
|
function description(usage, { examples = [], replaces, exits } = {}, options) {
|
|
@@ -337,12 +525,12 @@ function description(usage, { examples = [], replaces, exits } = {}, options) {
|
|
|
337
525
|
...options === undefined ? {} : { options: Object.entries(options).map(([name, { short, description }]) => ({ name, ...short === undefined ? {} : { short }, ...description === undefined ? {} : { description } })) }
|
|
338
526
|
};
|
|
339
527
|
}
|
|
340
|
-
function exampleRows(examples, colors = paint(
|
|
528
|
+
function exampleRows(examples, colors = paint(process6.stdout)) {
|
|
341
529
|
if (examples.length === 0)
|
|
342
530
|
return [];
|
|
343
531
|
return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
|
|
344
532
|
}
|
|
345
|
-
function optionRows(options, colors = paint(
|
|
533
|
+
function optionRows(options, colors = paint(process6.stdout)) {
|
|
346
534
|
const rows = Object.entries(options).map(([name, option]) => {
|
|
347
535
|
const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
|
|
348
536
|
const named = option.choices === undefined ? "<value>" : `<${option.choices.join("|")}>`;
|
|
@@ -433,7 +621,7 @@ var OUTPUT_OPTIONS = {
|
|
|
433
621
|
color: { type: "string", description: "auto (on a terminal alone), always or never" },
|
|
434
622
|
plain: { type: "boolean", description: "plain text, no colour or spinner: --color never" }
|
|
435
623
|
};
|
|
436
|
-
function applyOutput(color, plain, env =
|
|
624
|
+
function applyOutput(color, plain, env = process7.env) {
|
|
437
625
|
const when = plain === true ? "never" : color ?? "auto";
|
|
438
626
|
if (when === "auto")
|
|
439
627
|
return;
|
|
@@ -604,7 +792,7 @@ function nearestOption(message, options, allowNegative) {
|
|
|
604
792
|
}
|
|
605
793
|
// ../kit/src/process.ts
|
|
606
794
|
import { constants } from "os";
|
|
607
|
-
import
|
|
795
|
+
import process8 from "process";
|
|
608
796
|
var KILL_GRACE_MS = 2000;
|
|
609
797
|
var SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
610
798
|
function exitCodeOf(code, signal) {
|
|
@@ -626,7 +814,7 @@ function holdSignals(cleanup) {
|
|
|
626
814
|
},
|
|
627
815
|
release: () => {
|
|
628
816
|
for (const signal of SIGNALS)
|
|
629
|
-
|
|
817
|
+
process8.off(signal, onSignal);
|
|
630
818
|
}
|
|
631
819
|
};
|
|
632
820
|
function onSignal(signal) {
|
|
@@ -639,10 +827,10 @@ function holdSignals(cleanup) {
|
|
|
639
827
|
return;
|
|
640
828
|
held.release();
|
|
641
829
|
cleanup(signal);
|
|
642
|
-
|
|
830
|
+
process8.kill(process8.pid, signal);
|
|
643
831
|
}
|
|
644
832
|
for (const signal of SIGNALS)
|
|
645
|
-
|
|
833
|
+
process8.on(signal, onSignal);
|
|
646
834
|
return held;
|
|
647
835
|
}
|
|
648
836
|
async function runCommand(argv, { cwd, env, stdin = "ignore", timeoutMs, signal, maxBytes = Infinity, onChunk, onStart } = {}) {
|
|
@@ -656,7 +844,7 @@ async function runCommand(argv, { cwd, env, stdin = "ignore", timeoutMs, signal,
|
|
|
656
844
|
return;
|
|
657
845
|
try {
|
|
658
846
|
if (grouped)
|
|
659
|
-
|
|
847
|
+
process8.kill(-proc.pid, killing);
|
|
660
848
|
else
|
|
661
849
|
proc.kill(killing);
|
|
662
850
|
} catch {}
|
|
@@ -730,15 +918,15 @@ var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x
|
|
|
730
918
|
var RESET_STYLE = `${ESC}[0m`;
|
|
731
919
|
var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
732
920
|
// ../kit/src/cli.ts
|
|
733
|
-
import { readFileSync, realpathSync } from "fs";
|
|
734
|
-
import { dirname as
|
|
735
|
-
import
|
|
921
|
+
import { readFileSync as readFileSync2, realpathSync } from "fs";
|
|
922
|
+
import { dirname as dirname3, join as join5 } from "path";
|
|
923
|
+
import process10 from "process";
|
|
736
924
|
|
|
737
925
|
// ../kit/src/usage.ts
|
|
738
926
|
import { appendFileSync, mkdirSync } from "fs";
|
|
739
|
-
import { homedir } from "os";
|
|
740
|
-
import { dirname, isAbsolute, join } from "path";
|
|
741
|
-
import
|
|
927
|
+
import { homedir as homedir3 } from "os";
|
|
928
|
+
import { dirname as dirname2, isAbsolute, join as join4 } from "path";
|
|
929
|
+
import process9 from "process";
|
|
742
930
|
|
|
743
931
|
// ../kit/src/logs.ts
|
|
744
932
|
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
@@ -752,14 +940,14 @@ var usageError;
|
|
|
752
940
|
function noteUsageError(message) {
|
|
753
941
|
usageError = message;
|
|
754
942
|
}
|
|
755
|
-
function stateHome(env =
|
|
943
|
+
function stateHome(env = process9.env) {
|
|
756
944
|
const dir = env.XDG_STATE_HOME;
|
|
757
|
-
return dir !== undefined && isAbsolute(dir) ? dir :
|
|
945
|
+
return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
|
|
758
946
|
}
|
|
759
|
-
function usagePath(env =
|
|
760
|
-
return
|
|
947
|
+
function usagePath(env = process9.env) {
|
|
948
|
+
return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
761
949
|
}
|
|
762
|
-
function usageRecorded(env =
|
|
950
|
+
function usageRecorded(env = process9.env) {
|
|
763
951
|
const off = env.KAYNOOO_CLI_NO_USAGE;
|
|
764
952
|
return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
|
|
765
953
|
}
|
|
@@ -773,12 +961,12 @@ function usageEntry(bin, command, argv, code, now = new Date) {
|
|
|
773
961
|
usageError = undefined;
|
|
774
962
|
return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
|
|
775
963
|
}
|
|
776
|
-
function recordUsage(entry, env =
|
|
964
|
+
function recordUsage(entry, env = process9.env) {
|
|
777
965
|
if (!usageRecorded(env))
|
|
778
966
|
return;
|
|
779
967
|
try {
|
|
780
968
|
const path = usagePath(env);
|
|
781
|
-
mkdirSync(
|
|
969
|
+
mkdirSync(dirname2(path), { recursive: true, mode: 448 });
|
|
782
970
|
appendFileSync(path, `${JSON.stringify(entry)}
|
|
783
971
|
`, { mode: 384 });
|
|
784
972
|
} catch {}
|
|
@@ -791,8 +979,8 @@ var LEVEL_OPTIONS = {
|
|
|
791
979
|
};
|
|
792
980
|
var TOP_OPTIONS = { ...LEVEL_OPTIONS, version: { type: "boolean", short: "v", description: "print the version" } };
|
|
793
981
|
function printError(prefix, message, hint, cause) {
|
|
794
|
-
const colors = paint(
|
|
795
|
-
const styled = takesControlCodes(
|
|
982
|
+
const colors = paint(process10.stderr);
|
|
983
|
+
const styled = takesControlCodes(process10.stderr);
|
|
796
984
|
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
797
985
|
const under = (label, text, tint) => text === undefined ? "" : `
|
|
798
986
|
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
@@ -814,7 +1002,7 @@ function reportThrown(prefix, error, redact = (text) => text, json = false) {
|
|
|
814
1002
|
const help = error instanceof UsageError ? `run ${prefix} --help for its options` : undefined;
|
|
815
1003
|
const named = error instanceof Error && !(error instanceof Exit) && error.name !== "Error" ? `${error.name}: ${error.message}` : undefined;
|
|
816
1004
|
const cause = error instanceof Error && error.cause instanceof Error ? redact(error.cause.message) : undefined;
|
|
817
|
-
if (error instanceof
|
|
1005
|
+
if (error instanceof Exit && error.code === 2)
|
|
818
1006
|
noteUsageError(error.message);
|
|
819
1007
|
const report = json ? printJsonError : (message, hint, causeText) => printError(prefix, message, hint, causeText);
|
|
820
1008
|
report(redact(named ?? (error instanceof Error ? error.message : String(error))), error instanceof Exit ? error.hint ?? help : undefined, cause);
|
|
@@ -878,17 +1066,17 @@ async function printCandidates(found) {
|
|
|
878
1066
|
return 0;
|
|
879
1067
|
}
|
|
880
1068
|
function packageVersion(entry = Bun.main) {
|
|
881
|
-
for (let dir =
|
|
1069
|
+
for (let dir = dirname3(realpathSync(entry));; dir = dirname3(dir)) {
|
|
882
1070
|
try {
|
|
883
|
-
const { version } = JSON.parse(
|
|
1071
|
+
const { version } = JSON.parse(readFileSync2(join5(dir, "package.json"), "utf8"));
|
|
884
1072
|
if (typeof version === "string")
|
|
885
1073
|
return version;
|
|
886
1074
|
} catch {}
|
|
887
|
-
if (
|
|
1075
|
+
if (dirname3(dir) === dir)
|
|
888
1076
|
return "0.0.0";
|
|
889
1077
|
}
|
|
890
1078
|
}
|
|
891
|
-
async function runMain({ name, version, run, redact, usage }, argv =
|
|
1079
|
+
async function runMain({ name, version, run, redact, usage }, argv = process10.argv.slice(2)) {
|
|
892
1080
|
if (argv[0] === COMPLETE)
|
|
893
1081
|
return printCandidates(leafCandidates(run, argv.slice(1)));
|
|
894
1082
|
if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
|
|
@@ -916,9 +1104,625 @@ var SESSION_FILES = new Glob("*.json");
|
|
|
916
1104
|
var ACCOUNT_DIRS = new Glob("*/sessions");
|
|
917
1105
|
// src/branch.ts
|
|
918
1106
|
import { basename } from "path";
|
|
919
|
-
import
|
|
1107
|
+
import process14 from "process";
|
|
1108
|
+
|
|
1109
|
+
// ../kit/src/picker/score.ts
|
|
1110
|
+
var MATCH = 16;
|
|
1111
|
+
var CONSECUTIVE = 12;
|
|
1112
|
+
var BOUNDARY = 10;
|
|
1113
|
+
var PREFIX = 8;
|
|
1114
|
+
var GAP = 1;
|
|
1115
|
+
var BOUNDARY_AFTER = new Set([" ", "/", "-", "_", ".", ":", "\\", "(", "[", ",", "\t"]);
|
|
1116
|
+
var UPPER = /\p{Lu}/u;
|
|
1117
|
+
var LOWER = /\p{Ll}/u;
|
|
1118
|
+
var NONE = Number.NEGATIVE_INFINITY;
|
|
1119
|
+
function startsWord(text, index) {
|
|
1120
|
+
if (index === 0)
|
|
1121
|
+
return true;
|
|
1122
|
+
const before = text[index - 1];
|
|
1123
|
+
return BOUNDARY_AFTER.has(before) || LOWER.test(before) && UPPER.test(text[index]);
|
|
1124
|
+
}
|
|
1125
|
+
function fuzzy(query, text) {
|
|
1126
|
+
if (query === "")
|
|
1127
|
+
return { score: 0, positions: [] };
|
|
1128
|
+
const exact = query !== query.toLowerCase();
|
|
1129
|
+
const needle = exact ? query : query.toLowerCase();
|
|
1130
|
+
const haystack = exact ? text : text.toLowerCase();
|
|
1131
|
+
if (haystack.length !== text.length || needle.length > haystack.length)
|
|
1132
|
+
return;
|
|
1133
|
+
const width = haystack.length;
|
|
1134
|
+
const best = Array.from({ length: needle.length }, () => new Array(width).fill(NONE));
|
|
1135
|
+
const from = Array.from({ length: needle.length }, () => new Array(width).fill(-1));
|
|
1136
|
+
for (let j = 0;j < needle.length; j++) {
|
|
1137
|
+
let run = NONE;
|
|
1138
|
+
let runAt = -1;
|
|
1139
|
+
for (let i = j;i < width; i++) {
|
|
1140
|
+
if (j > 0 && i - 2 >= j - 1) {
|
|
1141
|
+
const candidate = best[j - 1][i - 2] + (i - 2) * GAP;
|
|
1142
|
+
if (candidate > run) {
|
|
1143
|
+
run = candidate;
|
|
1144
|
+
runAt = i - 2;
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
if (haystack[i] !== needle[j])
|
|
1148
|
+
continue;
|
|
1149
|
+
const here = MATCH + (startsWord(text, i) ? BOUNDARY : 0) + (i === 0 ? PREFIX : 0);
|
|
1150
|
+
if (j === 0) {
|
|
1151
|
+
best[0][i] = here - i * GAP;
|
|
1152
|
+
continue;
|
|
1153
|
+
}
|
|
1154
|
+
const joined = best[j - 1][i - 1] + CONSECUTIVE;
|
|
1155
|
+
const skipped = run - (i - 1) * GAP;
|
|
1156
|
+
if (joined > NONE && joined >= skipped) {
|
|
1157
|
+
best[j][i] = joined + here;
|
|
1158
|
+
from[j][i] = i - 1;
|
|
1159
|
+
} else if (skipped > NONE) {
|
|
1160
|
+
best[j][i] = skipped + here;
|
|
1161
|
+
from[j][i] = runAt;
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
const last = best[needle.length - 1];
|
|
1166
|
+
let end = -1;
|
|
1167
|
+
for (let i = 0;i < width; i++) {
|
|
1168
|
+
if (last[i] > NONE && (end === -1 || last[i] > last[end]))
|
|
1169
|
+
end = i;
|
|
1170
|
+
}
|
|
1171
|
+
if (end === -1)
|
|
1172
|
+
return;
|
|
1173
|
+
const positions = [];
|
|
1174
|
+
for (let j = needle.length - 1, i = end;j >= 0; j--) {
|
|
1175
|
+
positions.unshift(i);
|
|
1176
|
+
i = from[j][i];
|
|
1177
|
+
}
|
|
1178
|
+
return { score: last[end], positions };
|
|
1179
|
+
}
|
|
1180
|
+
function rank(query, lines) {
|
|
1181
|
+
if (query === "")
|
|
1182
|
+
return lines.map((_, index) => ({ index, positions: [] }));
|
|
1183
|
+
const found = [];
|
|
1184
|
+
for (const [index, line] of lines.entries()) {
|
|
1185
|
+
const match = fuzzy(query, line);
|
|
1186
|
+
if (match !== undefined)
|
|
1187
|
+
found.push({ index, positions: match.positions, score: match.score });
|
|
1188
|
+
}
|
|
1189
|
+
return found.sort((a, b) => b.score - a.score || lines[a.index].length - lines[b.index].length || a.index - b.index);
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
// ../kit/src/picker/state.ts
|
|
1193
|
+
var ANSI2 = /\x1B(?:\[[\x30-\x3F]*[\x20-\x2F]*[\x40-\x7E]|\][^\x07\x1B]*(?:\x07|\x1B\\)|[\x40-\x5A\x5C-\x5F])/g;
|
|
1194
|
+
var CONTROL2 = /[\x00-\x1F\x7F-\x9F]/g;
|
|
1195
|
+
var IS_CONTROL = /[\x00-\x1F\x7F-\x9F]/;
|
|
1196
|
+
var SGR = /^\x1B\[[0-9;]*m$/;
|
|
1197
|
+
var RESET = /^\x1B\[0?m$/;
|
|
1198
|
+
var COLUMN_GAP = 2;
|
|
1199
|
+
var PASTE_START = "\x1B[200~";
|
|
1200
|
+
var PASTE_END = "\x1B[201~";
|
|
1201
|
+
var LAST_WORD = /\S*\s*$/;
|
|
1202
|
+
var KEY_SEQUENCES = /\x1B\[[0-9;]*[A-Z~]|\x1BO[A-Z]|\x1B[\s\S]|[\s\S]/gu;
|
|
1203
|
+
var KEYS = {
|
|
1204
|
+
"\x1B[A": "up",
|
|
1205
|
+
"\x1BOA": "up",
|
|
1206
|
+
"\x10": "up",
|
|
1207
|
+
"\x1B[B": "down",
|
|
1208
|
+
"\x1BOB": "down",
|
|
1209
|
+
"\x0E": "down",
|
|
1210
|
+
"\r": "enter",
|
|
1211
|
+
"\n": "enter",
|
|
1212
|
+
"\x03": "cancel",
|
|
1213
|
+
"\x1B": "cancel",
|
|
1214
|
+
"\x7F": "erase",
|
|
1215
|
+
"\b": "erase",
|
|
1216
|
+
"\x15": "clear",
|
|
1217
|
+
"\x17": "word",
|
|
1218
|
+
"\t": "tab"
|
|
1219
|
+
};
|
|
1220
|
+
var CTRL_KEY = /^ctrl-[abd-gkln-z]$/;
|
|
1221
|
+
function repeat(value, count) {
|
|
1222
|
+
return Array.from({ length: count }).fill(value);
|
|
1223
|
+
}
|
|
1224
|
+
function styled(line) {
|
|
1225
|
+
let text = "";
|
|
1226
|
+
const styles = [];
|
|
1227
|
+
let active = "";
|
|
1228
|
+
let colored = false;
|
|
1229
|
+
let last = 0;
|
|
1230
|
+
function add(chunk) {
|
|
1231
|
+
const flat = chunk.replace(CONTROL2, " ");
|
|
1232
|
+
text += flat;
|
|
1233
|
+
colored ||= active !== "" && flat !== "";
|
|
1234
|
+
styles.push(...repeat(active, flat.length));
|
|
1235
|
+
}
|
|
1236
|
+
for (const found of line.matchAll(ANSI2)) {
|
|
1237
|
+
add(line.slice(last, found.index));
|
|
1238
|
+
last = found.index + found[0].length;
|
|
1239
|
+
if (SGR.test(found[0]))
|
|
1240
|
+
active = RESET.test(found[0]) ? "" : active + found[0];
|
|
1241
|
+
}
|
|
1242
|
+
add(line.slice(last));
|
|
1243
|
+
return { text, styles: colored ? styles : undefined };
|
|
1244
|
+
}
|
|
1245
|
+
function lay(rows, searched) {
|
|
1246
|
+
const parsed = rows.map((row) => row.cells.map(styled));
|
|
1247
|
+
const widths = (parsed[0] ?? []).map((_, column) => Math.max(...parsed.map((cells) => Bun.stringWidth(cells[column]?.text ?? ""))));
|
|
1248
|
+
return parsed.map((cells) => {
|
|
1249
|
+
let text = "";
|
|
1250
|
+
const styles = [];
|
|
1251
|
+
let colored = false;
|
|
1252
|
+
let searchEnd = 0;
|
|
1253
|
+
for (const [column, cell] of cells.entries()) {
|
|
1254
|
+
const last = column === cells.length - 1;
|
|
1255
|
+
const shown = cells.length > 1 && last ? cell.text.trimEnd() : cell.text;
|
|
1256
|
+
text += shown;
|
|
1257
|
+
colored ||= cell.styles !== undefined;
|
|
1258
|
+
styles.push(...cell.styles?.slice(0, shown.length) ?? repeat("", shown.length));
|
|
1259
|
+
if (column < searched)
|
|
1260
|
+
searchEnd = cells.length > 1 ? text.trimEnd().length : text.length;
|
|
1261
|
+
if (!last) {
|
|
1262
|
+
const pad = widths[column] - Bun.stringWidth(cell.text) + COLUMN_GAP;
|
|
1263
|
+
text += " ".repeat(pad);
|
|
1264
|
+
styles.push(...repeat("", pad));
|
|
1265
|
+
}
|
|
1266
|
+
}
|
|
1267
|
+
return { text, styles: colored ? styles : undefined, searchable: text.slice(0, searchEnd) };
|
|
1268
|
+
});
|
|
1269
|
+
}
|
|
1270
|
+
function parseKeys(input, paste = { active: false }, ctrls = []) {
|
|
1271
|
+
const codes = new Map(ctrls.map((name) => [String.fromCharCode(name.codePointAt(name.length - 1) - 96), name]));
|
|
1272
|
+
return (input.match(KEY_SEQUENCES) ?? []).flatMap((sequence) => {
|
|
1273
|
+
const named = codes.get(sequence);
|
|
1274
|
+
const key = named === undefined ? KEYS[sequence] : { ctrl: named };
|
|
1275
|
+
if (sequence === PASTE_START || sequence === PASTE_END) {
|
|
1276
|
+
paste.active = sequence === PASTE_START;
|
|
1277
|
+
return [];
|
|
1278
|
+
}
|
|
1279
|
+
if (paste.active && (sequence === "\r" || sequence === `
|
|
1280
|
+
`))
|
|
1281
|
+
return [];
|
|
1282
|
+
if (key !== undefined)
|
|
1283
|
+
return [key];
|
|
1284
|
+
return IS_CONTROL.test(sequence) || sequence.startsWith("\x1B") ? [] : [{ text: sequence }];
|
|
1285
|
+
});
|
|
1286
|
+
}
|
|
1287
|
+
function createState(rows, { multi = false, prompt = "> ", query = "", header = "", expect = [], searched = Number.POSITIVE_INFINITY } = {}) {
|
|
1288
|
+
const state = { rows: [], items: [], styles: [], searchable: [], searched, header, expect, key: "", status: "", query, matches: [], cursor: 0, marked: new Set, multi, prompt, done: undefined };
|
|
1289
|
+
return withRows(state, rows);
|
|
1290
|
+
}
|
|
1291
|
+
function withRows(state, rows, header = state.header) {
|
|
1292
|
+
const cells = rows.map((row) => typeof row === "string" ? { hidden: [], cells: [row] } : row);
|
|
1293
|
+
const laid = lay(cells, state.searched);
|
|
1294
|
+
const searchable = laid.map((row) => row.searchable);
|
|
1295
|
+
const matches = rank(state.query, searchable);
|
|
1296
|
+
return { ...state, rows: cells, items: laid.map((row) => row.text), styles: laid.map((row) => row.styles), searchable, header, matches, cursor: Math.max(0, Math.min(matches.length - 1, state.cursor)), marked: new Set };
|
|
1297
|
+
}
|
|
1298
|
+
function withQuery(state, query) {
|
|
1299
|
+
return { ...state, query, matches: rank(query, state.searchable), cursor: 0 };
|
|
1300
|
+
}
|
|
1301
|
+
function move(state, by) {
|
|
1302
|
+
return { ...state, cursor: Math.max(0, Math.min(state.matches.length - 1, state.cursor + by)) };
|
|
1303
|
+
}
|
|
1304
|
+
function highlighted(state) {
|
|
1305
|
+
return state.matches[state.cursor]?.index;
|
|
1306
|
+
}
|
|
1307
|
+
function press(state, key) {
|
|
1308
|
+
if (state.done !== undefined)
|
|
1309
|
+
return state;
|
|
1310
|
+
if (typeof key === "object") {
|
|
1311
|
+
if ("text" in key)
|
|
1312
|
+
return withQuery(state, state.query + key.text);
|
|
1313
|
+
return state.expect.includes(key.ctrl) ? { ...state, done: "accept", key: key.ctrl } : state;
|
|
1314
|
+
}
|
|
1315
|
+
switch (key) {
|
|
1316
|
+
case "up":
|
|
1317
|
+
return move(state, -1);
|
|
1318
|
+
case "down":
|
|
1319
|
+
return move(state, 1);
|
|
1320
|
+
case "cancel":
|
|
1321
|
+
return { ...state, done: "cancel" };
|
|
1322
|
+
case "erase":
|
|
1323
|
+
return withQuery(state, [...state.query].slice(0, -1).join(""));
|
|
1324
|
+
case "clear":
|
|
1325
|
+
return withQuery(state, "");
|
|
1326
|
+
case "word":
|
|
1327
|
+
return withQuery(state, state.query.replace(LAST_WORD, ""));
|
|
1328
|
+
case "enter":
|
|
1329
|
+
return state.matches.length === 0 && state.marked.size === 0 ? state : { ...state, done: "accept" };
|
|
1330
|
+
case "tab": {
|
|
1331
|
+
const at = highlighted(state);
|
|
1332
|
+
if (!state.multi || at === undefined)
|
|
1333
|
+
return state;
|
|
1334
|
+
const marked = new Set(state.marked);
|
|
1335
|
+
if (!marked.delete(at))
|
|
1336
|
+
marked.add(at);
|
|
1337
|
+
return move({ ...state, marked }, 1);
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
function chosen(state) {
|
|
1342
|
+
if (state.marked.size > 0)
|
|
1343
|
+
return state.rows.filter((_, index) => state.marked.has(index));
|
|
1344
|
+
const at = highlighted(state);
|
|
1345
|
+
return at === undefined ? [] : [state.rows[at]];
|
|
1346
|
+
}
|
|
920
1347
|
|
|
921
|
-
// ../kit/src/
|
|
1348
|
+
// ../kit/src/picker/terminal.ts
|
|
1349
|
+
import { openSync } from "fs";
|
|
1350
|
+
import process13 from "process";
|
|
1351
|
+
import tty from "tty";
|
|
1352
|
+
|
|
1353
|
+
// ../kit/src/picker/preview.ts
|
|
1354
|
+
import process12 from "process";
|
|
1355
|
+
|
|
1356
|
+
// ../kit/src/shell.ts
|
|
1357
|
+
var SEPARATORS = new Set([`
|
|
1358
|
+
`, ";", "|"]);
|
|
1359
|
+
var PLAIN_BREAKERS = new Set(["<", ">", "(", ")", "&", "|"]);
|
|
1360
|
+
var GLOB_CHARS = new Set(["*", "?", "["]);
|
|
1361
|
+
var COMMENT_PREFIXES = new Set([";", "&", "|", "(", ")"]);
|
|
1362
|
+
var SAFE_WORD = /^[\w@%+=:,./-]+$/;
|
|
1363
|
+
var DOUBLE_QUOTED_ESCAPES = new Set(["$", "`", '"', "\\"]);
|
|
1364
|
+
var KEYWORDS = new Set(["then", "do", "else", "if", "elif", "while", "until", "!", "{", "}"]);
|
|
1365
|
+
var SHELLS = new Set(["sh", "bash", "zsh", "dash", "ash", "ksh", "fish", "csh", "tcsh", "nu", "xonsh", "elvish", "pwsh", "powershell"]);
|
|
1366
|
+
var SCANNED_PROGRAMS = new Set(["su", "runuser", "flock"]);
|
|
1367
|
+
var COMMAND_STRING_PROGRAMS = new Set([...SHELLS, ...SCANNED_PROGRAMS]);
|
|
1368
|
+
var POWERSHELL = new Set(["pwsh", "powershell"]);
|
|
1369
|
+
var SHELL_VALUE_OPTIONS = new Set(["-o", "-O", "--rcfile", "--init-file"]);
|
|
1370
|
+
var FISH_VALUE_OPTIONS = new Set(["-C", "--init-command"]);
|
|
1371
|
+
var PRINTERS = new Set(["echo", "printf"]);
|
|
1372
|
+
var FIND_EXEC = new Set(["-exec", "-execdir", "-ok", "-okdir"]);
|
|
1373
|
+
function wrapper(short = "", long = [], operands = 0, runsString = false) {
|
|
1374
|
+
return { short, long, operands, runsString };
|
|
1375
|
+
}
|
|
1376
|
+
var WRAPPERS = new Map([
|
|
1377
|
+
["command", wrapper()],
|
|
1378
|
+
["builtin", wrapper()],
|
|
1379
|
+
["nohup", wrapper()],
|
|
1380
|
+
["setsid", wrapper()],
|
|
1381
|
+
["busybox", wrapper()],
|
|
1382
|
+
["exec", wrapper("a")],
|
|
1383
|
+
["time", wrapper("fo", ["--format", "--output"])],
|
|
1384
|
+
["env", wrapper("uCS", ["--unset", "--chdir", "--split-string", "--argv0"])],
|
|
1385
|
+
["sudo", wrapper("ughpCTrtUDR", ["--user", "--group", "--host", "--prompt", "--close-from", "--command-timeout", "--role", "--type", "--other-user", "--chdir", "--chroot"])],
|
|
1386
|
+
["doas", wrapper("uC")],
|
|
1387
|
+
["timeout", wrapper("sk", ["--signal", "--kill-after"], 1)],
|
|
1388
|
+
["nice", wrapper("n", ["--adjustment"])],
|
|
1389
|
+
["ionice", wrapper("cnpPu", ["--class", "--classdata", "--pid", "--pgid", "--uid"])],
|
|
1390
|
+
["stdbuf", wrapper("ioe", ["--input", "--output", "--error"])],
|
|
1391
|
+
["chroot", wrapper("", ["--userspec", "--groups"], 1)],
|
|
1392
|
+
["watch", wrapper("n", ["--interval"], 0, true)],
|
|
1393
|
+
["flock", wrapper("wE", ["--timeout", "--conflict-exit-code"], 1)],
|
|
1394
|
+
["parallel", wrapper("jnNSaIPlLdTsC", ["--jobs", "--max-args", "--max-replace-args", "--sshlogin", "--arg-file", "--delimiter", "--colsep"], 0, true)],
|
|
1395
|
+
["unbuffer", wrapper()],
|
|
1396
|
+
["systemd-run", wrapper("puEMH", ["--property", "--unit", "--description", "--slice", "--setenv", "--machine", "--host", "--uid", "--gid", "--working-directory", "--nice", "--service-type"])],
|
|
1397
|
+
["xargs", wrapper("IndsEaPL", ["--max-args", "--max-procs", "--max-chars", "--delimiter", "--arg-file", "--eof", "--max-lines", "--process-slot-var"])]
|
|
1398
|
+
]);
|
|
1399
|
+
function shellQuote(word) {
|
|
1400
|
+
return SAFE_WORD.test(word) && !word.startsWith("=") ? word : `'${word.replaceAll("'", "'\\''")}'`;
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
// ../kit/src/picker/preview.ts
|
|
1404
|
+
var PREVIEW_BYTES = 64 * 1024;
|
|
1405
|
+
var PREVIEW_LINES = 200;
|
|
1406
|
+
var PREVIEW_TIMEOUT_MS = 3000;
|
|
1407
|
+
var PLACEHOLDER = /\{([1-9]\d*)?\}/g;
|
|
1408
|
+
function previewCommand(template, line, hidden = []) {
|
|
1409
|
+
return template.replace(PLACEHOLDER, (_, field) => shellQuote(field === undefined ? line : hidden[Number(field) - 1] ?? ""));
|
|
1410
|
+
}
|
|
1411
|
+
function previewLines(output) {
|
|
1412
|
+
const lines = output.split(`
|
|
1413
|
+
`);
|
|
1414
|
+
if (lines.at(-1) === "")
|
|
1415
|
+
lines.pop();
|
|
1416
|
+
return lines.slice(0, PREVIEW_LINES);
|
|
1417
|
+
}
|
|
1418
|
+
async function runPreview(template, line, signal, hidden = []) {
|
|
1419
|
+
const proc = Bun.spawn(["sh", "-c", `exec 2>&1
|
|
1420
|
+
${previewCommand(template, line, hidden)}`], { stdin: "ignore", stdout: "pipe", stderr: "ignore", detached: true });
|
|
1421
|
+
const stop = () => {
|
|
1422
|
+
try {
|
|
1423
|
+
process12.kill(-proc.pid, "SIGKILL");
|
|
1424
|
+
} catch {
|
|
1425
|
+
proc.kill();
|
|
1426
|
+
}
|
|
1427
|
+
};
|
|
1428
|
+
const timer = setTimeout(stop, PREVIEW_TIMEOUT_MS);
|
|
1429
|
+
const chunks = [];
|
|
1430
|
+
let size = 0;
|
|
1431
|
+
signal.addEventListener("abort", stop);
|
|
1432
|
+
try {
|
|
1433
|
+
for await (const chunk of proc.stdout) {
|
|
1434
|
+
chunks.push(chunk);
|
|
1435
|
+
size += chunk.length;
|
|
1436
|
+
if (size >= PREVIEW_BYTES) {
|
|
1437
|
+
stop();
|
|
1438
|
+
break;
|
|
1439
|
+
}
|
|
1440
|
+
}
|
|
1441
|
+
} finally {
|
|
1442
|
+
clearTimeout(timer);
|
|
1443
|
+
signal.removeEventListener("abort", stop);
|
|
1444
|
+
}
|
|
1445
|
+
return signal.aborted ? [] : previewLines(Buffer.concat(chunks).subarray(0, PREVIEW_BYTES).toString());
|
|
1446
|
+
}
|
|
1447
|
+
|
|
1448
|
+
// ../kit/src/picker/view.ts
|
|
1449
|
+
var MIN_HEIGHT = 4;
|
|
1450
|
+
var PREVIEW_MIN_COLUMNS = 80;
|
|
1451
|
+
var PREVIEW_MIN_ROWS = 10;
|
|
1452
|
+
var PREVIEW_SHARE = 0.4;
|
|
1453
|
+
var RESET2 = "\x1B[0m";
|
|
1454
|
+
var SEPARATOR = " \u2502 ";
|
|
1455
|
+
var HEIGHT = /^(\d+)(%?)$/;
|
|
1456
|
+
function parseHeight(text) {
|
|
1457
|
+
const found = HEIGHT.exec(text);
|
|
1458
|
+
if (found === null || Number(found[1]) < 1)
|
|
1459
|
+
return;
|
|
1460
|
+
return found[2] === "%" ? { percent: Math.min(100, Number(found[1])) } : { lines: Number(found[1]) };
|
|
1461
|
+
}
|
|
1462
|
+
function layoutFor(size, height, previewing, position = "right") {
|
|
1463
|
+
const wanted = "lines" in height ? height.lines : Math.round(size.rows * height.percent / 100);
|
|
1464
|
+
const rows = Math.min(size.rows, Math.max(MIN_HEIGHT, wanted));
|
|
1465
|
+
if (position === "down") {
|
|
1466
|
+
const previewHeight = previewing && rows >= PREVIEW_MIN_ROWS ? Math.round(rows * PREVIEW_SHARE) : 0;
|
|
1467
|
+
return { height: rows, listHeight: rows - previewHeight, listWidth: size.columns, previewWidth: 0, previewHeight };
|
|
1468
|
+
}
|
|
1469
|
+
const split = previewing && size.columns >= PREVIEW_MIN_COLUMNS;
|
|
1470
|
+
const listWidth = split ? Math.floor(size.columns / 2) : size.columns;
|
|
1471
|
+
return { height: rows, listHeight: rows, listWidth, previewWidth: split ? size.columns - listWidth - SEPARATOR.length : 0, previewHeight: 0 };
|
|
1472
|
+
}
|
|
1473
|
+
function fit(text, width, plain, mark = plain, marked = new Set, styles) {
|
|
1474
|
+
const room = Bun.stringWidth(text) > width ? width - 1 : width;
|
|
1475
|
+
let out = "";
|
|
1476
|
+
let used = 0;
|
|
1477
|
+
let at = 0;
|
|
1478
|
+
for (const char of text) {
|
|
1479
|
+
const size = Bun.stringWidth(char);
|
|
1480
|
+
if (used + size > room)
|
|
1481
|
+
break;
|
|
1482
|
+
const style = marked.has(at) ? "" : styles?.[at] ?? "";
|
|
1483
|
+
out += style === "" ? (marked.has(at) ? mark : plain)(char) : `${style}${char}${RESET2}`;
|
|
1484
|
+
used += size;
|
|
1485
|
+
at += char.length;
|
|
1486
|
+
}
|
|
1487
|
+
if (room < width) {
|
|
1488
|
+
out += plain("\u2026");
|
|
1489
|
+
used += 1;
|
|
1490
|
+
}
|
|
1491
|
+
return { text: out, width: used };
|
|
1492
|
+
}
|
|
1493
|
+
var identity = (text) => text;
|
|
1494
|
+
function fitStyled(line, width, plain = identity) {
|
|
1495
|
+
const { text, styles } = styled(line);
|
|
1496
|
+
return fit(text, width, plain, plain, new Set, styles);
|
|
1497
|
+
}
|
|
1498
|
+
function caretColumn(state, layout) {
|
|
1499
|
+
return Math.min(layout.listWidth, Bun.stringWidth(state.prompt) + Bun.stringWidth(state.query) + 1);
|
|
1500
|
+
}
|
|
1501
|
+
function listLines(state, layout, theme) {
|
|
1502
|
+
const header = state.header === "" ? [] : state.header.split(`
|
|
1503
|
+
`).map((line) => fitStyled(line, layout.listWidth, theme.label));
|
|
1504
|
+
const rows = Math.max(1, layout.listHeight - 2 - header.length);
|
|
1505
|
+
const prefixWidth = state.multi ? 4 : 2;
|
|
1506
|
+
const top = Math.max(0, Math.min(state.cursor - Math.floor(rows / 2), state.matches.length - rows));
|
|
1507
|
+
const marks = (state.marked.size > 0 ? ` (${state.marked.size} marked)` : "") + (state.status === "" ? "" : ` ${state.status}`);
|
|
1508
|
+
const promptWidth = Bun.stringWidth(state.prompt);
|
|
1509
|
+
const query = fit(state.query, Math.max(1, layout.listWidth - promptWidth), identity);
|
|
1510
|
+
const count = fit(` ${state.matches.length}/${state.items.length}${marks}`, layout.listWidth, theme.label);
|
|
1511
|
+
const lines = [{ text: theme.accent(state.prompt) + query.text, width: promptWidth + query.width }, count, ...header];
|
|
1512
|
+
if (state.matches.length === 0)
|
|
1513
|
+
lines.push(fit(" no match", layout.listWidth, theme.label));
|
|
1514
|
+
for (const [offset, match] of state.matches.slice(top, top + rows).entries()) {
|
|
1515
|
+
const current = top + offset === state.cursor;
|
|
1516
|
+
const pointer = current ? theme.accent("\u276F") : " ";
|
|
1517
|
+
const mark = state.multi ? `${state.marked.has(match.index) ? theme.id("\u25CF") : " "} ` : "";
|
|
1518
|
+
const text = fit(state.items[match.index], layout.listWidth - prefixWidth, current ? theme.value : identity, theme.accent, new Set(match.positions), state.styles[match.index]);
|
|
1519
|
+
lines.push({ text: `${pointer} ${mark}${text.text}`, width: prefixWidth + text.width });
|
|
1520
|
+
}
|
|
1521
|
+
return lines;
|
|
1522
|
+
}
|
|
1523
|
+
function render(state, layout, theme, preview = []) {
|
|
1524
|
+
const left = listLines(state, layout, theme);
|
|
1525
|
+
return Array.from({ length: layout.height }, (_, row) => {
|
|
1526
|
+
if (row >= layout.listHeight)
|
|
1527
|
+
return row === layout.listHeight ? theme.label("\u2500".repeat(layout.listWidth)) : fitStyled(preview[row - layout.listHeight - 1] ?? "", layout.listWidth).text;
|
|
1528
|
+
const list = left[row] ?? { text: "", width: 0 };
|
|
1529
|
+
if (layout.previewWidth === 0)
|
|
1530
|
+
return list.text;
|
|
1531
|
+
return `${list.text}${" ".repeat(Math.max(0, layout.listWidth - list.width))}${theme.label(SEPARATOR)}${fitStyled(preview[row] ?? "", layout.previewWidth).text}`;
|
|
1532
|
+
});
|
|
1533
|
+
}
|
|
1534
|
+
|
|
1535
|
+
// ../kit/src/picker/terminal.ts
|
|
1536
|
+
var PREVIEW_DELAY_MS = 120;
|
|
1537
|
+
var OPEN = { ctrl: "open" };
|
|
1538
|
+
var HIDE_CURSOR = "\x1B[?25l";
|
|
1539
|
+
var SHOW_CURSOR = "\x1B[?25h";
|
|
1540
|
+
var PASTE_ON = "\x1B[?2004h";
|
|
1541
|
+
var PASTE_OFF = "\x1B[?2004l";
|
|
1542
|
+
var CLEAR_BELOW = "\r\x1B[J";
|
|
1543
|
+
var SIGNALS2 = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
1544
|
+
function up(lines) {
|
|
1545
|
+
return lines > 0 ? `\x1B[${lines}A` : "";
|
|
1546
|
+
}
|
|
1547
|
+
function openTerminal() {
|
|
1548
|
+
let input;
|
|
1549
|
+
try {
|
|
1550
|
+
input = new tty.ReadStream(openSync("/dev/tty", "r"));
|
|
1551
|
+
} catch {
|
|
1552
|
+
return fail("no terminal to read keys from (/dev/tty)");
|
|
1553
|
+
}
|
|
1554
|
+
const out = process13.stderr;
|
|
1555
|
+
let resize;
|
|
1556
|
+
return {
|
|
1557
|
+
get columns() {
|
|
1558
|
+
return out.columns || 80;
|
|
1559
|
+
},
|
|
1560
|
+
get rows() {
|
|
1561
|
+
return out.rows || 24;
|
|
1562
|
+
},
|
|
1563
|
+
theme: { accent: role("accent", out), label: role("label", out), value: role("value", out), id: role("id", out) },
|
|
1564
|
+
write: (text) => void out.write(text),
|
|
1565
|
+
listen(onChunk, onResize) {
|
|
1566
|
+
resize = onResize;
|
|
1567
|
+
input.setRawMode(true);
|
|
1568
|
+
out.on("resize", onResize);
|
|
1569
|
+
input.on("data", (chunk) => onChunk(chunk.toString()));
|
|
1570
|
+
},
|
|
1571
|
+
close() {
|
|
1572
|
+
if (resize !== undefined)
|
|
1573
|
+
out.off("resize", resize);
|
|
1574
|
+
try {
|
|
1575
|
+
input.setRawMode(false);
|
|
1576
|
+
} finally {
|
|
1577
|
+
input.destroy();
|
|
1578
|
+
}
|
|
1579
|
+
}
|
|
1580
|
+
};
|
|
1581
|
+
}
|
|
1582
|
+
async function select(rows, settings, terminal = openTerminal()) {
|
|
1583
|
+
const { theme } = terminal;
|
|
1584
|
+
let state = createState(rows, settings);
|
|
1585
|
+
let preview = [];
|
|
1586
|
+
let reserved = 0;
|
|
1587
|
+
let previewTimer;
|
|
1588
|
+
let previewing = new AbortController;
|
|
1589
|
+
let shownLine;
|
|
1590
|
+
let finish;
|
|
1591
|
+
let restored = false;
|
|
1592
|
+
function draw(clear = false) {
|
|
1593
|
+
if (restored)
|
|
1594
|
+
return;
|
|
1595
|
+
const layout = layoutFor({ columns: terminal.columns, rows: terminal.rows }, settings.height, settings.preview !== undefined, settings.previewPosition);
|
|
1596
|
+
let frame = HIDE_CURSOR;
|
|
1597
|
+
if (reserved !== layout.height || clear) {
|
|
1598
|
+
frame += CLEAR_BELOW + `
|
|
1599
|
+
`.repeat(layout.height - 1) + up(layout.height - 1);
|
|
1600
|
+
reserved = layout.height;
|
|
1601
|
+
}
|
|
1602
|
+
frame += `\r${render(state, layout, theme, preview).map((line) => `\x1B[2K${line}`).join(`
|
|
1603
|
+
`)}${up(layout.height - 1)}\r\x1B[${caretColumn(state, layout)}G${SHOW_CURSOR}`;
|
|
1604
|
+
terminal.write(frame);
|
|
1605
|
+
}
|
|
1606
|
+
function refreshPreview() {
|
|
1607
|
+
const at = highlighted(state);
|
|
1608
|
+
if (settings.preview === undefined || at === shownLine)
|
|
1609
|
+
return;
|
|
1610
|
+
shownLine = at;
|
|
1611
|
+
clearTimeout(previewTimer);
|
|
1612
|
+
previewing.abort();
|
|
1613
|
+
previewing = new AbortController;
|
|
1614
|
+
if (at === undefined) {
|
|
1615
|
+
preview = [];
|
|
1616
|
+
return;
|
|
1617
|
+
}
|
|
1618
|
+
const { signal } = previewing;
|
|
1619
|
+
const template = settings.preview;
|
|
1620
|
+
const row = state.rows[at];
|
|
1621
|
+
async function show() {
|
|
1622
|
+
const shown = typeof template === "string" ? await runPreview(template, row.cells.join(" "), signal, row.hidden) : await previewOf(template, row.hidden, signal);
|
|
1623
|
+
if (!signal.aborted) {
|
|
1624
|
+
preview = shown;
|
|
1625
|
+
draw();
|
|
1626
|
+
}
|
|
1627
|
+
}
|
|
1628
|
+
previewTimer = setTimeout(() => void show(), PREVIEW_DELAY_MS);
|
|
1629
|
+
}
|
|
1630
|
+
function restore() {
|
|
1631
|
+
if (restored)
|
|
1632
|
+
return;
|
|
1633
|
+
restored = true;
|
|
1634
|
+
clearTimeout(previewTimer);
|
|
1635
|
+
previewing.abort();
|
|
1636
|
+
terminal.write(`${CLEAR_BELOW}${PASTE_OFF}${SHOW_CURSOR}`);
|
|
1637
|
+
terminal.close();
|
|
1638
|
+
}
|
|
1639
|
+
const stop = () => {
|
|
1640
|
+
state = { ...state, done: "cancel" };
|
|
1641
|
+
finish?.();
|
|
1642
|
+
};
|
|
1643
|
+
const paste = { active: false };
|
|
1644
|
+
const { reload } = settings;
|
|
1645
|
+
const pending = [];
|
|
1646
|
+
let busy = false;
|
|
1647
|
+
let draining = false;
|
|
1648
|
+
async function reloadRows() {
|
|
1649
|
+
if (reload === undefined)
|
|
1650
|
+
return;
|
|
1651
|
+
busy = true;
|
|
1652
|
+
state = { ...state, status: "reloading" };
|
|
1653
|
+
draw();
|
|
1654
|
+
try {
|
|
1655
|
+
const next = await reload.run();
|
|
1656
|
+
state = { ...withRows(state, next.rows, next.header), status: "" };
|
|
1657
|
+
shownLine = undefined;
|
|
1658
|
+
} catch (error) {
|
|
1659
|
+
state = { ...state, status: `reload failed: ${error instanceof Error ? error.message : String(error)}` };
|
|
1660
|
+
} finally {
|
|
1661
|
+
busy = false;
|
|
1662
|
+
}
|
|
1663
|
+
}
|
|
1664
|
+
async function drain() {
|
|
1665
|
+
if (draining)
|
|
1666
|
+
return;
|
|
1667
|
+
draining = true;
|
|
1668
|
+
try {
|
|
1669
|
+
for (let key = pending.shift();key !== undefined && state.done === undefined; key = pending.shift()) {
|
|
1670
|
+
if (key === OPEN || typeof key === "object" && "ctrl" in key && key.ctrl === reload?.key)
|
|
1671
|
+
await reloadRows();
|
|
1672
|
+
else
|
|
1673
|
+
state = press(state, key);
|
|
1674
|
+
}
|
|
1675
|
+
} finally {
|
|
1676
|
+
draining = false;
|
|
1677
|
+
}
|
|
1678
|
+
if (state.done !== undefined) {
|
|
1679
|
+
finish?.();
|
|
1680
|
+
return;
|
|
1681
|
+
}
|
|
1682
|
+
refreshPreview();
|
|
1683
|
+
draw();
|
|
1684
|
+
}
|
|
1685
|
+
function onChunk(chunk) {
|
|
1686
|
+
const keys = parseKeys(chunk, paste, [...settings.expect ?? [], ...reload?.key === undefined ? [] : [reload.key]]);
|
|
1687
|
+
if (busy && keys.includes("cancel")) {
|
|
1688
|
+
stop();
|
|
1689
|
+
return;
|
|
1690
|
+
}
|
|
1691
|
+
pending.push(...keys);
|
|
1692
|
+
drain();
|
|
1693
|
+
}
|
|
1694
|
+
const ended = new Promise((resolve) => {
|
|
1695
|
+
finish = resolve;
|
|
1696
|
+
});
|
|
1697
|
+
process13.once("exit", restore);
|
|
1698
|
+
for (const signal of SIGNALS2)
|
|
1699
|
+
process13.once(signal, stop);
|
|
1700
|
+
try {
|
|
1701
|
+
terminal.write(PASTE_ON);
|
|
1702
|
+
terminal.listen(onChunk, () => draw(true));
|
|
1703
|
+
refreshPreview();
|
|
1704
|
+
draw(true);
|
|
1705
|
+
if (reload?.onOpen === true) {
|
|
1706
|
+
pending.push(OPEN);
|
|
1707
|
+
drain();
|
|
1708
|
+
}
|
|
1709
|
+
await ended;
|
|
1710
|
+
} finally {
|
|
1711
|
+
for (const signal of SIGNALS2)
|
|
1712
|
+
process13.off(signal, stop);
|
|
1713
|
+
process13.off("exit", restore);
|
|
1714
|
+
restore();
|
|
1715
|
+
}
|
|
1716
|
+
return state.done === "accept" ? { rows: chosen(state), key: state.key, query: state.query } : undefined;
|
|
1717
|
+
}
|
|
1718
|
+
async function previewOf(preview, hidden, signal) {
|
|
1719
|
+
try {
|
|
1720
|
+
return previewLines(await preview(hidden, signal));
|
|
1721
|
+
} catch (error) {
|
|
1722
|
+
return [error instanceof Error ? error.message : String(error)];
|
|
1723
|
+
}
|
|
1724
|
+
}
|
|
1725
|
+
// ../kit/src/picker.ts
|
|
922
1726
|
function table(rows) {
|
|
923
1727
|
const widths = rows[0]?.cells.map((_, index) => Math.max(...rows.map((row) => Bun.stringWidth(row.cells[index])))) ?? [];
|
|
924
1728
|
return rows.map((row) => [
|
|
@@ -926,47 +1730,17 @@ function table(rows) {
|
|
|
926
1730
|
...row.cells.map((cell, index) => index === row.cells.length - 1 ? cell.trimEnd() : `${cell}${" ".repeat(widths[index] + 1 - Bun.stringWidth(cell))}`)
|
|
927
1731
|
].join("\t"));
|
|
928
1732
|
}
|
|
929
|
-
function
|
|
930
|
-
const
|
|
931
|
-
const
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
options.height ?? "100%",
|
|
941
|
-
"--delimiter",
|
|
942
|
-
"\t",
|
|
943
|
-
"--with-nth",
|
|
944
|
-
`${hidden + 1}..`,
|
|
945
|
-
"--nth",
|
|
946
|
-
`1..${options.searched}`,
|
|
947
|
-
"--tabstop",
|
|
948
|
-
"1",
|
|
949
|
-
"--tiebreak",
|
|
950
|
-
"chunk,index",
|
|
951
|
-
...options.preview === undefined ? [] : ["--preview", options.preview, "--preview-window", "down,40%"],
|
|
952
|
-
...(options.bind ?? []).flatMap((bind) => ["--bind", bind]),
|
|
953
|
-
...options.query === true ? ["--print-query"] : []
|
|
954
|
-
], {
|
|
955
|
-
stdin: new TextEncoder().encode(table(rows).join(`
|
|
956
|
-
`)),
|
|
957
|
-
stdout: "pipe",
|
|
958
|
-
stderr: "inherit"
|
|
959
|
-
});
|
|
960
|
-
return picked(proc.stdout.toString(), hidden, options);
|
|
961
|
-
}
|
|
962
|
-
function picked(output, hidden, options) {
|
|
963
|
-
const lines = output.split(`
|
|
964
|
-
`);
|
|
965
|
-
const query = options.query === true ? lines.shift() : "";
|
|
966
|
-
const [key, line] = (options.expect ?? []).length > 0 ? [lines[0], lines[1]] : ["", lines[0]];
|
|
967
|
-
if (line !== undefined && line !== "")
|
|
968
|
-
return { key, hidden: line.split("\t").slice(0, hidden), query };
|
|
969
|
-
return options.query === true && key !== "" ? { key, hidden: [], query } : undefined;
|
|
1733
|
+
async function pick(rows, options) {
|
|
1734
|
+
const height = parseHeight(options.height ?? "100%");
|
|
1735
|
+
const keys = [...options.expect ?? [], ...options.reload?.key === undefined ? [] : [options.reload.key]];
|
|
1736
|
+
if (height === undefined)
|
|
1737
|
+
throw new Error(`pick: the height is lines or a percentage, not ${options.height}`);
|
|
1738
|
+
for (const key of keys) {
|
|
1739
|
+
if (!CTRL_KEY.test(key))
|
|
1740
|
+
throw new Error(`pick: ${key} is not a key it can report, one of ctrl-a to ctrl-z but ctrl-c, h, i, j and m`);
|
|
1741
|
+
}
|
|
1742
|
+
const found = await select(rows, { multi: false, prompt: options.prompt, query: "", height, preview: options.preview, previewPosition: "down", header: options.header, expect: options.expect, searched: options.searched, reload: options.reload });
|
|
1743
|
+
return found === undefined ? undefined : { key: found.key, hidden: found.rows[0]?.hidden ?? [], query: found.query };
|
|
970
1744
|
}
|
|
971
1745
|
function word(arg) {
|
|
972
1746
|
if (typeof arg === "string")
|
|
@@ -988,7 +1762,7 @@ gb -
|
|
|
988
1762
|
gb -c <name>
|
|
989
1763
|
gb -d <branch>
|
|
990
1764
|
|
|
991
|
-
Without a branch, picks one
|
|
1765
|
+
Without a branch, picks one when a terminal is attached: the branches last checked out first, as the reflog has them, then the other local ones, then the remote ones with no local branch yet. The header says where HEAD stands, each row how its branch stands against its upstream and whether it is merged into the base. enter switches to it (a remote one gets its local tracking branch), ctrl-n creates a branch named after the query, ctrl-d deletes the highlighted local branch with \`git branch -d\`, which refuses one not merged yet, and ctrl-r fetches, prunes and lists again. Without a terminal it lists the branches instead.
|
|
992
1766
|
\`gb <branch>\` switches without the picker, a bare name finding the remote branch it would create. \`gb -\` goes back to the previous branch, \`gb -c <name>\` creates one and switches to it, \`gb -d <branch>\` deletes a local one.
|
|
993
1767
|
A branch checked out in another worktree cannot be switched to: gb prints that worktree's path on stdout and a hint on stderr, since a command cannot change its parent shell's folder.`;
|
|
994
1768
|
var EXITS = [[0, "it switched, created, deleted or listed"], ["git", "the code of the git command it ran, when that failed"], [1, "there is no branch by that name or nothing was picked"], [2, "outside a git repository or a usage error"]];
|
|
@@ -999,7 +1773,7 @@ var options = {
|
|
|
999
1773
|
};
|
|
1000
1774
|
var FIELDS = ["%(refname)", "%(refname:short)", "%(committerdate:unix)", "%(committerdate:relative)", "%(subject)", "%(worktreepath)", "%(upstream:short)", "%(upstream:track,nobracket)"];
|
|
1001
1775
|
var CHECKOUT = /^checkout: moving from (\S+) to (\S+)$/;
|
|
1002
|
-
var
|
|
1776
|
+
var KEYS2 = "enter go, ctrl-n new from query, ctrl-d delete if merged, ctrl-r fetch";
|
|
1003
1777
|
var TRACK_TINTS = { ahead: "green", behind: "red", gone: "yellow" };
|
|
1004
1778
|
var TRACK_WORD = /^\w+/;
|
|
1005
1779
|
var WHITESPACE = /\s+/;
|
|
@@ -1126,7 +1900,12 @@ function header(found, current, failure = "") {
|
|
|
1126
1900
|
const here = found.find((branch) => !branch.remote && branch.name === current) ?? (current ? { name: current, upstream: "", track: "" } : undefined);
|
|
1127
1901
|
const line = status(here, here ? "" : read(["rev-parse", "--short", "HEAD"]), changed);
|
|
1128
1902
|
return `${line}${failure ? colors.dim(" | ") + colors.red(failure) : ""}
|
|
1129
|
-
${
|
|
1903
|
+
${KEYS2}`;
|
|
1904
|
+
}
|
|
1905
|
+
async function fetched() {
|
|
1906
|
+
const proc = Bun.spawn(["git", "fetch", "--all", "--prune", "--quiet"], { stdout: "ignore", stderr: "ignore" });
|
|
1907
|
+
const success = await proc.exited === 0;
|
|
1908
|
+
return header(branches(base()), read(["branch", "--show-current"]), success ? "" : "fetch failed");
|
|
1130
1909
|
}
|
|
1131
1910
|
function listed2(all) {
|
|
1132
1911
|
list([
|
|
@@ -1143,7 +1922,7 @@ function listed2(all) {
|
|
|
1143
1922
|
{ label: "New branch", hint: "git switch -c", typed: "-c", asks: "" }
|
|
1144
1923
|
]);
|
|
1145
1924
|
}
|
|
1146
|
-
function hidden(flag, rest) {
|
|
1925
|
+
async function hidden(flag, rest) {
|
|
1147
1926
|
if (flag === "--show") {
|
|
1148
1927
|
console.log(preview(rest[0] ?? ""));
|
|
1149
1928
|
return 0;
|
|
@@ -1151,19 +1930,13 @@ function hidden(flag, rest) {
|
|
|
1151
1930
|
if (read(["rev-parse", "--git-dir"]) === "")
|
|
1152
1931
|
fail("not a git repository", 2);
|
|
1153
1932
|
if (flag === "--fetch") {
|
|
1154
|
-
|
|
1155
|
-
console.log(header(branches(base()), read(["branch", "--show-current"]), fetched ? "" : "fetch failed"));
|
|
1933
|
+
console.log(await fetched());
|
|
1156
1934
|
return 0;
|
|
1157
1935
|
}
|
|
1158
1936
|
if (flag === "--list") {
|
|
1159
1937
|
listed2(load().all);
|
|
1160
1938
|
return 0;
|
|
1161
1939
|
}
|
|
1162
|
-
if (flag === "--rows") {
|
|
1163
|
-
console.log(table(load().all.map(row)).join(`
|
|
1164
|
-
`));
|
|
1165
|
-
return 0;
|
|
1166
|
-
}
|
|
1167
1940
|
if (flag === "--names") {
|
|
1168
1941
|
console.log(load().all.map((branch) => `${branch.name} ${branch.when}, ${branch.subject}`).join(`
|
|
1169
1942
|
`));
|
|
@@ -1179,8 +1952,8 @@ function create(name) {
|
|
|
1179
1952
|
}
|
|
1180
1953
|
async function gb(args) {
|
|
1181
1954
|
const [flag = ""] = args;
|
|
1182
|
-
if (["--show", "--fetch", "--list", "--
|
|
1183
|
-
return hidden(flag, args.slice(1)) ?? 0;
|
|
1955
|
+
if (["--show", "--fetch", "--list", "--names"].includes(flag))
|
|
1956
|
+
return await hidden(flag, args.slice(1)) ?? 0;
|
|
1184
1957
|
const emitting = flag === "--emit";
|
|
1185
1958
|
const parsed = parse(emitting ? args.slice(1) : args, usage2, options, { exits: EXITS, replaces: "git switch, git branch -d and git checkout typed per branch", examples: ["gb", ["gb feature", "switch to feature, or create it from origin/feature"], ["gb -", "back to the previous branch"], ["gb -c fix/login", "create a branch and switch to it"], ["gb -d old", "delete a merged local branch"]] });
|
|
1186
1959
|
const [named] = parsed.positionals;
|
|
@@ -1201,29 +1974,24 @@ async function gb(args) {
|
|
|
1201
1974
|
let branch;
|
|
1202
1975
|
let key = "";
|
|
1203
1976
|
if (named === undefined) {
|
|
1204
|
-
const interactive =
|
|
1205
|
-
if (!interactive
|
|
1977
|
+
const interactive = process14.stdin.isTTY && process14.stderr.isTTY && parsed.values.json !== true;
|
|
1978
|
+
if (!interactive) {
|
|
1206
1979
|
if (emitting)
|
|
1207
|
-
fail("the picker needs a terminal
|
|
1208
|
-
if (interactive)
|
|
1209
|
-
console.error("fzf is not installed, listing the branches instead");
|
|
1980
|
+
fail("the picker needs a terminal", 1);
|
|
1210
1981
|
if (parsed.values.json === true)
|
|
1211
1982
|
printJson(all);
|
|
1212
1983
|
else
|
|
1213
|
-
console.log(table(all.map(row)).map((line) =>
|
|
1984
|
+
console.log(table(all.map(row)).map((line) => takesControlCodes(process14.stdout) ? line : Bun.stripANSI(line)).join(`
|
|
1214
1985
|
`));
|
|
1215
1986
|
return 0;
|
|
1216
1987
|
}
|
|
1217
|
-
const
|
|
1218
|
-
const picked = fzf(all.map(row), {
|
|
1988
|
+
const picked = await pick(all.map(row), {
|
|
1219
1989
|
prompt: "gb> ",
|
|
1220
1990
|
header: header(found, current),
|
|
1221
1991
|
expect: ["ctrl-d", "ctrl-n"],
|
|
1222
1992
|
searched: 1,
|
|
1223
|
-
preview:
|
|
1224
|
-
|
|
1225
|
-
query: true,
|
|
1226
|
-
hidden: 1
|
|
1993
|
+
preview: (fields) => preview(fields[0]),
|
|
1994
|
+
reload: { key: "ctrl-r", run: async () => ({ header: await fetched(), rows: load().all.map(row) }) }
|
|
1227
1995
|
});
|
|
1228
1996
|
if (picked?.key === "ctrl-n") {
|
|
1229
1997
|
const name = picked.query.trim();
|
|
@@ -1270,4 +2038,4 @@ async function finish(command, typed, emitting) {
|
|
|
1270
2038
|
}
|
|
1271
2039
|
|
|
1272
2040
|
// src/gb.ts
|
|
1273
|
-
|
|
2041
|
+
process15.exitCode = await runMain({ name: "gb", run: gb });
|