@kaynooo/cli-git 0.15.1 → 0.16.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 +11 -1
- package/dist/gb.js +262 -60
- package/dist/gc.js +81 -33
- package/dist/gcl.js +70 -32
- package/dist/gd.js +77 -39
- package/dist/gf.js +74 -36
- package/dist/gm.js +2885 -0
- package/dist/gp.js +74 -36
- package/dist/gst.js +115 -50
- package/dist/gu.js +68 -29
- package/package.json +6 -4
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# gst, gc, gb, gp, gf, gcl, gd and gu
|
|
1
|
+
# gst, gc, gb, gp, gf, gm, gcl, gd and gu
|
|
2
2
|
|
|
3
3
|
Git for Claude Code, where a session would otherwise chain `git status --short` and `git log --oneline` to read the state, or `git add <paths> && git status && git commit -m ...` to commit and have the commit bounced over its message.
|
|
4
4
|
|
|
@@ -51,6 +51,16 @@ Prints one line with the branch, its upstream and how far ahead and behind it is
|
|
|
51
51
|
- `--json` prints `{path, branch, upstream, commits, outcome, reason?}` per repository, `outcome` being `updated`, `current`, `fetched`, `skipped` or `refused`.
|
|
52
52
|
- Exit codes: 0 every repository was fetched, and fast-forwarded when it could be, 1 a fetch failed, a branch diverged or a fast-forward was refused, 2 outside a repository or a usage error.
|
|
53
53
|
|
|
54
|
+
## gm
|
|
55
|
+
|
|
56
|
+
`gm <branch>` merges the branch into the current one with `git merge --no-edit` and prints what came in: fast-forward or merge commit, the commits and the files changed. A working tree with uncommitted changes is refused.
|
|
57
|
+
|
|
58
|
+
- On a conflict every conflicted file is listed with its conflict regions (the markers and two lines around them, line numbers included) and the exit is 1; `gm --continue` commits once the markers are gone, refusing while one remains, and `gm --abort` gives the merge up.
|
|
59
|
+
- `gm --dry-run <branch>` says whether the merge would conflict, and in which files, with `git merge-tree` and nothing touched.
|
|
60
|
+
- `--squash` stages the branch's changes without committing them, to commit with `gc`.
|
|
61
|
+
- With no branch, a terminal gets a picker of the other branches (ahead and behind the current one, a preview of the commits, files and verdict; enter merges, ctrl-s squashes); without a terminal the branches are listed with their verdict, `--json` giving `[{name, ahead, behind, outcome, conflicts}]`.
|
|
62
|
+
- Exit codes: 0 merged, already merged or no conflict in a dry run, 1 a conflict, a dirty tree or a refusal, 2 outside a repository or a usage error.
|
|
63
|
+
|
|
54
64
|
## gcl
|
|
55
65
|
|
|
56
66
|
`gcl <repo> [dir]` clones into `<workspace>/<name>` (the workspace `cli config` reports) or into `dir`, and prints the path on stdout, so `cd (gcl kaynooo/cli)` lands in it. `<repo>` is a URL, an scp-style address, a local path or `owner/name`, read as `https://git.kaynooo.fr/owner/name.git`; the `forge` key of `~/.config/kaynooo-cli/config.json` names another host.
|
package/dist/gb.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
4
|
// src/gb.ts
|
|
5
|
-
import
|
|
5
|
+
import process23 from "process";
|
|
6
6
|
// ../kit/src/args.ts
|
|
7
7
|
import process9 from "process";
|
|
8
8
|
import { parseArgs } from "util";
|
|
@@ -493,7 +493,7 @@ function printJson(value) {
|
|
|
493
493
|
|
|
494
494
|
// ../kit/src/help.ts
|
|
495
495
|
import process8 from "process";
|
|
496
|
-
var OPTIONAL =
|
|
496
|
+
var OPTIONAL = /(?<!\x1B)\[[^\]]+\]/g;
|
|
497
497
|
var USAGE_PREFIX = /^usage:\s*/i;
|
|
498
498
|
var FULL_STOP = /\.$/;
|
|
499
499
|
function usageLine(usage, colors = paint(process8.stdout)) {
|
|
@@ -532,29 +532,41 @@ function exampleRows(examples, colors = paint(process8.stdout)) {
|
|
|
532
532
|
return [];
|
|
533
533
|
return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
|
|
534
534
|
}
|
|
535
|
+
var MAX_INLINE_CHOICES = 24;
|
|
535
536
|
function optionRows(options, colors = paint(process8.stdout)) {
|
|
536
537
|
const rows = Object.entries(options).map(([name, option]) => {
|
|
537
538
|
const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
|
|
538
|
-
const
|
|
539
|
+
const choices = option.choices?.join("|");
|
|
540
|
+
const inline = choices !== undefined && choices.length <= MAX_INLINE_CHOICES;
|
|
541
|
+
const named = inline ? `<${choices}>` : "<value>";
|
|
539
542
|
const placeholder = option.optional === undefined ? named : `[${named}]`;
|
|
540
543
|
const value = option.type === "string" ? ` ${colors.dim(placeholder)}` : "";
|
|
541
|
-
|
|
544
|
+
const described = choices === undefined || inline ? option.description ?? "" : [option.description, `one of ${option.choices.join(", ")}`].filter(Boolean).join("; ");
|
|
545
|
+
return { flag: `${short}${colors.cyan(`--${name}`)}${value}`, width: `-x, --${name}`.length + (option.type === "string" ? placeholder.length + 1 : 0), description: described };
|
|
542
546
|
});
|
|
543
547
|
const width = Math.max(0, ...rows.map((row) => row.width));
|
|
544
548
|
return rows.map((row) => row.description === "" ? ` ${row.flag}` : ` ${row.flag}${" ".repeat(width - row.width)} ${colors.dim(row.description)}`);
|
|
545
549
|
}
|
|
546
550
|
|
|
547
551
|
// ../kit/src/text.ts
|
|
548
|
-
var ANSI =
|
|
552
|
+
var ANSI = /\x1B(?:\[[\x30-\x3F]*[\x20-\x2F]*[\x40-\x7E]|\][^\x07\x1B]*(?:\x07|\x1B\\)|[\x40-\x5A\x5C-\x5F])/g;
|
|
549
553
|
function distance(a, b) {
|
|
550
|
-
|
|
551
|
-
|
|
554
|
+
const left = [...a];
|
|
555
|
+
const right = [...b];
|
|
556
|
+
let before = [];
|
|
557
|
+
let previous = Array.from({ length: right.length + 1 }, (_, index) => index);
|
|
558
|
+
for (const [i, x] of left.entries()) {
|
|
552
559
|
const current = [i + 1];
|
|
553
|
-
for (const [j,
|
|
554
|
-
|
|
560
|
+
for (const [j, y] of right.entries()) {
|
|
561
|
+
let cost = Math.min((previous[j + 1] ?? 0) + 1, (current[j] ?? 0) + 1, (previous[j] ?? 0) + (x === y ? 0 : 1));
|
|
562
|
+
if (i > 0 && j > 0 && x === right[j - 1] && left[i - 1] === y)
|
|
563
|
+
cost = Math.min(cost, (before[j - 1] ?? 0) + 1);
|
|
564
|
+
current.push(cost);
|
|
565
|
+
}
|
|
566
|
+
before = previous;
|
|
555
567
|
previous = current;
|
|
556
568
|
}
|
|
557
|
-
return previous[
|
|
569
|
+
return previous[right.length] ?? 0;
|
|
558
570
|
}
|
|
559
571
|
function closest(name, names) {
|
|
560
572
|
const started = names.find((known) => known.startsWith(name));
|
|
@@ -1138,13 +1150,31 @@ function interceptOutput(clear) {
|
|
|
1138
1150
|
// ../kit/src/cli.ts
|
|
1139
1151
|
import { readFileSync as readFileSync4, realpathSync as realpathSync2 } from "fs";
|
|
1140
1152
|
import { dirname as dirname3, join as join5 } from "path";
|
|
1141
|
-
import
|
|
1153
|
+
import process15 from "process";
|
|
1154
|
+
|
|
1155
|
+
// ../kit/src/notify.ts
|
|
1156
|
+
import process13 from "process";
|
|
1157
|
+
var NOTIFY_AFTER_MS = 1e4;
|
|
1158
|
+
var CANCELLED = 130;
|
|
1159
|
+
function base64(text) {
|
|
1160
|
+
return Buffer.from(text).toString("base64");
|
|
1161
|
+
}
|
|
1162
|
+
function notification(title, body, env = process13.env) {
|
|
1163
|
+
const id = `kit${process13.pid}`;
|
|
1164
|
+
const codes = [`\x1B]99;i=${id}:d=0:o=unfocused:e=1;${base64(title)}\x1B\\`, `\x1B]99;i=${id}:p=body:e=1;${base64(body)}\x1B\\`];
|
|
1165
|
+
return codes.map((code) => env.TMUX === undefined || env.TMUX === "" ? code : `\x1BPtmux;${code.replaceAll("\x1B", "\x1B\x1B")}\x1B\\`).join("");
|
|
1166
|
+
}
|
|
1167
|
+
function notifyEnd(command, code, ms, { stream = process13.stderr, env = process13.env } = {}) {
|
|
1168
|
+
if (ms < NOTIFY_AFTER_MS || code === CANCELLED || outputMode(stream, env) !== "terminal")
|
|
1169
|
+
return;
|
|
1170
|
+
stream.write(notification(command, code === 0 ? `done in ${formatDuration(ms)}` : `failed with exit ${code} after ${formatDuration(ms)}`, env));
|
|
1171
|
+
}
|
|
1142
1172
|
|
|
1143
1173
|
// ../kit/src/usage.ts
|
|
1144
1174
|
import { appendFileSync, mkdirSync } from "fs";
|
|
1145
1175
|
import { homedir as homedir3 } from "os";
|
|
1146
1176
|
import { dirname as dirname2, isAbsolute, join as join4 } from "path";
|
|
1147
|
-
import
|
|
1177
|
+
import process14 from "process";
|
|
1148
1178
|
|
|
1149
1179
|
// ../kit/src/logs.ts
|
|
1150
1180
|
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
@@ -1158,14 +1188,14 @@ var usageError;
|
|
|
1158
1188
|
function noteUsageError(message) {
|
|
1159
1189
|
usageError = message;
|
|
1160
1190
|
}
|
|
1161
|
-
function stateHome(env =
|
|
1191
|
+
function stateHome(env = process14.env) {
|
|
1162
1192
|
const dir = env.XDG_STATE_HOME;
|
|
1163
1193
|
return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
|
|
1164
1194
|
}
|
|
1165
|
-
function usagePath(env =
|
|
1195
|
+
function usagePath(env = process14.env) {
|
|
1166
1196
|
return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
1167
1197
|
}
|
|
1168
|
-
function usageRecorded(env =
|
|
1198
|
+
function usageRecorded(env = process14.env) {
|
|
1169
1199
|
const off = env.KAYNOOO_CLI_NO_USAGE;
|
|
1170
1200
|
return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
|
|
1171
1201
|
}
|
|
@@ -1179,7 +1209,7 @@ function usageEntry(bin, command, argv, code, now = new Date) {
|
|
|
1179
1209
|
usageError = undefined;
|
|
1180
1210
|
return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
|
|
1181
1211
|
}
|
|
1182
|
-
function recordUsage(entry, env =
|
|
1212
|
+
function recordUsage(entry, env = process14.env) {
|
|
1183
1213
|
if (!usageRecorded(env))
|
|
1184
1214
|
return;
|
|
1185
1215
|
try {
|
|
@@ -1197,8 +1227,8 @@ var LEVEL_OPTIONS = {
|
|
|
1197
1227
|
};
|
|
1198
1228
|
var TOP_OPTIONS = { ...LEVEL_OPTIONS, version: { type: "boolean", short: "v", description: "print the version" } };
|
|
1199
1229
|
function printError(prefix, message, hint, cause) {
|
|
1200
|
-
const colors = paint(
|
|
1201
|
-
const styled = takesControlCodes(
|
|
1230
|
+
const colors = paint(process15.stderr);
|
|
1231
|
+
const styled = takesControlCodes(process15.stderr);
|
|
1202
1232
|
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
1203
1233
|
const under = (label, text, tint) => text === undefined ? "" : `
|
|
1204
1234
|
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
@@ -1306,11 +1336,13 @@ async function runMain({ name, version, run, redact, usage }, argv = scriptArgs(
|
|
|
1306
1336
|
return 0;
|
|
1307
1337
|
}
|
|
1308
1338
|
let code;
|
|
1339
|
+
const started = performance.now();
|
|
1309
1340
|
try {
|
|
1310
1341
|
code = await run(argv);
|
|
1311
1342
|
} catch (error) {
|
|
1312
1343
|
code = reportThrown(name, error, redact, wantsJson(argv));
|
|
1313
1344
|
}
|
|
1345
|
+
notifyEnd(name, code, performance.now() - started);
|
|
1314
1346
|
if (usage !== false) {
|
|
1315
1347
|
try {
|
|
1316
1348
|
recordUsage(usageEntry(name, undefined, argv, code));
|
|
@@ -1327,7 +1359,7 @@ var {Glob } = globalThis.Bun;
|
|
|
1327
1359
|
var SESSION_FILES = new Glob("*.json");
|
|
1328
1360
|
var ACCOUNT_DIRS = new Glob("*/sessions");
|
|
1329
1361
|
// ../kit/src/watch.ts
|
|
1330
|
-
import
|
|
1362
|
+
import process16 from "process";
|
|
1331
1363
|
import { format } from "util";
|
|
1332
1364
|
var CLEAR_SCREEN = "\x1B[H\x1B[2J";
|
|
1333
1365
|
var DEFAULT_SECONDS = 2;
|
|
@@ -1349,7 +1381,7 @@ function withoutWatch(args) {
|
|
|
1349
1381
|
}
|
|
1350
1382
|
return rest;
|
|
1351
1383
|
}
|
|
1352
|
-
async function watching(value, step, stream =
|
|
1384
|
+
async function watching(value, step, stream = process16.stdout, sleep = Bun.sleep, rounds = Number.POSITIVE_INFINITY) {
|
|
1353
1385
|
const interval = value === "" ? DEFAULT_SECONDS * 1000 : duration(value, "watch", DEFAULT_SECONDS * 1000);
|
|
1354
1386
|
if (!takesControlCodes(stream))
|
|
1355
1387
|
badUsage("--watch redraws the screen, so it needs a terminal");
|
|
@@ -1357,12 +1389,12 @@ async function watching(value, step, stream = process15.stdout, sleep = Bun.slee
|
|
|
1357
1389
|
for (let round = 0;round < rounds; round++) {
|
|
1358
1390
|
const held = [];
|
|
1359
1391
|
const { log, info } = console;
|
|
1360
|
-
const write =
|
|
1392
|
+
const write = process16.stdout.write.bind(process16.stdout);
|
|
1361
1393
|
const hold = (...parts) => void held.push(`${format(...parts)}
|
|
1362
1394
|
`);
|
|
1363
1395
|
console.log = hold;
|
|
1364
1396
|
console.info = hold;
|
|
1365
|
-
|
|
1397
|
+
process16.stdout.write = (text) => {
|
|
1366
1398
|
held.push(typeof text === "string" ? text : Buffer.from(text).toString());
|
|
1367
1399
|
return true;
|
|
1368
1400
|
};
|
|
@@ -1371,7 +1403,7 @@ async function watching(value, step, stream = process15.stdout, sleep = Bun.slee
|
|
|
1371
1403
|
} finally {
|
|
1372
1404
|
console.log = log;
|
|
1373
1405
|
console.info = info;
|
|
1374
|
-
|
|
1406
|
+
process16.stdout.write = write;
|
|
1375
1407
|
}
|
|
1376
1408
|
stream.write(`${CLEAR_SCREEN}${held.join("")}`);
|
|
1377
1409
|
await sleep(interval);
|
|
@@ -1381,7 +1413,7 @@ async function watching(value, step, stream = process15.stdout, sleep = Bun.slee
|
|
|
1381
1413
|
// ../kit/src/workspace-root.ts
|
|
1382
1414
|
import { homedir as homedir4 } from "os";
|
|
1383
1415
|
import { dirname as dirname4, join as join6, resolve as resolve4 } from "path";
|
|
1384
|
-
import
|
|
1416
|
+
import process17 from "process";
|
|
1385
1417
|
function expandHome(path, home) {
|
|
1386
1418
|
if (path === "~")
|
|
1387
1419
|
return home;
|
|
@@ -1391,13 +1423,126 @@ function configured(env, home) {
|
|
|
1391
1423
|
const folder = readUserConfig("config", { home, xdg: env.XDG_CONFIG_HOME }).workspace;
|
|
1392
1424
|
return typeof folder === "string" && folder !== "" ? folder : undefined;
|
|
1393
1425
|
}
|
|
1394
|
-
function workspaceRoot({ given, env =
|
|
1426
|
+
function workspaceRoot({ given, env = process17.env, home = homedir4() } = {}) {
|
|
1395
1427
|
const fromEnv = env.KAYNOOO_WORKSPACE === "" ? undefined : env.KAYNOOO_WORKSPACE;
|
|
1396
1428
|
return resolve4(expandHome(given ?? fromEnv ?? configured(env, home) ?? join6(home, "Workspace"), home));
|
|
1397
1429
|
}
|
|
1398
1430
|
// src/branch.ts
|
|
1399
1431
|
import { basename } from "path";
|
|
1400
|
-
import
|
|
1432
|
+
import process22 from "process";
|
|
1433
|
+
|
|
1434
|
+
// ../kit/src/picker/line.ts
|
|
1435
|
+
var EDIT_KEYS = new Set(["left", "right", "wordleft", "wordright", "linestart", "lineend", "erase", "delete", "word", "killword", "killnext", "clear", "killend", "yank"]);
|
|
1436
|
+
var BACKWARD_KILLS = new Set(["word", "killword", "clear"]);
|
|
1437
|
+
var FORWARD_KILLS = new Set(["killnext", "killend"]);
|
|
1438
|
+
function isEditKey(key) {
|
|
1439
|
+
return EDIT_KEYS.has(key);
|
|
1440
|
+
}
|
|
1441
|
+
function endsKills(key) {
|
|
1442
|
+
return typeof key !== "object" && !isEditKey(key);
|
|
1443
|
+
}
|
|
1444
|
+
var WORD_CHARACTER = /[\p{L}\p{N}_]/u;
|
|
1445
|
+
var SPACE = /\s/;
|
|
1446
|
+
function isWord(char) {
|
|
1447
|
+
return char !== undefined && WORD_CHARACTER.test(char);
|
|
1448
|
+
}
|
|
1449
|
+
function wordStart(chars, from) {
|
|
1450
|
+
let at = from;
|
|
1451
|
+
while (at > 0 && !isWord(chars[at - 1]))
|
|
1452
|
+
at--;
|
|
1453
|
+
while (at > 0 && isWord(chars[at - 1]))
|
|
1454
|
+
at--;
|
|
1455
|
+
return at;
|
|
1456
|
+
}
|
|
1457
|
+
function wordEnd(chars, from) {
|
|
1458
|
+
let at = from;
|
|
1459
|
+
while (at < chars.length && !isWord(chars[at]))
|
|
1460
|
+
at++;
|
|
1461
|
+
while (at < chars.length && isWord(chars[at]))
|
|
1462
|
+
at++;
|
|
1463
|
+
return at;
|
|
1464
|
+
}
|
|
1465
|
+
function spacedStart(chars, from) {
|
|
1466
|
+
let at = from;
|
|
1467
|
+
while (at > 0 && SPACE.test(chars[at - 1] ?? ""))
|
|
1468
|
+
at--;
|
|
1469
|
+
while (at > 0 && !SPACE.test(chars[at - 1] ?? ""))
|
|
1470
|
+
at--;
|
|
1471
|
+
return at;
|
|
1472
|
+
}
|
|
1473
|
+
function cut(chars, from, to) {
|
|
1474
|
+
return { text: [...chars.slice(0, from), ...chars.slice(to)].join(""), caret: from };
|
|
1475
|
+
}
|
|
1476
|
+
function insert(chars, at, text) {
|
|
1477
|
+
const typed = [...text];
|
|
1478
|
+
return { text: [...chars.slice(0, at), ...typed, ...chars.slice(at)].join(""), caret: at + typed.length };
|
|
1479
|
+
}
|
|
1480
|
+
function editLine(line, key) {
|
|
1481
|
+
const { killed = "", chained = false } = line;
|
|
1482
|
+
const chars = [...line.text];
|
|
1483
|
+
const at = Math.min(Math.max(line.caret, 0), chars.length);
|
|
1484
|
+
const edited = edit(chars, at, key, killed);
|
|
1485
|
+
if (typeof key === "object" || !BACKWARD_KILLS.has(key) && !FORWARD_KILLS.has(key))
|
|
1486
|
+
return line.killed === undefined ? edited : { ...edited, killed };
|
|
1487
|
+
const taken = chars.slice(edited.caret, edited.caret + chars.length - [...edited.text].length).join("");
|
|
1488
|
+
if (taken === "")
|
|
1489
|
+
return { ...edited, killed, chained };
|
|
1490
|
+
if (!chained)
|
|
1491
|
+
return { ...edited, killed: taken, chained: true };
|
|
1492
|
+
return { ...edited, killed: BACKWARD_KILLS.has(key) ? taken + killed : killed + taken, chained: true };
|
|
1493
|
+
}
|
|
1494
|
+
function edit(chars, at, key, killed) {
|
|
1495
|
+
const text = chars.join("");
|
|
1496
|
+
if (typeof key === "object")
|
|
1497
|
+
return insert(chars, at, key.text);
|
|
1498
|
+
switch (key) {
|
|
1499
|
+
case "left":
|
|
1500
|
+
return { text, caret: Math.max(at - 1, 0) };
|
|
1501
|
+
case "right":
|
|
1502
|
+
return { text, caret: Math.min(at + 1, chars.length) };
|
|
1503
|
+
case "wordleft":
|
|
1504
|
+
return { text, caret: wordStart(chars, at) };
|
|
1505
|
+
case "wordright":
|
|
1506
|
+
return { text, caret: wordEnd(chars, at) };
|
|
1507
|
+
case "linestart":
|
|
1508
|
+
return { text, caret: 0 };
|
|
1509
|
+
case "lineend":
|
|
1510
|
+
return { text, caret: chars.length };
|
|
1511
|
+
case "erase":
|
|
1512
|
+
return cut(chars, Math.max(at - 1, 0), at);
|
|
1513
|
+
case "delete":
|
|
1514
|
+
return cut(chars, at, Math.min(at + 1, chars.length));
|
|
1515
|
+
case "word":
|
|
1516
|
+
return cut(chars, spacedStart(chars, at), at);
|
|
1517
|
+
case "killword":
|
|
1518
|
+
return cut(chars, wordStart(chars, at), at);
|
|
1519
|
+
case "killnext":
|
|
1520
|
+
return cut(chars, at, wordEnd(chars, at));
|
|
1521
|
+
case "clear":
|
|
1522
|
+
return cut(chars, 0, at);
|
|
1523
|
+
case "killend":
|
|
1524
|
+
return cut(chars, at, chars.length);
|
|
1525
|
+
case "yank":
|
|
1526
|
+
return insert(chars, at, killed);
|
|
1527
|
+
}
|
|
1528
|
+
}
|
|
1529
|
+
function visible({ text, caret }, room) {
|
|
1530
|
+
const chars = [...text];
|
|
1531
|
+
const at = Math.min(Math.max(caret, 0), chars.length);
|
|
1532
|
+
const cells = at === chars.length ? [...chars, " "] : chars;
|
|
1533
|
+
const widths = cells.map((char) => Bun.stringWidth(char));
|
|
1534
|
+
const width = (from, to) => widths.slice(from, to).reduce((sum, size) => sum + size, 0);
|
|
1535
|
+
const fits = (from, to) => (from > 0 ? 1 : 0) + width(from, to) + (to < cells.length ? 1 : 0) <= room;
|
|
1536
|
+
let start = at;
|
|
1537
|
+
let end = at + 1;
|
|
1538
|
+
while (start > 0 && fits(start - 1, end))
|
|
1539
|
+
start--;
|
|
1540
|
+
while (end < cells.length && fits(start, end + 1))
|
|
1541
|
+
end++;
|
|
1542
|
+
const left = start > 0 ? "\u2026" : "";
|
|
1543
|
+
const right = end < cells.length ? "\u2026" : "";
|
|
1544
|
+
return { before: left + chars.slice(start, at).join(""), at: chars[at], after: chars.slice(at + 1, end).join("") + right, column: left.length + width(start, at) };
|
|
1545
|
+
}
|
|
1401
1546
|
|
|
1402
1547
|
// ../kit/src/picker/score.ts
|
|
1403
1548
|
var MATCH = 16;
|
|
@@ -1483,7 +1628,6 @@ function rank(query, lines) {
|
|
|
1483
1628
|
}
|
|
1484
1629
|
|
|
1485
1630
|
// ../kit/src/picker/state.ts
|
|
1486
|
-
var ANSI2 = /\x1B(?:\[[\x30-\x3F]*[\x20-\x2F]*[\x40-\x7E]|\][^\x07\x1B]*(?:\x07|\x1B\\)|[\x40-\x5A\x5C-\x5F])/g;
|
|
1487
1631
|
var CONTROL2 = /[\x00-\x1F\x7F-\x9F]/g;
|
|
1488
1632
|
var IS_CONTROL = /[\x00-\x1F\x7F-\x9F]/;
|
|
1489
1633
|
var SGR = /^\x1B\[[0-9;]*m$/;
|
|
@@ -1491,7 +1635,6 @@ var RESET = /^\x1B\[0?m$/;
|
|
|
1491
1635
|
var COLUMN_GAP = 2;
|
|
1492
1636
|
var PASTE_START = "\x1B[200~";
|
|
1493
1637
|
var PASTE_END = "\x1B[201~";
|
|
1494
|
-
var LAST_WORD = /\S*\s*$/;
|
|
1495
1638
|
var KEY_SEQUENCES = /\x1B\[<[0-9;]*[A-Za-z]|\x1B\[[0-9;]*[A-Z~]|\x1BO[A-Z]|\x1B[\s\S]|[\s\S]/gu;
|
|
1496
1639
|
var MOUSE = /^\x1B\[<(\d+);\d+;\d+[a-z]$/i;
|
|
1497
1640
|
var WHEEL_LINES = 3;
|
|
@@ -1514,10 +1657,29 @@ var KEYS = {
|
|
|
1514
1657
|
"\n": "enter",
|
|
1515
1658
|
"\x03": "cancel",
|
|
1516
1659
|
"\x1B": "cancel",
|
|
1660
|
+
"\x1B[D": "left",
|
|
1661
|
+
"\x1BOD": "left",
|
|
1662
|
+
"\x1B[C": "right",
|
|
1663
|
+
"\x1BOC": "right",
|
|
1664
|
+
"\x1B[1;5D": "wordleft",
|
|
1665
|
+
"\x1B[1;3D": "wordleft",
|
|
1666
|
+
"\x1Bb": "wordleft",
|
|
1667
|
+
"\x1B[1;5C": "wordright",
|
|
1668
|
+
"\x1B[1;3C": "wordright",
|
|
1669
|
+
"\x1Bf": "wordright",
|
|
1670
|
+
"\x01": "linestart",
|
|
1671
|
+
"\x05": "lineend",
|
|
1517
1672
|
"\x7F": "erase",
|
|
1518
|
-
"\
|
|
1673
|
+
"\x1B[3~": "delete",
|
|
1674
|
+
"\b": "killword",
|
|
1675
|
+
"\x1B\x7F": "killword",
|
|
1676
|
+
"\x1B[3;5~": "killnext",
|
|
1677
|
+
"\x1Bd": "killnext",
|
|
1519
1678
|
"\x15": "clear",
|
|
1679
|
+
"\v": "killend",
|
|
1520
1680
|
"\x17": "word",
|
|
1681
|
+
"\x19": "yank",
|
|
1682
|
+
"\x1A": "suspend",
|
|
1521
1683
|
"\t": "tab",
|
|
1522
1684
|
"\x1F": "help",
|
|
1523
1685
|
"\x1BOP": "help",
|
|
@@ -1539,7 +1701,7 @@ function styled(line) {
|
|
|
1539
1701
|
colored ||= active !== "" && flat !== "";
|
|
1540
1702
|
styles.push(...repeat(active, flat.length));
|
|
1541
1703
|
}
|
|
1542
|
-
for (const found of line.matchAll(
|
|
1704
|
+
for (const found of line.matchAll(ANSI)) {
|
|
1543
1705
|
add(line.slice(last, found.index));
|
|
1544
1706
|
last = found.index + found[0].length;
|
|
1545
1707
|
if (SGR.test(found[0]))
|
|
@@ -1551,7 +1713,7 @@ function styled(line) {
|
|
|
1551
1713
|
function lay(rows, searched) {
|
|
1552
1714
|
const parsed = rows.map((row) => row.cells.map(styled));
|
|
1553
1715
|
const widths = (parsed[0] ?? []).map((_, column) => Math.max(...parsed.map((cells) => Bun.stringWidth(cells[column]?.text ?? ""))));
|
|
1554
|
-
return parsed.map((cells) => {
|
|
1716
|
+
return parsed.map((cells, at) => {
|
|
1555
1717
|
let text = "";
|
|
1556
1718
|
const styles = [];
|
|
1557
1719
|
let colored = false;
|
|
@@ -1570,7 +1732,7 @@ function lay(rows, searched) {
|
|
|
1570
1732
|
styles.push(...repeat("", pad));
|
|
1571
1733
|
}
|
|
1572
1734
|
}
|
|
1573
|
-
return { text, styles: colored ? styles : undefined, searchable: text.slice(0, searchEnd) };
|
|
1735
|
+
return { text, styles: colored ? styles : undefined, searchable: rows[at].search ?? text.slice(0, searchEnd) };
|
|
1574
1736
|
});
|
|
1575
1737
|
}
|
|
1576
1738
|
function parseKeys(input, paste = { active: false }, ctrls = []) {
|
|
@@ -1594,7 +1756,7 @@ function parseKeys(input, paste = { active: false }, ctrls = []) {
|
|
|
1594
1756
|
});
|
|
1595
1757
|
}
|
|
1596
1758
|
function createState(rows, { multi = false, prompt = "> ", query = "", header = "", expect = [], searched = Number.POSITIVE_INFINITY } = {}) {
|
|
1597
|
-
const state = { rows: [], items: [], styles: [], searchable: [], searched, header, expect, key: "", status: "", query, matches: [], cursor: 0, help: false, page: 10, marked: new Set, multi, prompt, done: undefined };
|
|
1759
|
+
const state = { rows: [], items: [], styles: [], searchable: [], searched, header, expect, key: "", status: "", query, caret: [...query].length, killed: "", chained: false, matches: [], cursor: 0, help: false, page: 10, marked: new Set, multi, prompt, done: undefined };
|
|
1598
1760
|
return withRows(state, rows);
|
|
1599
1761
|
}
|
|
1600
1762
|
function withRows(state, rows, header = state.header) {
|
|
@@ -1604,8 +1766,9 @@ function withRows(state, rows, header = state.header) {
|
|
|
1604
1766
|
const matches = rank(state.query, searchable);
|
|
1605
1767
|
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 };
|
|
1606
1768
|
}
|
|
1607
|
-
function withQuery(state,
|
|
1608
|
-
|
|
1769
|
+
function withQuery(state, key) {
|
|
1770
|
+
const { text, caret, killed = "", chained = false } = editLine({ text: state.query, caret: state.caret, killed: state.killed, chained: state.chained }, key);
|
|
1771
|
+
return text === state.query ? { ...state, caret, killed, chained } : { ...state, query: text, caret, killed, chained, matches: rank(text, state.searchable), cursor: 0 };
|
|
1609
1772
|
}
|
|
1610
1773
|
function move(state, by) {
|
|
1611
1774
|
return { ...state, cursor: Math.max(0, Math.min(state.matches.length - 1, state.cursor + by)) };
|
|
@@ -1616,13 +1779,17 @@ function highlighted(state) {
|
|
|
1616
1779
|
function press(state, key) {
|
|
1617
1780
|
if (state.done !== undefined)
|
|
1618
1781
|
return state;
|
|
1782
|
+
if (state.chained && endsKills(key))
|
|
1783
|
+
state = { ...state, chained: false };
|
|
1619
1784
|
if (state.help)
|
|
1620
1785
|
return { ...state, help: false };
|
|
1621
1786
|
if (typeof key === "object") {
|
|
1622
1787
|
if ("text" in key)
|
|
1623
|
-
return withQuery(state,
|
|
1788
|
+
return withQuery(state, key);
|
|
1624
1789
|
return state.expect.includes(key.ctrl) ? { ...state, done: "accept", key: key.ctrl } : state;
|
|
1625
1790
|
}
|
|
1791
|
+
if (isEditKey(key))
|
|
1792
|
+
return withQuery(state, key);
|
|
1626
1793
|
switch (key) {
|
|
1627
1794
|
case "up":
|
|
1628
1795
|
return move(state, -1);
|
|
@@ -1640,12 +1807,8 @@ function press(state, key) {
|
|
|
1640
1807
|
return { ...state, done: "cancel" };
|
|
1641
1808
|
case "help":
|
|
1642
1809
|
return { ...state, help: true };
|
|
1643
|
-
case "
|
|
1644
|
-
return
|
|
1645
|
-
case "clear":
|
|
1646
|
-
return withQuery(state, "");
|
|
1647
|
-
case "word":
|
|
1648
|
-
return withQuery(state, state.query.replace(LAST_WORD, ""));
|
|
1810
|
+
case "suspend":
|
|
1811
|
+
return state;
|
|
1649
1812
|
case "enter":
|
|
1650
1813
|
return state.matches.length === 0 && state.marked.size === 0 ? state : { ...state, done: "accept" };
|
|
1651
1814
|
case "tab": {
|
|
@@ -1668,11 +1831,25 @@ function chosen(state) {
|
|
|
1668
1831
|
|
|
1669
1832
|
// ../kit/src/picker/terminal.ts
|
|
1670
1833
|
import { openSync } from "fs";
|
|
1671
|
-
import
|
|
1834
|
+
import process21 from "process";
|
|
1672
1835
|
import tty from "tty";
|
|
1673
1836
|
|
|
1837
|
+
// ../kit/src/job.ts
|
|
1838
|
+
import process19 from "process";
|
|
1839
|
+
function stopJob() {
|
|
1840
|
+
process19.kill(0, "SIGTSTP");
|
|
1841
|
+
}
|
|
1842
|
+
|
|
1843
|
+
// ../kit/src/redraw.ts
|
|
1844
|
+
var SYNC_START = "\x1B[?2026h";
|
|
1845
|
+
var SYNC_END = "\x1B[?2026l";
|
|
1846
|
+
function overwrite(lines, columns) {
|
|
1847
|
+
return lines.map((line) => textWidth(line) % columns === 0 ? `\x1B[2K${line}` : `${line}\x1B[K`).join(`
|
|
1848
|
+
`);
|
|
1849
|
+
}
|
|
1850
|
+
|
|
1674
1851
|
// ../kit/src/picker/preview.ts
|
|
1675
|
-
import
|
|
1852
|
+
import process20 from "process";
|
|
1676
1853
|
|
|
1677
1854
|
// ../kit/src/shell.ts
|
|
1678
1855
|
var SEPARATORS = new Set([`
|
|
@@ -1741,7 +1918,7 @@ async function runPreview(template, line, signal, hidden = []) {
|
|
|
1741
1918
|
${previewCommand(template, line, hidden)}`], { stdin: "ignore", stdout: "pipe", stderr: "ignore", detached: true });
|
|
1742
1919
|
const stop = () => {
|
|
1743
1920
|
try {
|
|
1744
|
-
|
|
1921
|
+
process20.kill(-proc.pid, "SIGKILL");
|
|
1745
1922
|
} catch {
|
|
1746
1923
|
proc.kill();
|
|
1747
1924
|
}
|
|
@@ -1816,13 +1993,20 @@ function fitStyled(line, width, plain = identity) {
|
|
|
1816
1993
|
const { text, styles } = styled(line);
|
|
1817
1994
|
return fit(text, width, plain, plain, new Set, styles);
|
|
1818
1995
|
}
|
|
1819
|
-
function
|
|
1820
|
-
return
|
|
1996
|
+
function queryShown(state, layout) {
|
|
1997
|
+
return visible({ text: state.query, caret: state.caret }, Math.max(1, layout.listWidth - Bun.stringWidth(state.prompt)));
|
|
1998
|
+
}
|
|
1999
|
+
function caretCell(state, layout) {
|
|
2000
|
+
const shown = queryShown(state, layout);
|
|
2001
|
+
return { column: Math.min(layout.listWidth, Bun.stringWidth(state.prompt) + shown.column + 1), char: shown.at ?? " " };
|
|
1821
2002
|
}
|
|
1822
2003
|
function helpLines(state, layout, theme) {
|
|
1823
2004
|
const lines = [
|
|
1824
2005
|
"type to filter, up, down, ctrl-p, ctrl-n, pgup, pgdn, home, end, the wheel move",
|
|
1825
|
-
|
|
2006
|
+
"left, right, ctrl-a, ctrl-e move in the query, ctrl or alt with left or right by a word",
|
|
2007
|
+
"ctrl-w or ctrl-backspace erase a word, ctrl-u back to the start, ctrl-k to the end, del ahead",
|
|
2008
|
+
"ctrl-y puts back what they erased, ctrl-z suspends to the shell",
|
|
2009
|
+
`enter picks${state.multi ? ", tab marks" : ""}, esc or ctrl-c cancels`,
|
|
1826
2010
|
...state.expect.length > 0 ? [`${state.expect.join(", ")} end the pick as this command asks`] : []
|
|
1827
2011
|
];
|
|
1828
2012
|
const title = "keys, any key closes this help";
|
|
@@ -1838,7 +2022,9 @@ function listLines(state, layout, theme) {
|
|
|
1838
2022
|
const top = Math.max(0, Math.min(state.cursor - Math.floor(rows / 2), state.matches.length - rows));
|
|
1839
2023
|
const marks = (state.marked.size > 0 ? ` (${state.marked.size} marked)` : "") + (state.status === "" ? "" : ` ${state.status}`);
|
|
1840
2024
|
const promptWidth = Bun.stringWidth(state.prompt);
|
|
1841
|
-
const
|
|
2025
|
+
const shown = queryShown(state, layout);
|
|
2026
|
+
const text = `${shown.before}${shown.at ?? ""}${shown.after}`;
|
|
2027
|
+
const query = { text, width: Bun.stringWidth(text) };
|
|
1842
2028
|
const place = state.matches.length > rows ? ` (${state.cursor + 1} of ${state.matches.length})` : "";
|
|
1843
2029
|
const count = fit(` ${state.matches.length}/${state.items.length}${place}${marks} ctrl-/ keys`, layout.listWidth, theme.label);
|
|
1844
2030
|
const lines = [{ text: theme.accent(state.prompt) + query.text, width: promptWidth + query.width }, count, ...header];
|
|
@@ -1871,7 +2057,8 @@ var ESCAPE_WAIT_MS = 30;
|
|
|
1871
2057
|
var OPEN = { ctrl: "open" };
|
|
1872
2058
|
var HIDE_CURSOR = "\x1B[?25l";
|
|
1873
2059
|
var SHOW_CURSOR = "\x1B[?25h";
|
|
1874
|
-
var
|
|
2060
|
+
var REVERSE = "\x1B[7m";
|
|
2061
|
+
var REVERSE_END = "\x1B[27m";
|
|
1875
2062
|
var PASTE_ON = "\x1B[?2004h";
|
|
1876
2063
|
var PASTE_OFF = "\x1B[?2004l";
|
|
1877
2064
|
var MOUSE_ON = "\x1B[?1000h\x1B[?1006h";
|
|
@@ -1889,7 +2076,7 @@ function openTerminal() {
|
|
|
1889
2076
|
return fail("no terminal to read keys from (/dev/tty)");
|
|
1890
2077
|
}
|
|
1891
2078
|
const keys = Bun.file("/dev/tty").stream().getReader();
|
|
1892
|
-
const out =
|
|
2079
|
+
const out = process21.stderr;
|
|
1893
2080
|
let resize;
|
|
1894
2081
|
async function read(onChunk) {
|
|
1895
2082
|
const decoder = new TextDecoder;
|
|
@@ -1915,6 +2102,11 @@ function openTerminal() {
|
|
|
1915
2102
|
out.on("resize", onResize);
|
|
1916
2103
|
read(onChunk);
|
|
1917
2104
|
},
|
|
2105
|
+
suspend() {
|
|
2106
|
+
mode.setRawMode(false);
|
|
2107
|
+
stopJob();
|
|
2108
|
+
mode.setRawMode(true);
|
|
2109
|
+
},
|
|
1918
2110
|
close() {
|
|
1919
2111
|
if (resize !== undefined)
|
|
1920
2112
|
out.off("resize", resize);
|
|
@@ -1942,14 +2134,14 @@ async function select(rows, settings, terminal = openTerminal()) {
|
|
|
1942
2134
|
if (restored)
|
|
1943
2135
|
return;
|
|
1944
2136
|
const layout = layoutFor({ columns: terminal.columns, rows: terminal.rows }, settings.height, settings.preview !== undefined, settings.previewPosition);
|
|
1945
|
-
let frame = HIDE_CURSOR;
|
|
2137
|
+
let frame = SYNC_START + HIDE_CURSOR;
|
|
1946
2138
|
if (reserved !== layout.height || clear) {
|
|
1947
2139
|
frame += CLEAR_BELOW2 + `
|
|
1948
2140
|
`.repeat(layout.height - 1) + up(layout.height - 1);
|
|
1949
2141
|
reserved = layout.height;
|
|
1950
2142
|
}
|
|
1951
|
-
|
|
1952
|
-
|
|
2143
|
+
const caret = caretCell(state, layout);
|
|
2144
|
+
frame += `\r${overwrite(render(state, layout, theme, preview), terminal.columns)}${up(layout.height - 1)}\r\x1B[${caret.column}G${REVERSE}${caret.char}${REVERSE_END}\r${SYNC_END}`;
|
|
1953
2145
|
terminal.write(frame);
|
|
1954
2146
|
}
|
|
1955
2147
|
function pageSize() {
|
|
@@ -1991,6 +2183,14 @@ async function select(rows, settings, terminal = openTerminal()) {
|
|
|
1991
2183
|
terminal.write(`${CLEAR_BELOW2}${MOUSE_OFF}${PASTE_OFF}${SHOW_CURSOR}`);
|
|
1992
2184
|
terminal.close();
|
|
1993
2185
|
}
|
|
2186
|
+
function suspend() {
|
|
2187
|
+
if (terminal.suspend === undefined)
|
|
2188
|
+
return;
|
|
2189
|
+
terminal.write(`${CLEAR_BELOW2}${MOUSE_OFF}${PASTE_OFF}${SHOW_CURSOR}`);
|
|
2190
|
+
terminal.suspend();
|
|
2191
|
+
terminal.write(`${PASTE_ON}${MOUSE_ON}`);
|
|
2192
|
+
draw(true);
|
|
2193
|
+
}
|
|
1994
2194
|
const stop = () => {
|
|
1995
2195
|
state = { ...state, done: "cancel" };
|
|
1996
2196
|
finish?.();
|
|
@@ -2024,6 +2224,8 @@ async function select(rows, settings, terminal = openTerminal()) {
|
|
|
2024
2224
|
for (let key = pending.shift();key !== undefined && state.done === undefined; key = pending.shift()) {
|
|
2025
2225
|
if (key === OPEN || typeof key === "object" && "ctrl" in key && key.ctrl === reload?.key)
|
|
2026
2226
|
await reloadRows();
|
|
2227
|
+
else if (key === "suspend")
|
|
2228
|
+
suspend();
|
|
2027
2229
|
else
|
|
2028
2230
|
state = press({ ...state, page: pageSize() }, key);
|
|
2029
2231
|
}
|
|
@@ -2063,9 +2265,9 @@ async function select(rows, settings, terminal = openTerminal()) {
|
|
|
2063
2265
|
const ended = new Promise((resolve) => {
|
|
2064
2266
|
finish = resolve;
|
|
2065
2267
|
});
|
|
2066
|
-
|
|
2268
|
+
process21.once("exit", restore);
|
|
2067
2269
|
for (const signal of SIGNALS2)
|
|
2068
|
-
|
|
2270
|
+
process21.once(signal, stop);
|
|
2069
2271
|
try {
|
|
2070
2272
|
terminal.write(`${PASTE_ON}${MOUSE_ON}`);
|
|
2071
2273
|
terminal.listen(onChunk, () => draw(true));
|
|
@@ -2078,8 +2280,8 @@ async function select(rows, settings, terminal = openTerminal()) {
|
|
|
2078
2280
|
await ended;
|
|
2079
2281
|
} finally {
|
|
2080
2282
|
for (const signal of SIGNALS2)
|
|
2081
|
-
|
|
2082
|
-
|
|
2283
|
+
process21.off(signal, stop);
|
|
2284
|
+
process21.off("exit", restore);
|
|
2083
2285
|
restore();
|
|
2084
2286
|
}
|
|
2085
2287
|
return state.done === "accept" ? { rows: chosen(state), key: state.key, query: state.query } : undefined;
|
|
@@ -2350,7 +2552,7 @@ async function gb(args) {
|
|
|
2350
2552
|
if (parsed.values.json === true)
|
|
2351
2553
|
printJson(all);
|
|
2352
2554
|
else
|
|
2353
|
-
console.log(table2(all.map(row)).map((line) => takesControlCodes(
|
|
2555
|
+
console.log(table2(all.map(row)).map((line) => takesControlCodes(process22.stdout) ? line : Bun.stripANSI(line)).join(`
|
|
2354
2556
|
`));
|
|
2355
2557
|
return 0;
|
|
2356
2558
|
}
|
|
@@ -2407,4 +2609,4 @@ async function finish(command, typed, emitting) {
|
|
|
2407
2609
|
}
|
|
2408
2610
|
|
|
2409
2611
|
// src/gb.ts
|
|
2410
|
-
|
|
2612
|
+
process23.exitCode = await runMain({ name: "gb", run: gb });
|