@kaynooo/cli-pick 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/pick.js +121 -44
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ bun add -g @kaynooo/cli-pick
|
|
|
14
14
|
| `ls \| pick --preview 'head -20 {}'` | a pane on the right (terminal at least 80 columns) shows the command run for the highlighted line, `{}` shell-quoted |
|
|
15
15
|
| `--height 12`, `--height 60%` | lines or a share of the terminal to draw in (40%) |
|
|
16
16
|
|
|
17
|
-
Keys: type to filter, up and down or ctrl-p and ctrl-n to move, enter to pick, esc or ctrl-c to cancel (exit 1, nothing printed), ctrl-u clears the query, ctrl-w deletes a word.
|
|
17
|
+
Keys: type to filter, up and down or ctrl-p and ctrl-n to move, enter to pick, esc or ctrl-c to cancel (exit 1, nothing printed), ctrl-u clears the query, ctrl-w deletes a word, ctrl-/ or f1 opens a legend of the keys until the next key.
|
|
18
18
|
|
|
19
19
|
The query matches as a subsequence, best first: a word start, a run of consecutive characters or a prefix scores higher, a lowercase query ignores case and an uppercase letter makes it exact. Keys come from `/dev/tty` and the selector is drawn on stderr, so a pipe or a command substitution takes only the choice. Without a terminal on stderr it exits 2.
|
|
20
20
|
|
package/dist/pick.js
CHANGED
|
@@ -2,11 +2,14 @@
|
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
4
|
// src/pick.ts
|
|
5
|
-
import
|
|
5
|
+
import process15 from "process";
|
|
6
6
|
// ../kit/src/args.ts
|
|
7
|
-
import
|
|
7
|
+
import process9 from "process";
|
|
8
8
|
import { parseArgs } from "util";
|
|
9
9
|
|
|
10
|
+
// ../kit/src/format.ts
|
|
11
|
+
import process7 from "process";
|
|
12
|
+
|
|
10
13
|
// ../kit/src/mode.ts
|
|
11
14
|
import process2 from "process";
|
|
12
15
|
var MODES = new Set(["terminal", "ci", "agent", "pipe"]);
|
|
@@ -519,29 +522,35 @@ function upTo(text, separator) {
|
|
|
519
522
|
return end === -1 ? text : text.slice(0, end);
|
|
520
523
|
}
|
|
521
524
|
var ESCAPE = String.fromCharCode(27);
|
|
525
|
+
function jsonText(value, replacer, { stream = process7.stdout, env = process7.env } = {}) {
|
|
526
|
+
return JSON.stringify(value, replacer, outputMode(stream, env) === "agent" ? undefined : 2);
|
|
527
|
+
}
|
|
528
|
+
function printJson(value) {
|
|
529
|
+
console.log(jsonText(value));
|
|
530
|
+
}
|
|
522
531
|
|
|
523
532
|
// ../kit/src/help.ts
|
|
524
|
-
import
|
|
533
|
+
import process8 from "process";
|
|
525
534
|
var OPTIONAL = /\[[^\]]+\]/g;
|
|
526
535
|
var USAGE_PREFIX = /^usage:\s*/i;
|
|
527
536
|
var FULL_STOP = /\.$/;
|
|
528
|
-
function usageLine(usage, colors = paint(
|
|
537
|
+
function usageLine(usage, colors = paint(process8.stdout)) {
|
|
529
538
|
return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
|
|
530
539
|
}
|
|
531
|
-
function helpText(usage, options, colors = paint(
|
|
540
|
+
function helpText(usage, options, colors = paint(process8.stdout), { examples = [], replaces, exits = [] } = {}) {
|
|
532
541
|
return [usageLine(usage, colors), ...replacesRows(replaces, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exitRows(exits, colors), ...exampleRows(examples, colors)].join(`
|
|
533
542
|
`);
|
|
534
543
|
}
|
|
535
|
-
function exitRows(exits, colors = paint(
|
|
544
|
+
function exitRows(exits, colors = paint(process8.stdout)) {
|
|
536
545
|
if (exits.length === 0)
|
|
537
546
|
return [];
|
|
538
547
|
const width = Math.max(...exits.map(([code]) => String(code).length));
|
|
539
548
|
return ["", colors.bold("Exits:"), ...exits.map(([code, meaning]) => ` ${colors.cyan(String(code).padEnd(width))} ${meaning}`)];
|
|
540
549
|
}
|
|
541
|
-
function replacesRows(replaces, colors = paint(
|
|
550
|
+
function replacesRows(replaces, colors = paint(process8.stdout)) {
|
|
542
551
|
return replaces === undefined || outputMode() === "agent" ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
543
552
|
}
|
|
544
|
-
function wantsHelpJson(env =
|
|
553
|
+
function wantsHelpJson(env = process8.env) {
|
|
545
554
|
return env.KIT_HELP === "json";
|
|
546
555
|
}
|
|
547
556
|
function description(usage, { examples = [], replaces, exits } = {}, options) {
|
|
@@ -560,12 +569,12 @@ function description(usage, { examples = [], replaces, exits } = {}, options) {
|
|
|
560
569
|
...options === undefined ? {} : { options: Object.entries(options).map(([name, { short, description }]) => ({ name, ...short === undefined ? {} : { short }, ...description === undefined ? {} : { description } })) }
|
|
561
570
|
};
|
|
562
571
|
}
|
|
563
|
-
function exampleRows(examples, colors = paint(
|
|
572
|
+
function exampleRows(examples, colors = paint(process8.stdout)) {
|
|
564
573
|
if (examples.length === 0 || outputMode() === "agent")
|
|
565
574
|
return [];
|
|
566
575
|
return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
|
|
567
576
|
}
|
|
568
|
-
function optionRows(options, colors = paint(
|
|
577
|
+
function optionRows(options, colors = paint(process8.stdout)) {
|
|
569
578
|
const rows = Object.entries(options).map(([name, option]) => {
|
|
570
579
|
const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
|
|
571
580
|
const named = option.choices === undefined ? "<value>" : `<${option.choices.join("|")}>`;
|
|
@@ -599,6 +608,9 @@ function closest(name, names) {
|
|
|
599
608
|
|
|
600
609
|
// ../kit/src/args.ts
|
|
601
610
|
var DASHES = /^-+/;
|
|
611
|
+
function jsonOption(holds) {
|
|
612
|
+
return { type: "boolean", description: holds === undefined ? "print JSON" : `print JSON: ${holds}` };
|
|
613
|
+
}
|
|
602
614
|
class Exit extends Error {
|
|
603
615
|
code;
|
|
604
616
|
hint;
|
|
@@ -642,7 +654,7 @@ var OUTPUT_OPTIONS = {
|
|
|
642
654
|
color: { type: "string", description: "auto (on a terminal alone), always or never" },
|
|
643
655
|
plain: { type: "boolean", description: "plain text, no colour or spinner: --color never" }
|
|
644
656
|
};
|
|
645
|
-
function applyOutput(color, plain, env =
|
|
657
|
+
function applyOutput(color, plain, env = process9.env) {
|
|
646
658
|
const when = plain === true ? "never" : color ?? "auto";
|
|
647
659
|
if (when === "auto")
|
|
648
660
|
return;
|
|
@@ -819,13 +831,13 @@ var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
|
819
831
|
// ../kit/src/cli.ts
|
|
820
832
|
import { readFileSync as readFileSync2, realpathSync } from "fs";
|
|
821
833
|
import { dirname as dirname3, join as join5 } from "path";
|
|
822
|
-
import
|
|
834
|
+
import process11 from "process";
|
|
823
835
|
|
|
824
836
|
// ../kit/src/usage.ts
|
|
825
837
|
import { appendFileSync, mkdirSync } from "fs";
|
|
826
838
|
import { homedir as homedir3 } from "os";
|
|
827
839
|
import { dirname as dirname2, isAbsolute, join as join4 } from "path";
|
|
828
|
-
import
|
|
840
|
+
import process10 from "process";
|
|
829
841
|
|
|
830
842
|
// ../kit/src/logs.ts
|
|
831
843
|
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
@@ -839,14 +851,14 @@ var usageError;
|
|
|
839
851
|
function noteUsageError(message) {
|
|
840
852
|
usageError = message;
|
|
841
853
|
}
|
|
842
|
-
function stateHome(env =
|
|
854
|
+
function stateHome(env = process10.env) {
|
|
843
855
|
const dir = env.XDG_STATE_HOME;
|
|
844
856
|
return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
|
|
845
857
|
}
|
|
846
|
-
function usagePath(env =
|
|
858
|
+
function usagePath(env = process10.env) {
|
|
847
859
|
return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
848
860
|
}
|
|
849
|
-
function usageRecorded(env =
|
|
861
|
+
function usageRecorded(env = process10.env) {
|
|
850
862
|
const off = env.KAYNOOO_CLI_NO_USAGE;
|
|
851
863
|
return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
|
|
852
864
|
}
|
|
@@ -860,7 +872,7 @@ function usageEntry(bin, command, argv, code, now = new Date) {
|
|
|
860
872
|
usageError = undefined;
|
|
861
873
|
return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
|
|
862
874
|
}
|
|
863
|
-
function recordUsage(entry, env =
|
|
875
|
+
function recordUsage(entry, env = process10.env) {
|
|
864
876
|
if (!usageRecorded(env))
|
|
865
877
|
return;
|
|
866
878
|
try {
|
|
@@ -878,8 +890,8 @@ var LEVEL_OPTIONS = {
|
|
|
878
890
|
};
|
|
879
891
|
var TOP_OPTIONS = { ...LEVEL_OPTIONS, version: { type: "boolean", short: "v", description: "print the version" } };
|
|
880
892
|
function printError(prefix, message, hint, cause) {
|
|
881
|
-
const colors = paint(
|
|
882
|
-
const styled = takesControlCodes(
|
|
893
|
+
const colors = paint(process11.stderr);
|
|
894
|
+
const styled = takesControlCodes(process11.stderr);
|
|
883
895
|
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
884
896
|
const under = (label, text, tint) => text === undefined ? "" : `
|
|
885
897
|
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
@@ -972,7 +984,7 @@ function packageVersion(entry = Bun.main) {
|
|
|
972
984
|
return "0.0.0";
|
|
973
985
|
}
|
|
974
986
|
}
|
|
975
|
-
async function runMain({ name, version, run, redact, usage }, argv =
|
|
987
|
+
async function runMain({ name, version, run, redact, usage }, argv = process11.argv.slice(2)) {
|
|
976
988
|
if (argv[0] === COMPLETE)
|
|
977
989
|
return printCandidates(leafCandidates(run, argv.slice(1)));
|
|
978
990
|
if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
|
|
@@ -1099,6 +1111,14 @@ var KEYS = {
|
|
|
1099
1111
|
"\x1B[B": "down",
|
|
1100
1112
|
"\x1BOB": "down",
|
|
1101
1113
|
"\x0E": "down",
|
|
1114
|
+
"\x1B[5~": "pageup",
|
|
1115
|
+
"\x1B[6~": "pagedown",
|
|
1116
|
+
"\x1B[H": "home",
|
|
1117
|
+
"\x1B[1~": "home",
|
|
1118
|
+
"\x1BOH": "home",
|
|
1119
|
+
"\x1B[F": "end",
|
|
1120
|
+
"\x1B[4~": "end",
|
|
1121
|
+
"\x1BOF": "end",
|
|
1102
1122
|
"\r": "enter",
|
|
1103
1123
|
"\n": "enter",
|
|
1104
1124
|
"\x03": "cancel",
|
|
@@ -1107,7 +1127,10 @@ var KEYS = {
|
|
|
1107
1127
|
"\b": "erase",
|
|
1108
1128
|
"\x15": "clear",
|
|
1109
1129
|
"\x17": "word",
|
|
1110
|
-
"\t": "tab"
|
|
1130
|
+
"\t": "tab",
|
|
1131
|
+
"\x1F": "help",
|
|
1132
|
+
"\x1BOP": "help",
|
|
1133
|
+
"\x1B[11~": "help"
|
|
1111
1134
|
};
|
|
1112
1135
|
function repeat(value, count) {
|
|
1113
1136
|
return Array.from({ length: count }).fill(value);
|
|
@@ -1176,7 +1199,7 @@ function parseKeys(input, paste = { active: false }, ctrls = []) {
|
|
|
1176
1199
|
});
|
|
1177
1200
|
}
|
|
1178
1201
|
function createState(rows, { multi = false, prompt = "> ", query = "", header = "", expect = [], searched = Number.POSITIVE_INFINITY } = {}) {
|
|
1179
|
-
const state = { rows: [], items: [], styles: [], searchable: [], searched, header, expect, key: "", status: "", query, matches: [], cursor: 0, marked: new Set, multi, prompt, done: undefined };
|
|
1202
|
+
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 };
|
|
1180
1203
|
return withRows(state, rows);
|
|
1181
1204
|
}
|
|
1182
1205
|
function withRows(state, rows, header = state.header) {
|
|
@@ -1198,6 +1221,8 @@ function highlighted(state) {
|
|
|
1198
1221
|
function press(state, key) {
|
|
1199
1222
|
if (state.done !== undefined)
|
|
1200
1223
|
return state;
|
|
1224
|
+
if (state.help)
|
|
1225
|
+
return { ...state, help: false };
|
|
1201
1226
|
if (typeof key === "object") {
|
|
1202
1227
|
if ("text" in key)
|
|
1203
1228
|
return withQuery(state, state.query + key.text);
|
|
@@ -1208,8 +1233,18 @@ function press(state, key) {
|
|
|
1208
1233
|
return move(state, -1);
|
|
1209
1234
|
case "down":
|
|
1210
1235
|
return move(state, 1);
|
|
1236
|
+
case "pageup":
|
|
1237
|
+
return move(state, -state.page);
|
|
1238
|
+
case "pagedown":
|
|
1239
|
+
return move(state, state.page);
|
|
1240
|
+
case "home":
|
|
1241
|
+
return move(state, -state.matches.length);
|
|
1242
|
+
case "end":
|
|
1243
|
+
return move(state, state.matches.length);
|
|
1211
1244
|
case "cancel":
|
|
1212
1245
|
return { ...state, done: "cancel" };
|
|
1246
|
+
case "help":
|
|
1247
|
+
return { ...state, help: true };
|
|
1213
1248
|
case "erase":
|
|
1214
1249
|
return withQuery(state, [...state.query].slice(0, -1).join(""));
|
|
1215
1250
|
case "clear":
|
|
@@ -1238,11 +1273,11 @@ function chosen(state) {
|
|
|
1238
1273
|
|
|
1239
1274
|
// ../kit/src/picker/terminal.ts
|
|
1240
1275
|
import { openSync } from "fs";
|
|
1241
|
-
import
|
|
1276
|
+
import process14 from "process";
|
|
1242
1277
|
import tty from "tty";
|
|
1243
1278
|
|
|
1244
1279
|
// ../kit/src/picker/preview.ts
|
|
1245
|
-
import
|
|
1280
|
+
import process13 from "process";
|
|
1246
1281
|
|
|
1247
1282
|
// ../kit/src/shell.ts
|
|
1248
1283
|
var SEPARATORS = new Set([`
|
|
@@ -1311,7 +1346,7 @@ async function runPreview(template, line, signal, hidden = []) {
|
|
|
1311
1346
|
${previewCommand(template, line, hidden)}`], { stdin: "ignore", stdout: "pipe", stderr: "ignore", detached: true });
|
|
1312
1347
|
const stop = () => {
|
|
1313
1348
|
try {
|
|
1314
|
-
|
|
1349
|
+
process13.kill(-proc.pid, "SIGKILL");
|
|
1315
1350
|
} catch {
|
|
1316
1351
|
proc.kill();
|
|
1317
1352
|
}
|
|
@@ -1390,7 +1425,18 @@ function fitStyled(line, width, plain = identity) {
|
|
|
1390
1425
|
function caretColumn(state, layout) {
|
|
1391
1426
|
return Math.min(layout.listWidth, Bun.stringWidth(state.prompt) + Bun.stringWidth(state.query) + 1);
|
|
1392
1427
|
}
|
|
1428
|
+
function helpLines(state, layout, theme) {
|
|
1429
|
+
const lines = [
|
|
1430
|
+
"type to filter, up, down, ctrl-p, ctrl-n, pgup, pgdn, home, end move",
|
|
1431
|
+
`enter picks${state.multi ? ", tab marks" : ""}, ctrl-u clears the query, ctrl-w a word, esc or ctrl-c cancels`,
|
|
1432
|
+
...state.expect.length > 0 ? [`${state.expect.join(", ")} end the pick as this command asks`] : []
|
|
1433
|
+
];
|
|
1434
|
+
const title = "keys, any key closes this help";
|
|
1435
|
+
return [{ text: theme.accent(title), width: title.length }, ...lines.map((line) => fit(` ${line}`, layout.listWidth, theme.label))];
|
|
1436
|
+
}
|
|
1393
1437
|
function listLines(state, layout, theme) {
|
|
1438
|
+
if (state.help)
|
|
1439
|
+
return helpLines(state, layout, theme);
|
|
1394
1440
|
const header = state.header === "" ? [] : state.header.split(`
|
|
1395
1441
|
`).map((line) => fitStyled(line, layout.listWidth, theme.label));
|
|
1396
1442
|
const rows = Math.max(1, layout.listHeight - 2 - header.length);
|
|
@@ -1399,7 +1445,8 @@ function listLines(state, layout, theme) {
|
|
|
1399
1445
|
const marks = (state.marked.size > 0 ? ` (${state.marked.size} marked)` : "") + (state.status === "" ? "" : ` ${state.status}`);
|
|
1400
1446
|
const promptWidth = Bun.stringWidth(state.prompt);
|
|
1401
1447
|
const query = fit(state.query, Math.max(1, layout.listWidth - promptWidth), identity);
|
|
1402
|
-
const
|
|
1448
|
+
const place = state.matches.length > rows ? ` (${state.cursor + 1} of ${state.matches.length})` : "";
|
|
1449
|
+
const count = fit(` ${state.matches.length}/${state.items.length}${place}${marks}`, layout.listWidth, theme.label);
|
|
1403
1450
|
const lines = [{ text: theme.accent(state.prompt) + query.text, width: promptWidth + query.width }, count, ...header];
|
|
1404
1451
|
if (state.matches.length === 0)
|
|
1405
1452
|
lines.push(fit(" no match", layout.listWidth, theme.label));
|
|
@@ -1426,6 +1473,7 @@ function render(state, layout, theme, preview = []) {
|
|
|
1426
1473
|
|
|
1427
1474
|
// ../kit/src/picker/terminal.ts
|
|
1428
1475
|
var PREVIEW_DELAY_MS = 120;
|
|
1476
|
+
var ESCAPE_WAIT_MS = 30;
|
|
1429
1477
|
var OPEN = { ctrl: "open" };
|
|
1430
1478
|
var HIDE_CURSOR = "\x1B[?25l";
|
|
1431
1479
|
var SHOW_CURSOR = "\x1B[?25h";
|
|
@@ -1443,7 +1491,7 @@ function openTerminal() {
|
|
|
1443
1491
|
} catch {
|
|
1444
1492
|
return fail("no terminal to read keys from (/dev/tty)");
|
|
1445
1493
|
}
|
|
1446
|
-
const out =
|
|
1494
|
+
const out = process14.stderr;
|
|
1447
1495
|
let resize;
|
|
1448
1496
|
return {
|
|
1449
1497
|
get columns() {
|
|
@@ -1481,20 +1529,29 @@ async function select(rows, settings, terminal = openTerminal()) {
|
|
|
1481
1529
|
let shownLine;
|
|
1482
1530
|
let finish;
|
|
1483
1531
|
let restored = false;
|
|
1532
|
+
let escape;
|
|
1533
|
+
let caret;
|
|
1484
1534
|
function draw(clear = false) {
|
|
1485
1535
|
if (restored)
|
|
1486
1536
|
return;
|
|
1487
1537
|
const layout = layoutFor({ columns: terminal.columns, rows: terminal.rows }, settings.height, settings.preview !== undefined, settings.previewPosition);
|
|
1488
1538
|
let frame = HIDE_CURSOR;
|
|
1489
1539
|
if (reserved !== layout.height || clear) {
|
|
1540
|
+
frame += clear && caret !== undefined ? up(Math.floor((caret - 1) / terminal.columns)) : "";
|
|
1490
1541
|
frame += CLEAR_BELOW + `
|
|
1491
1542
|
`.repeat(layout.height - 1) + up(layout.height - 1);
|
|
1492
1543
|
reserved = layout.height;
|
|
1493
1544
|
}
|
|
1545
|
+
caret = caretColumn(state, layout);
|
|
1494
1546
|
frame += `\r${render(state, layout, theme, preview).map((line) => `\x1B[2K${line}`).join(`
|
|
1495
|
-
`)}${up(layout.height - 1)}\r\x1B[${
|
|
1547
|
+
`)}${up(layout.height - 1)}\r\x1B[${caret}G${SHOW_CURSOR}`;
|
|
1496
1548
|
terminal.write(frame);
|
|
1497
1549
|
}
|
|
1550
|
+
function pageSize() {
|
|
1551
|
+
const headerLines = state.header === "" ? 0 : state.header.split(`
|
|
1552
|
+
`).length;
|
|
1553
|
+
return Math.max(1, layoutFor({ columns: terminal.columns, rows: terminal.rows }, settings.height, settings.preview !== undefined, settings.previewPosition).listHeight - 2 - headerLines);
|
|
1554
|
+
}
|
|
1498
1555
|
function refreshPreview() {
|
|
1499
1556
|
const at = highlighted(state);
|
|
1500
1557
|
if (settings.preview === undefined || at === shownLine)
|
|
@@ -1524,6 +1581,7 @@ async function select(rows, settings, terminal = openTerminal()) {
|
|
|
1524
1581
|
return;
|
|
1525
1582
|
restored = true;
|
|
1526
1583
|
clearTimeout(previewTimer);
|
|
1584
|
+
clearTimeout(escape);
|
|
1527
1585
|
previewing.abort();
|
|
1528
1586
|
terminal.write(`${CLEAR_BELOW}${PASTE_OFF}${SHOW_CURSOR}`);
|
|
1529
1587
|
terminal.close();
|
|
@@ -1562,7 +1620,7 @@ async function select(rows, settings, terminal = openTerminal()) {
|
|
|
1562
1620
|
if (key === OPEN || typeof key === "object" && "ctrl" in key && key.ctrl === reload?.key)
|
|
1563
1621
|
await reloadRows();
|
|
1564
1622
|
else
|
|
1565
|
-
state = press(state, key);
|
|
1623
|
+
state = press({ ...state, page: pageSize() }, key);
|
|
1566
1624
|
}
|
|
1567
1625
|
} finally {
|
|
1568
1626
|
draining = false;
|
|
@@ -1575,6 +1633,20 @@ async function select(rows, settings, terminal = openTerminal()) {
|
|
|
1575
1633
|
draw();
|
|
1576
1634
|
}
|
|
1577
1635
|
function onChunk(chunk) {
|
|
1636
|
+
if (escape !== undefined) {
|
|
1637
|
+
clearTimeout(escape);
|
|
1638
|
+
escape = undefined;
|
|
1639
|
+
chunk = `\x1B${chunk}`;
|
|
1640
|
+
} else if (chunk === "\x1B") {
|
|
1641
|
+
escape = setTimeout(() => {
|
|
1642
|
+
escape = undefined;
|
|
1643
|
+
takeKeys("\x1B");
|
|
1644
|
+
}, ESCAPE_WAIT_MS);
|
|
1645
|
+
return;
|
|
1646
|
+
}
|
|
1647
|
+
takeKeys(chunk);
|
|
1648
|
+
}
|
|
1649
|
+
function takeKeys(chunk) {
|
|
1578
1650
|
const keys = parseKeys(chunk, paste, [...settings.expect ?? [], ...reload?.key === undefined ? [] : [reload.key]]);
|
|
1579
1651
|
if (busy && keys.includes("cancel")) {
|
|
1580
1652
|
stop();
|
|
@@ -1586,9 +1658,9 @@ async function select(rows, settings, terminal = openTerminal()) {
|
|
|
1586
1658
|
const ended = new Promise((resolve) => {
|
|
1587
1659
|
finish = resolve;
|
|
1588
1660
|
});
|
|
1589
|
-
|
|
1661
|
+
process14.once("exit", restore);
|
|
1590
1662
|
for (const signal of SIGNALS)
|
|
1591
|
-
|
|
1663
|
+
process14.once(signal, stop);
|
|
1592
1664
|
try {
|
|
1593
1665
|
terminal.write(PASTE_ON);
|
|
1594
1666
|
terminal.listen(onChunk, () => draw(true));
|
|
@@ -1601,8 +1673,8 @@ async function select(rows, settings, terminal = openTerminal()) {
|
|
|
1601
1673
|
await ended;
|
|
1602
1674
|
} finally {
|
|
1603
1675
|
for (const signal of SIGNALS)
|
|
1604
|
-
|
|
1605
|
-
|
|
1676
|
+
process14.off(signal, stop);
|
|
1677
|
+
process14.off("exit", restore);
|
|
1606
1678
|
restore();
|
|
1607
1679
|
}
|
|
1608
1680
|
return state.done === "accept" ? { rows: chosen(state), key: state.key, query: state.query } : undefined;
|
|
@@ -1621,7 +1693,7 @@ async function choose(lines, settings) {
|
|
|
1621
1693
|
// src/pick.ts
|
|
1622
1694
|
var USAGE = `pick [options]: a fuzzy selector, the lines on stdin in, the chosen one on stdout out
|
|
1623
1695
|
|
|
1624
|
-
Type to filter: the query matches as a subsequence, best matches first (a word start, a run of characters or a prefix scores higher; a lowercase query ignores case, an uppercase letter makes it exact). Up and down or ctrl-p and ctrl-n move, enter picks, esc and ctrl-c cancel, ctrl-u clears the query.
|
|
1696
|
+
Type to filter: the query matches as a subsequence, best matches first (a word start, a run of characters or a prefix scores higher; a lowercase query ignores case, an uppercase letter makes it exact). Up and down or ctrl-p and ctrl-n move, enter picks, esc and ctrl-c cancel, ctrl-u clears the query, ctrl-/ or f1 shows the keys.
|
|
1625
1697
|
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).
|
|
1626
1698
|
--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.
|
|
1627
1699
|
|
|
@@ -1633,8 +1705,17 @@ var OPTIONS = {
|
|
|
1633
1705
|
prompt: { type: "string", short: "p", description: "the prompt before the query (> )" },
|
|
1634
1706
|
query: { type: "string", short: "q", description: "the query to start with" },
|
|
1635
1707
|
preview: { type: "string", description: "a command shown for the highlighted line, {} standing for it" },
|
|
1636
|
-
height: { type: "string", description: "lines or a percentage of the terminal to draw in (40%)" }
|
|
1708
|
+
height: { type: "string", description: "lines or a percentage of the terminal to draw in (40%)" },
|
|
1709
|
+
json: jsonOption("{picked: [lines]}, the choice or the marked lines in input order")
|
|
1637
1710
|
};
|
|
1711
|
+
function print(chosen, json) {
|
|
1712
|
+
if (json)
|
|
1713
|
+
printJson({ picked: chosen });
|
|
1714
|
+
else
|
|
1715
|
+
process15.stdout.write(`${chosen.join(`
|
|
1716
|
+
`)}
|
|
1717
|
+
`);
|
|
1718
|
+
}
|
|
1638
1719
|
async function main(args) {
|
|
1639
1720
|
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 {}'"] });
|
|
1640
1721
|
if (positionals.length > 0)
|
|
@@ -1642,28 +1723,24 @@ async function main(args) {
|
|
|
1642
1723
|
const height = values.height === undefined ? DEFAULT_HEIGHT : parseHeight(values.height);
|
|
1643
1724
|
if (height === undefined)
|
|
1644
1725
|
badUsage(`--height takes a number of lines or a percentage such as 40%, not ${values.height}`);
|
|
1645
|
-
if (
|
|
1726
|
+
if (process15.stdin.isTTY)
|
|
1646
1727
|
badUsage("pipe the lines to pick on stdin");
|
|
1647
1728
|
const lines = (await Bun.stdin.text()).split(`
|
|
1648
1729
|
`).filter((line) => line !== "");
|
|
1649
1730
|
if (lines.length === 0)
|
|
1650
1731
|
fail("nothing to pick from", 1);
|
|
1651
|
-
if (!
|
|
1732
|
+
if (!process15.stderr.isTTY) {
|
|
1652
1733
|
const query = (values.query ?? "").toLowerCase();
|
|
1653
1734
|
const matching = lines.filter((line) => line.toLowerCase().includes(query));
|
|
1654
1735
|
if (matching.length === 0)
|
|
1655
1736
|
return 1;
|
|
1656
|
-
|
|
1657
|
-
`)}
|
|
1658
|
-
`);
|
|
1737
|
+
print(values.multi === true ? matching : matching.slice(0, 1), values.json === true);
|
|
1659
1738
|
return 0;
|
|
1660
1739
|
}
|
|
1661
1740
|
const picked = await choose(lines, { multi: values.multi === true, prompt: values.prompt ?? "> ", query: values.query ?? "", height, preview: values.preview });
|
|
1662
1741
|
if (picked === undefined)
|
|
1663
1742
|
return 1;
|
|
1664
|
-
|
|
1665
|
-
`)}
|
|
1666
|
-
`);
|
|
1743
|
+
print(picked, values.json === true);
|
|
1667
1744
|
return 0;
|
|
1668
1745
|
}
|
|
1669
|
-
|
|
1746
|
+
process15.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.
|
|
4
|
+
"version": "0.6.0",
|
|
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.26.0"
|
|
39
39
|
}
|
|
40
40
|
}
|