@kaynooo/cli-pick 0.9.1 → 0.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/pick.js +81 -48
- package/package.json +2 -2
package/dist/pick.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
4
|
// src/pick.ts
|
|
5
|
-
import
|
|
5
|
+
import process16 from "process";
|
|
6
6
|
// ../kit/src/args.ts
|
|
7
7
|
import process9 from "process";
|
|
8
8
|
import { parseArgs } from "util";
|
|
@@ -326,6 +326,9 @@ function upTo(text, separator) {
|
|
|
326
326
|
return end === -1 ? text : text.slice(0, end);
|
|
327
327
|
}
|
|
328
328
|
var ESCAPE = String.fromCharCode(27);
|
|
329
|
+
function textWidth(text) {
|
|
330
|
+
return Bun.stringWidth(text);
|
|
331
|
+
}
|
|
329
332
|
function jsonText(value, replacer, { stream = process7.stdout, env = process7.env } = {}) {
|
|
330
333
|
return JSON.stringify(value, replacer, outputMode(stream, env) === "agent" ? undefined : 2);
|
|
331
334
|
}
|
|
@@ -341,18 +344,14 @@ var FULL_STOP = /\.$/;
|
|
|
341
344
|
function usageLine(usage, colors = paint(process8.stdout)) {
|
|
342
345
|
return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
|
|
343
346
|
}
|
|
344
|
-
function helpText(usage, options, colors = paint(process8.stdout), { examples = [],
|
|
345
|
-
return [usageLine(usage, colors),
|
|
347
|
+
function helpText(usage, options, colors = paint(process8.stdout), { examples = [], exits = [] } = {}) {
|
|
348
|
+
return [usageLine(usage, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exitRows(exits, colors), ...exampleRows(examples, colors)].join(`
|
|
346
349
|
`);
|
|
347
350
|
}
|
|
348
351
|
function exitRows(exits, colors = paint(process8.stdout)) {
|
|
349
352
|
if (exits.length === 0)
|
|
350
353
|
return [];
|
|
351
|
-
|
|
352
|
-
return ["", colors.bold("Exits:"), ...exits.map(([code, meaning]) => ` ${colors.cyan(String(code).padEnd(width))} ${meaning}`)];
|
|
353
|
-
}
|
|
354
|
-
function replacesRows(replaces, colors = paint(process8.stdout)) {
|
|
355
|
-
return replaces === undefined || outputMode() === "agent" ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
354
|
+
return ["", `${colors.bold("Exit codes:")} ${exits.map(([code, meaning]) => `${colors.cyan(String(code))} ${meaning}`).join(", ")}`];
|
|
356
355
|
}
|
|
357
356
|
function wantsHelpJson(env = process8.env) {
|
|
358
357
|
return env.KIT_HELP === "json";
|
|
@@ -627,21 +626,45 @@ function nearestOption(message, options, allowNegative) {
|
|
|
627
626
|
const meant = closest(word, known.map((flag) => flag.replace(DASHES, "")));
|
|
628
627
|
return meant === undefined ? undefined : `did you mean ${known.find((flag) => flag.replace(DASHES, "") === meant)}?`;
|
|
629
628
|
}
|
|
629
|
+
// ../kit/src/argv.ts
|
|
630
|
+
import { readFileSync as readFileSync2, realpathSync } from "fs";
|
|
631
|
+
import { resolve as resolve3 } from "path";
|
|
632
|
+
import process10 from "process";
|
|
633
|
+
function commandLine() {
|
|
634
|
+
try {
|
|
635
|
+
return readFileSync2("/proc/self/cmdline", "utf8").split("\x00").slice(0, -1);
|
|
636
|
+
} catch {
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
function realPath(word) {
|
|
641
|
+
try {
|
|
642
|
+
return realpathSync(resolve3(word));
|
|
643
|
+
} catch {
|
|
644
|
+
return;
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
function scriptArgs(argv = process10.argv.slice(2), cmdline = commandLine(), script = process10.argv[1]) {
|
|
648
|
+
const target = script === undefined ? undefined : realPath(script) ?? script;
|
|
649
|
+
const at = cmdline === undefined || target === undefined ? -1 : cmdline.findIndex((word, index) => index > 0 && (resolve3(word) === script || realPath(word) === target));
|
|
650
|
+
const after = at === -1 ? [] : cmdline.slice(at + 1);
|
|
651
|
+
return after[0] === "--" && after.length === argv.length + 1 && argv.every((word, index) => word === after[index + 1]) ? after : argv;
|
|
652
|
+
}
|
|
630
653
|
// ../kit/src/progress.ts
|
|
631
654
|
var ESC = String.fromCharCode(27);
|
|
632
655
|
var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x7E]|\\][^\\u0007${ESC}]*(?:\\u0007|${ESC}\\\\))`, "y");
|
|
633
656
|
var RESET_STYLE = `${ESC}[0m`;
|
|
634
657
|
var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
635
658
|
// ../kit/src/cli.ts
|
|
636
|
-
import { readFileSync as
|
|
659
|
+
import { readFileSync as readFileSync3, realpathSync as realpathSync2 } from "fs";
|
|
637
660
|
import { dirname as dirname3, join as join5 } from "path";
|
|
638
|
-
import
|
|
661
|
+
import process12 from "process";
|
|
639
662
|
|
|
640
663
|
// ../kit/src/usage.ts
|
|
641
664
|
import { appendFileSync, mkdirSync } from "fs";
|
|
642
665
|
import { homedir as homedir3 } from "os";
|
|
643
666
|
import { dirname as dirname2, isAbsolute, join as join4 } from "path";
|
|
644
|
-
import
|
|
667
|
+
import process11 from "process";
|
|
645
668
|
|
|
646
669
|
// ../kit/src/logs.ts
|
|
647
670
|
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
@@ -655,14 +678,14 @@ var usageError;
|
|
|
655
678
|
function noteUsageError(message) {
|
|
656
679
|
usageError = message;
|
|
657
680
|
}
|
|
658
|
-
function stateHome(env =
|
|
681
|
+
function stateHome(env = process11.env) {
|
|
659
682
|
const dir = env.XDG_STATE_HOME;
|
|
660
683
|
return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
|
|
661
684
|
}
|
|
662
|
-
function usagePath(env =
|
|
685
|
+
function usagePath(env = process11.env) {
|
|
663
686
|
return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
664
687
|
}
|
|
665
|
-
function usageRecorded(env =
|
|
688
|
+
function usageRecorded(env = process11.env) {
|
|
666
689
|
const off = env.KAYNOOO_CLI_NO_USAGE;
|
|
667
690
|
return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
|
|
668
691
|
}
|
|
@@ -676,7 +699,7 @@ function usageEntry(bin, command, argv, code, now = new Date) {
|
|
|
676
699
|
usageError = undefined;
|
|
677
700
|
return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
|
|
678
701
|
}
|
|
679
|
-
function recordUsage(entry, env =
|
|
702
|
+
function recordUsage(entry, env = process11.env) {
|
|
680
703
|
if (!usageRecorded(env))
|
|
681
704
|
return;
|
|
682
705
|
try {
|
|
@@ -694,8 +717,8 @@ var LEVEL_OPTIONS = {
|
|
|
694
717
|
};
|
|
695
718
|
var TOP_OPTIONS = { ...LEVEL_OPTIONS, version: { type: "boolean", short: "v", description: "print the version" } };
|
|
696
719
|
function printError(prefix, message, hint, cause) {
|
|
697
|
-
const colors = paint(
|
|
698
|
-
const styled = takesControlCodes(
|
|
720
|
+
const colors = paint(process12.stderr);
|
|
721
|
+
const styled = takesControlCodes(process12.stderr);
|
|
699
722
|
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
700
723
|
const under = (label, text, tint) => text === undefined ? "" : `
|
|
701
724
|
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
@@ -739,24 +762,24 @@ function candidateLine(candidate) {
|
|
|
739
762
|
}
|
|
740
763
|
function positionalBeingTyped(before, { options, positionals }) {
|
|
741
764
|
const takesValue = (word) => !word.includes("=") && Object.entries(options).some(([name, { short, type }]) => type === "string" && (word === `--${name}` || word === `-${short}`));
|
|
742
|
-
|
|
765
|
+
const typed = [];
|
|
743
766
|
let at = 0;
|
|
744
767
|
while (at < before.length) {
|
|
745
768
|
const word = before[at];
|
|
746
769
|
if (word === "--") {
|
|
747
|
-
|
|
770
|
+
typed.push(...before.slice(at + 1));
|
|
748
771
|
break;
|
|
749
772
|
}
|
|
750
773
|
if (!word.startsWith("-"))
|
|
751
|
-
|
|
774
|
+
typed.push(word);
|
|
752
775
|
else if (takesValue(word))
|
|
753
776
|
at++;
|
|
754
777
|
at++;
|
|
755
778
|
}
|
|
756
779
|
const last = positionals.at(-1);
|
|
757
|
-
if (
|
|
758
|
-
return positionals[
|
|
759
|
-
return last?.replace("]", "").endsWith("...") === true ? last : undefined;
|
|
780
|
+
if (typed.length < positionals.length)
|
|
781
|
+
return { name: positionals[typed.length], typed };
|
|
782
|
+
return { name: last?.replace("]", "").endsWith("...") === true ? last : undefined, typed };
|
|
760
783
|
}
|
|
761
784
|
async function leafCandidates(run, words) {
|
|
762
785
|
const completing = await completingOf(run);
|
|
@@ -764,12 +787,12 @@ async function leafCandidates(run, words) {
|
|
|
764
787
|
return Object.entries(completing.options).map(([name, option]) => option.description === undefined ? `--${name}` : `--${name} ${option.description}`);
|
|
765
788
|
const flag = words.at(-2);
|
|
766
789
|
const option = flag?.startsWith("-") === true ? Object.entries(completing.options).find(([name, { short }]) => flag === `--${name}` || flag === `-${short}`)?.[1] : undefined;
|
|
790
|
+
const { name, typed } = positionalBeingTyped(words.slice(0, -1), completing);
|
|
767
791
|
if (option?.type === "string") {
|
|
768
|
-
const values = option.choices === undefined ? await option.candidates?.() : option.choices;
|
|
792
|
+
const values = option.choices === undefined ? await option.candidates?.(typed) : option.choices;
|
|
769
793
|
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
770
794
|
}
|
|
771
|
-
const
|
|
772
|
-
const values = name === undefined ? undefined : await completing.candidates[name]?.();
|
|
795
|
+
const values = name === undefined ? undefined : await completing.candidates[name]?.(typed);
|
|
773
796
|
return values === undefined ? [FILES] : values.map(candidateLine);
|
|
774
797
|
}
|
|
775
798
|
async function printCandidates(found) {
|
|
@@ -778,9 +801,13 @@ async function printCandidates(found) {
|
|
|
778
801
|
return 0;
|
|
779
802
|
}
|
|
780
803
|
function packageVersion(entry = Bun.main) {
|
|
781
|
-
|
|
804
|
+
let resolved = entry;
|
|
805
|
+
try {
|
|
806
|
+
resolved = realpathSync2(entry);
|
|
807
|
+
} catch {}
|
|
808
|
+
for (let dir = dirname3(resolved);; dir = dirname3(dir)) {
|
|
782
809
|
try {
|
|
783
|
-
const { version } = JSON.parse(
|
|
810
|
+
const { version } = JSON.parse(readFileSync3(join5(dir, "package.json"), "utf8"));
|
|
784
811
|
if (typeof version === "string")
|
|
785
812
|
return version;
|
|
786
813
|
} catch {}
|
|
@@ -788,7 +815,7 @@ function packageVersion(entry = Bun.main) {
|
|
|
788
815
|
return "0.0.0";
|
|
789
816
|
}
|
|
790
817
|
}
|
|
791
|
-
async function runMain({ name, version, run, redact, usage }, argv =
|
|
818
|
+
async function runMain({ name, version, run, redact, usage }, argv = scriptArgs()) {
|
|
792
819
|
if (argv[0] === COMPLETE)
|
|
793
820
|
return printCandidates(leafCandidates(run, argv.slice(1)));
|
|
794
821
|
if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
|
|
@@ -1084,11 +1111,19 @@ function chosen(state) {
|
|
|
1084
1111
|
|
|
1085
1112
|
// ../kit/src/picker/terminal.ts
|
|
1086
1113
|
import { openSync } from "fs";
|
|
1087
|
-
import
|
|
1114
|
+
import process15 from "process";
|
|
1088
1115
|
import tty from "tty";
|
|
1089
1116
|
|
|
1117
|
+
// ../kit/src/redraw.ts
|
|
1118
|
+
var SYNC_START = "\x1B[?2026h";
|
|
1119
|
+
var SYNC_END = "\x1B[?2026l";
|
|
1120
|
+
function overwrite(lines, columns) {
|
|
1121
|
+
return lines.map((line) => textWidth(line) < columns ? `${line}\x1B[K` : `\x1B[2K${line}`).join(`
|
|
1122
|
+
`);
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1090
1125
|
// ../kit/src/picker/preview.ts
|
|
1091
|
-
import
|
|
1126
|
+
import process14 from "process";
|
|
1092
1127
|
|
|
1093
1128
|
// ../kit/src/shell.ts
|
|
1094
1129
|
var SEPARATORS = new Set([`
|
|
@@ -1157,7 +1192,7 @@ async function runPreview(template, line, signal, hidden = []) {
|
|
|
1157
1192
|
${previewCommand(template, line, hidden)}`], { stdin: "ignore", stdout: "pipe", stderr: "ignore", detached: true });
|
|
1158
1193
|
const stop = () => {
|
|
1159
1194
|
try {
|
|
1160
|
-
|
|
1195
|
+
process14.kill(-proc.pid, "SIGKILL");
|
|
1161
1196
|
} catch {
|
|
1162
1197
|
proc.kill();
|
|
1163
1198
|
}
|
|
@@ -1306,7 +1341,7 @@ function openTerminal() {
|
|
|
1306
1341
|
return fail("no terminal to read keys from (/dev/tty)");
|
|
1307
1342
|
}
|
|
1308
1343
|
const keys = Bun.file("/dev/tty").stream().getReader();
|
|
1309
|
-
const out =
|
|
1344
|
+
const out = process15.stderr;
|
|
1310
1345
|
let resize;
|
|
1311
1346
|
async function read(onChunk) {
|
|
1312
1347
|
const decoder = new TextDecoder;
|
|
@@ -1359,14 +1394,13 @@ async function select(rows, settings, terminal = openTerminal()) {
|
|
|
1359
1394
|
if (restored)
|
|
1360
1395
|
return;
|
|
1361
1396
|
const layout = layoutFor({ columns: terminal.columns, rows: terminal.rows }, settings.height, settings.preview !== undefined, settings.previewPosition);
|
|
1362
|
-
let frame = HIDE_CURSOR;
|
|
1397
|
+
let frame = SYNC_START + HIDE_CURSOR;
|
|
1363
1398
|
if (reserved !== layout.height || clear) {
|
|
1364
1399
|
frame += CLEAR_BELOW + `
|
|
1365
1400
|
`.repeat(layout.height - 1) + up(layout.height - 1);
|
|
1366
1401
|
reserved = layout.height;
|
|
1367
1402
|
}
|
|
1368
|
-
frame += `\r${render(state, layout, theme, preview).
|
|
1369
|
-
`)}${up(layout.height - 1)}\r\x1B[${caretColumn(state, layout)}G${CARET}\r`;
|
|
1403
|
+
frame += `\r${overwrite(render(state, layout, theme, preview), terminal.columns)}${up(layout.height - 1)}\r\x1B[${caretColumn(state, layout)}G${CARET}\r${SYNC_END}`;
|
|
1370
1404
|
terminal.write(frame);
|
|
1371
1405
|
}
|
|
1372
1406
|
function pageSize() {
|
|
@@ -1480,9 +1514,9 @@ async function select(rows, settings, terminal = openTerminal()) {
|
|
|
1480
1514
|
const ended = new Promise((resolve) => {
|
|
1481
1515
|
finish = resolve;
|
|
1482
1516
|
});
|
|
1483
|
-
|
|
1517
|
+
process15.once("exit", restore);
|
|
1484
1518
|
for (const signal of SIGNALS)
|
|
1485
|
-
|
|
1519
|
+
process15.once(signal, stop);
|
|
1486
1520
|
try {
|
|
1487
1521
|
terminal.write(`${PASTE_ON}${MOUSE_ON}`);
|
|
1488
1522
|
terminal.listen(onChunk, () => draw(true));
|
|
@@ -1495,8 +1529,8 @@ async function select(rows, settings, terminal = openTerminal()) {
|
|
|
1495
1529
|
await ended;
|
|
1496
1530
|
} finally {
|
|
1497
1531
|
for (const signal of SIGNALS)
|
|
1498
|
-
|
|
1499
|
-
|
|
1532
|
+
process15.off(signal, stop);
|
|
1533
|
+
process15.off("exit", restore);
|
|
1500
1534
|
restore();
|
|
1501
1535
|
}
|
|
1502
1536
|
return state.done === "accept" ? { rows: chosen(state), key: state.key, query: state.query } : undefined;
|
|
@@ -1519,9 +1553,8 @@ Type to filter: the query matches as a subsequence, best matches first (a word s
|
|
|
1519
1553
|
The keys are read from the terminal and the selector is drawn on stderr, so \`pick\` in a pipe or a command substitution prints only the choice. With --multi, tab marks the highlighted line and enter prints the marked lines in input order (the highlighted one when none is marked).
|
|
1520
1554
|
--preview runs a command for the highlighted line, \`{}\` standing for it shell-quoted, in a pane on the right when the terminal is at least 80 columns wide; its output is cut at 64 KiB and 200 lines.
|
|
1521
1555
|
|
|
1522
|
-
Without a terminal on stderr there is nobody to choose: it prints the first line containing the query (case-insensitive), every one with --multi
|
|
1523
|
-
|
|
1524
|
-
Exits 0 when it printed a choice, 1 when cancelled, nothing matches or there are no lines, 2 on a usage error.`;
|
|
1556
|
+
Without a terminal on stderr there is nobody to choose: it prints the first line containing the query (case-insensitive), every one with --multi.`;
|
|
1557
|
+
var EXITS = [[0, "it printed a choice"], [1, "cancelled, nothing matches or there are no lines"], [2, "a usage error"]];
|
|
1525
1558
|
var OPTIONS = {
|
|
1526
1559
|
multi: { type: "boolean", short: "m", description: "tab marks several lines, enter prints them" },
|
|
1527
1560
|
prompt: { type: "string", short: "p", description: "the prompt before the query (> )" },
|
|
@@ -1534,24 +1567,24 @@ function print(chosen, json) {
|
|
|
1534
1567
|
if (json)
|
|
1535
1568
|
printJson({ picked: chosen });
|
|
1536
1569
|
else
|
|
1537
|
-
|
|
1570
|
+
process16.stdout.write(`${chosen.join(`
|
|
1538
1571
|
`)}
|
|
1539
1572
|
`);
|
|
1540
1573
|
}
|
|
1541
1574
|
async function main(args) {
|
|
1542
|
-
const { values, positionals } = parse(args, USAGE, OPTIONS, { replaces: "fzf for a quick choice in a pipe, select in a shell script", examples: ["git branch --format='%(refname:short)' | pick", "ls | pick --multi --preview 'head -20 {}'"] });
|
|
1575
|
+
const { values, positionals } = parse(args, USAGE, OPTIONS, { exits: EXITS, replaces: "fzf for a quick choice in a pipe, select in a shell script", examples: ["git branch --format='%(refname:short)' | pick", "ls | pick --multi --preview 'head -20 {}'"] });
|
|
1543
1576
|
if (positionals.length > 0)
|
|
1544
1577
|
badUsage("pick takes its lines on stdin, not as arguments");
|
|
1545
1578
|
const height = values.height === undefined ? DEFAULT_HEIGHT : parseHeight(values.height);
|
|
1546
1579
|
if (height === undefined)
|
|
1547
1580
|
badUsage(`--height takes a number of lines or a percentage such as 40%, not ${values.height}`);
|
|
1548
|
-
if (
|
|
1581
|
+
if (process16.stdin.isTTY)
|
|
1549
1582
|
badUsage("pipe the lines to pick on stdin");
|
|
1550
1583
|
const lines = (await Bun.stdin.text()).split(`
|
|
1551
1584
|
`).filter((line) => line !== "");
|
|
1552
1585
|
if (lines.length === 0)
|
|
1553
1586
|
fail("nothing to pick from", 1);
|
|
1554
|
-
if (!
|
|
1587
|
+
if (!process16.stderr.isTTY) {
|
|
1555
1588
|
const query = (values.query ?? "").toLowerCase();
|
|
1556
1589
|
const matching = lines.filter((line) => line.toLowerCase().includes(query));
|
|
1557
1590
|
if (matching.length === 0)
|
|
@@ -1565,4 +1598,4 @@ async function main(args) {
|
|
|
1565
1598
|
print(picked, values.json === true);
|
|
1566
1599
|
return 0;
|
|
1567
1600
|
}
|
|
1568
|
-
|
|
1601
|
+
process16.exitCode = await runMain({ name: "pick", run: main });
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaynooo/cli-pick",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.9.
|
|
4
|
+
"version": "0.9.3",
|
|
5
5
|
"description": "pick: a themed fuzzy selector, lines on stdin, the chosen one on stdout.",
|
|
6
6
|
"author": "Kaynooo <contact@kaynooo.fr>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -35,6 +35,6 @@
|
|
|
35
35
|
"pick": "$npm_execpath src/pick.ts"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@kaynooo/cli-kit": "0.
|
|
38
|
+
"@kaynooo/cli-kit": "0.38.4"
|
|
39
39
|
}
|
|
40
40
|
}
|