@kaynooo/cli-pick 0.3.0 → 0.5.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 +166 -59
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @kaynooo/cli-pick
|
|
2
2
|
|
|
3
|
-
`pick` is a fuzzy selector in the colours of the other @kaynooo CLIs: lines on stdin, the chosen one on stdout. It is for a person at a terminal, not for a Claude Code session, which has no terminal to answer it.
|
|
3
|
+
`pick` is a fuzzy selector in the colours of the other @kaynooo CLIs: lines on stdin, the chosen one on stdout. It is for a person at a terminal, not for a Claude Code session, which has no terminal to answer it; without a terminal on stderr it prints the first line containing `--query` (case-insensitive, every one with `--multi`) and exits 1 when none does.
|
|
4
4
|
|
|
5
5
|
```sh
|
|
6
6
|
bun add -g @kaynooo/cli-pick
|
package/dist/pick.js
CHANGED
|
@@ -2,13 +2,34 @@
|
|
|
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
|
+
|
|
13
|
+
// ../kit/src/mode.ts
|
|
14
|
+
import process2 from "process";
|
|
15
|
+
var MODES = new Set(["terminal", "ci", "agent", "pipe"]);
|
|
16
|
+
var AGENT_VARIABLES = ["CLAUDECODE", "AI_AGENT"];
|
|
17
|
+
function isSet(value) {
|
|
18
|
+
return value !== undefined && value !== "" && value !== "0" && value.toLowerCase() !== "false";
|
|
19
|
+
}
|
|
20
|
+
function outputMode(stream = process2.stdout, env = process2.env) {
|
|
21
|
+
const chosen = env.KIT_MODE;
|
|
22
|
+
if (chosen !== undefined && MODES.has(chosen))
|
|
23
|
+
return chosen;
|
|
24
|
+
if (AGENT_VARIABLES.some((name) => isSet(env[name])))
|
|
25
|
+
return "agent";
|
|
26
|
+
if (stream.isTTY === true)
|
|
27
|
+
return "terminal";
|
|
28
|
+
return isSet(env.CI) ? "ci" : "pipe";
|
|
29
|
+
}
|
|
30
|
+
|
|
10
31
|
// ../kit/src/style.ts
|
|
11
|
-
import
|
|
32
|
+
import process6 from "process";
|
|
12
33
|
|
|
13
34
|
// ../../node_modules/.bun/@kaynooo+utils@2.31.2+d0769e28dc4dad16/node_modules/@kaynooo/utils/dist/chunk-mtetydvc.js
|
|
14
35
|
function x(s) {
|
|
@@ -275,13 +296,13 @@ var t = le();
|
|
|
275
296
|
import { existsSync as existsSync2, readFileSync, statSync } from "fs";
|
|
276
297
|
import { homedir as homedir2 } from "os";
|
|
277
298
|
import { join as join3, resolve as resolve2 } from "path";
|
|
278
|
-
import
|
|
299
|
+
import process5 from "process";
|
|
279
300
|
|
|
280
301
|
// ../kit/src/config.ts
|
|
281
302
|
import { existsSync } from "fs";
|
|
282
303
|
import { dirname, join, resolve } from "path";
|
|
283
|
-
import
|
|
284
|
-
function findUp(names, from =
|
|
304
|
+
import process3 from "process";
|
|
305
|
+
function findUp(names, from = process3.cwd(), stop = () => false) {
|
|
285
306
|
const wanted = typeof names === "string" ? [names] : names;
|
|
286
307
|
for (let dir = resolve(from);; dir = dirname(dir)) {
|
|
287
308
|
const found = wanted.map((name) => join(dir, name)).find((path) => existsSync(path));
|
|
@@ -295,9 +316,9 @@ function findUp(names, from = process2.cwd(), stop = () => false) {
|
|
|
295
316
|
// ../kit/src/user-config.ts
|
|
296
317
|
import { homedir } from "os";
|
|
297
318
|
import { join as join2 } from "path";
|
|
298
|
-
import
|
|
319
|
+
import process4 from "process";
|
|
299
320
|
function configBase(where = {}) {
|
|
300
|
-
const xdg = where.xdg ?? (where.home === undefined ?
|
|
321
|
+
const xdg = where.xdg ?? (where.home === undefined ? process4.env.XDG_CONFIG_HOME : undefined);
|
|
301
322
|
return xdg !== undefined && xdg !== "" ? xdg : join2(where.home ?? homedir(), ".config");
|
|
302
323
|
}
|
|
303
324
|
function userConfigPath(bin, where = {}) {
|
|
@@ -330,10 +351,30 @@ function isColorSpec(spec) {
|
|
|
330
351
|
return Number.isInteger(spec) && spec >= 0 && spec <= 255;
|
|
331
352
|
return typeof spec === "string" && spec.trim() !== "" && spec.trim().split(SPACES).every((token) => STYLES.has(token) || HEX.test(token));
|
|
332
353
|
}
|
|
333
|
-
|
|
354
|
+
var CUBE = [0, 95, 135, 175, 215, 255];
|
|
355
|
+
function cubeLevel(channel) {
|
|
356
|
+
return CUBE.reduce((best, value, level) => Math.abs(value - channel) < Math.abs(CUBE[best] - channel) ? level : best, 0);
|
|
357
|
+
}
|
|
358
|
+
function nearest256(hex) {
|
|
359
|
+
const rgb = [1, 3, 5].map((at) => Number.parseInt(hex.slice(at, at + 2), 16));
|
|
360
|
+
const levels = rgb.map(cubeLevel);
|
|
361
|
+
const cube = levels.map((level) => CUBE[level]);
|
|
362
|
+
const grey = Math.min(23, Math.max(0, Math.round((rgb.reduce((sum, channel) => sum + channel, 0) / 3 - 8) / 10)));
|
|
363
|
+
const greyValue = 8 + grey * 10;
|
|
364
|
+
const distance = (colour) => colour.reduce((sum, channel, at) => sum + (channel - rgb[at]) ** 2, 0);
|
|
365
|
+
return distance([greyValue, greyValue, greyValue]) < distance(cube) ? 232 + grey : 16 + 36 * levels[0] + 6 * levels[1] + levels[2];
|
|
366
|
+
}
|
|
367
|
+
function hasTrueColor(env) {
|
|
368
|
+
return env.COLORTERM === "truecolor" || env.COLORTERM === "24bit";
|
|
369
|
+
}
|
|
370
|
+
function tinted(colors, spec, env = process5.env) {
|
|
334
371
|
if (typeof spec === "number")
|
|
335
372
|
return colors.ansi256(spec);
|
|
336
|
-
return spec.trim().split(SPACES).reduce((chain, token) =>
|
|
373
|
+
return spec.trim().split(SPACES).reduce((chain, token) => {
|
|
374
|
+
if (HEX.test(token))
|
|
375
|
+
return hasTrueColor(env) ? chain.hex(token) : chain.ansi256(nearest256(token));
|
|
376
|
+
return chain[token] ?? chain;
|
|
377
|
+
}, colors);
|
|
337
378
|
}
|
|
338
379
|
function isRecord(value) {
|
|
339
380
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
@@ -417,7 +458,7 @@ function warnOnce(message) {
|
|
|
417
458
|
function isPath(chosen) {
|
|
418
459
|
return chosen.includes("/") || chosen.endsWith(".json");
|
|
419
460
|
}
|
|
420
|
-
function loadTheme({ env =
|
|
461
|
+
function loadTheme({ env = process5.env, from = process5.cwd(), home = homedir2(), warn = warnOnce } = {}) {
|
|
421
462
|
const theme = { colors: { ...DEFAULT.colors }, glyphs: { ...DEFAULT.glyphs }, accents: [...DEFAULT.accents], bins: Object.create(null) };
|
|
422
463
|
const sources = [];
|
|
423
464
|
const chosen = env.KIT_THEME ?? "";
|
|
@@ -447,7 +488,7 @@ function loadTheme({ env = process4.env, from = process4.cwd(), home = homedir2(
|
|
|
447
488
|
var current;
|
|
448
489
|
var currentKey = "";
|
|
449
490
|
function activeTheme() {
|
|
450
|
-
const key = `${
|
|
491
|
+
const key = `${process5.cwd()}\x00${process5.env.KIT_THEME ?? ""}\x00${process5.env.XDG_CONFIG_HOME ?? ""}`;
|
|
451
492
|
if (current === undefined || key !== currentKey) {
|
|
452
493
|
current = loadTheme();
|
|
453
494
|
currentKey = key;
|
|
@@ -456,20 +497,20 @@ function activeTheme() {
|
|
|
456
497
|
}
|
|
457
498
|
|
|
458
499
|
// ../kit/src/style.ts
|
|
459
|
-
function takesControlCodes(stream, env =
|
|
460
|
-
return stream.isTTY === true && env.NO_COLOR === undefined && env.NODE_DISABLE_COLORS === undefined && env.TERM !== "dumb";
|
|
500
|
+
function takesControlCodes(stream, env = process6.env) {
|
|
501
|
+
return stream.isTTY === true && env.NO_COLOR === undefined && env.NODE_DISABLE_COLORS === undefined && env.TERM !== "dumb" && outputMode(stream, env) === "terminal";
|
|
461
502
|
}
|
|
462
|
-
function paint(stream, env =
|
|
503
|
+
function paint(stream, env = process6.env) {
|
|
463
504
|
return le({ stream: { isTTY: takesControlCodes(stream, env) }, env });
|
|
464
505
|
}
|
|
465
|
-
function role(name, stream, env =
|
|
506
|
+
function role(name, stream, env = process6.env) {
|
|
466
507
|
const colors = paint(stream, env);
|
|
467
|
-
return takesControlCodes(stream, env) ? tinted(colors, activeTheme().theme.colors[name]) : colors;
|
|
508
|
+
return takesControlCodes(stream, env) ? tinted(colors, activeTheme().theme.colors[name], env) : colors;
|
|
468
509
|
}
|
|
469
510
|
var STATUS_WORDS = {
|
|
470
511
|
passed: ["passed", "success", "succeeded", "ok"],
|
|
471
512
|
failed: ["failed", "failure", "error", "errored"],
|
|
472
|
-
running: ["running", "in_progress", "started"],
|
|
513
|
+
running: ["running", "in_progress", "started", "holding"],
|
|
473
514
|
queued: ["queued", "waiting", "pending", "created", "blocked", "scheduled"],
|
|
474
515
|
skipped: ["skipped", "cancelled", "canceled", "manual"]
|
|
475
516
|
};
|
|
@@ -481,29 +522,35 @@ function upTo(text, separator) {
|
|
|
481
522
|
return end === -1 ? text : text.slice(0, end);
|
|
482
523
|
}
|
|
483
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
|
+
}
|
|
484
531
|
|
|
485
532
|
// ../kit/src/help.ts
|
|
486
|
-
import
|
|
533
|
+
import process8 from "process";
|
|
487
534
|
var OPTIONAL = /\[[^\]]+\]/g;
|
|
488
535
|
var USAGE_PREFIX = /^usage:\s*/i;
|
|
489
536
|
var FULL_STOP = /\.$/;
|
|
490
|
-
function usageLine(usage, colors = paint(
|
|
537
|
+
function usageLine(usage, colors = paint(process8.stdout)) {
|
|
491
538
|
return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
|
|
492
539
|
}
|
|
493
|
-
function helpText(usage, options, colors = paint(
|
|
540
|
+
function helpText(usage, options, colors = paint(process8.stdout), { examples = [], replaces, exits = [] } = {}) {
|
|
494
541
|
return [usageLine(usage, colors), ...replacesRows(replaces, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exitRows(exits, colors), ...exampleRows(examples, colors)].join(`
|
|
495
542
|
`);
|
|
496
543
|
}
|
|
497
|
-
function exitRows(exits, colors = paint(
|
|
544
|
+
function exitRows(exits, colors = paint(process8.stdout)) {
|
|
498
545
|
if (exits.length === 0)
|
|
499
546
|
return [];
|
|
500
547
|
const width = Math.max(...exits.map(([code]) => String(code).length));
|
|
501
548
|
return ["", colors.bold("Exits:"), ...exits.map(([code, meaning]) => ` ${colors.cyan(String(code).padEnd(width))} ${meaning}`)];
|
|
502
549
|
}
|
|
503
|
-
function replacesRows(replaces, colors = paint(
|
|
504
|
-
return replaces === undefined ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
550
|
+
function replacesRows(replaces, colors = paint(process8.stdout)) {
|
|
551
|
+
return replaces === undefined || outputMode() === "agent" ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
505
552
|
}
|
|
506
|
-
function wantsHelpJson(env =
|
|
553
|
+
function wantsHelpJson(env = process8.env) {
|
|
507
554
|
return env.KIT_HELP === "json";
|
|
508
555
|
}
|
|
509
556
|
function description(usage, { examples = [], replaces, exits } = {}, options) {
|
|
@@ -522,12 +569,12 @@ function description(usage, { examples = [], replaces, exits } = {}, options) {
|
|
|
522
569
|
...options === undefined ? {} : { options: Object.entries(options).map(([name, { short, description }]) => ({ name, ...short === undefined ? {} : { short }, ...description === undefined ? {} : { description } })) }
|
|
523
570
|
};
|
|
524
571
|
}
|
|
525
|
-
function exampleRows(examples, colors = paint(
|
|
526
|
-
if (examples.length === 0)
|
|
572
|
+
function exampleRows(examples, colors = paint(process8.stdout)) {
|
|
573
|
+
if (examples.length === 0 || outputMode() === "agent")
|
|
527
574
|
return [];
|
|
528
575
|
return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
|
|
529
576
|
}
|
|
530
|
-
function optionRows(options, colors = paint(
|
|
577
|
+
function optionRows(options, colors = paint(process8.stdout)) {
|
|
531
578
|
const rows = Object.entries(options).map(([name, option]) => {
|
|
532
579
|
const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
|
|
533
580
|
const named = option.choices === undefined ? "<value>" : `<${option.choices.join("|")}>`;
|
|
@@ -561,6 +608,9 @@ function closest(name, names) {
|
|
|
561
608
|
|
|
562
609
|
// ../kit/src/args.ts
|
|
563
610
|
var DASHES = /^-+/;
|
|
611
|
+
function jsonOption(holds) {
|
|
612
|
+
return { type: "boolean", description: holds === undefined ? "print JSON" : `print JSON: ${holds}` };
|
|
613
|
+
}
|
|
564
614
|
class Exit extends Error {
|
|
565
615
|
code;
|
|
566
616
|
hint;
|
|
@@ -604,7 +654,7 @@ var OUTPUT_OPTIONS = {
|
|
|
604
654
|
color: { type: "string", description: "auto (on a terminal alone), always or never" },
|
|
605
655
|
plain: { type: "boolean", description: "plain text, no colour or spinner: --color never" }
|
|
606
656
|
};
|
|
607
|
-
function applyOutput(color, plain, env =
|
|
657
|
+
function applyOutput(color, plain, env = process9.env) {
|
|
608
658
|
const when = plain === true ? "never" : color ?? "auto";
|
|
609
659
|
if (when === "auto")
|
|
610
660
|
return;
|
|
@@ -781,13 +831,13 @@ var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
|
781
831
|
// ../kit/src/cli.ts
|
|
782
832
|
import { readFileSync as readFileSync2, realpathSync } from "fs";
|
|
783
833
|
import { dirname as dirname3, join as join5 } from "path";
|
|
784
|
-
import
|
|
834
|
+
import process11 from "process";
|
|
785
835
|
|
|
786
836
|
// ../kit/src/usage.ts
|
|
787
837
|
import { appendFileSync, mkdirSync } from "fs";
|
|
788
838
|
import { homedir as homedir3 } from "os";
|
|
789
839
|
import { dirname as dirname2, isAbsolute, join as join4 } from "path";
|
|
790
|
-
import
|
|
840
|
+
import process10 from "process";
|
|
791
841
|
|
|
792
842
|
// ../kit/src/logs.ts
|
|
793
843
|
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
@@ -801,14 +851,14 @@ var usageError;
|
|
|
801
851
|
function noteUsageError(message) {
|
|
802
852
|
usageError = message;
|
|
803
853
|
}
|
|
804
|
-
function stateHome(env =
|
|
854
|
+
function stateHome(env = process10.env) {
|
|
805
855
|
const dir = env.XDG_STATE_HOME;
|
|
806
856
|
return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
|
|
807
857
|
}
|
|
808
|
-
function usagePath(env =
|
|
858
|
+
function usagePath(env = process10.env) {
|
|
809
859
|
return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
810
860
|
}
|
|
811
|
-
function usageRecorded(env =
|
|
861
|
+
function usageRecorded(env = process10.env) {
|
|
812
862
|
const off = env.KAYNOOO_CLI_NO_USAGE;
|
|
813
863
|
return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
|
|
814
864
|
}
|
|
@@ -822,7 +872,7 @@ function usageEntry(bin, command, argv, code, now = new Date) {
|
|
|
822
872
|
usageError = undefined;
|
|
823
873
|
return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
|
|
824
874
|
}
|
|
825
|
-
function recordUsage(entry, env =
|
|
875
|
+
function recordUsage(entry, env = process10.env) {
|
|
826
876
|
if (!usageRecorded(env))
|
|
827
877
|
return;
|
|
828
878
|
try {
|
|
@@ -840,8 +890,8 @@ var LEVEL_OPTIONS = {
|
|
|
840
890
|
};
|
|
841
891
|
var TOP_OPTIONS = { ...LEVEL_OPTIONS, version: { type: "boolean", short: "v", description: "print the version" } };
|
|
842
892
|
function printError(prefix, message, hint, cause) {
|
|
843
|
-
const colors = paint(
|
|
844
|
-
const styled = takesControlCodes(
|
|
893
|
+
const colors = paint(process11.stderr);
|
|
894
|
+
const styled = takesControlCodes(process11.stderr);
|
|
845
895
|
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
846
896
|
const under = (label, text, tint) => text === undefined ? "" : `
|
|
847
897
|
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
@@ -934,7 +984,7 @@ function packageVersion(entry = Bun.main) {
|
|
|
934
984
|
return "0.0.0";
|
|
935
985
|
}
|
|
936
986
|
}
|
|
937
|
-
async function runMain({ name, version, run, redact, usage }, argv =
|
|
987
|
+
async function runMain({ name, version, run, redact, usage }, argv = process11.argv.slice(2)) {
|
|
938
988
|
if (argv[0] === COMPLETE)
|
|
939
989
|
return printCandidates(leafCandidates(run, argv.slice(1)));
|
|
940
990
|
if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
|
|
@@ -1061,6 +1111,14 @@ var KEYS = {
|
|
|
1061
1111
|
"\x1B[B": "down",
|
|
1062
1112
|
"\x1BOB": "down",
|
|
1063
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",
|
|
1064
1122
|
"\r": "enter",
|
|
1065
1123
|
"\n": "enter",
|
|
1066
1124
|
"\x03": "cancel",
|
|
@@ -1138,7 +1196,7 @@ function parseKeys(input, paste = { active: false }, ctrls = []) {
|
|
|
1138
1196
|
});
|
|
1139
1197
|
}
|
|
1140
1198
|
function createState(rows, { multi = false, prompt = "> ", query = "", header = "", expect = [], searched = Number.POSITIVE_INFINITY } = {}) {
|
|
1141
|
-
const state = { rows: [], items: [], styles: [], searchable: [], searched, header, expect, key: "", status: "", query, matches: [], cursor: 0, marked: new Set, multi, prompt, done: undefined };
|
|
1199
|
+
const state = { rows: [], items: [], styles: [], searchable: [], searched, header, expect, key: "", status: "", query, matches: [], cursor: 0, page: 10, marked: new Set, multi, prompt, done: undefined };
|
|
1142
1200
|
return withRows(state, rows);
|
|
1143
1201
|
}
|
|
1144
1202
|
function withRows(state, rows, header = state.header) {
|
|
@@ -1170,6 +1228,14 @@ function press(state, key) {
|
|
|
1170
1228
|
return move(state, -1);
|
|
1171
1229
|
case "down":
|
|
1172
1230
|
return move(state, 1);
|
|
1231
|
+
case "pageup":
|
|
1232
|
+
return move(state, -state.page);
|
|
1233
|
+
case "pagedown":
|
|
1234
|
+
return move(state, state.page);
|
|
1235
|
+
case "home":
|
|
1236
|
+
return move(state, -state.matches.length);
|
|
1237
|
+
case "end":
|
|
1238
|
+
return move(state, state.matches.length);
|
|
1173
1239
|
case "cancel":
|
|
1174
1240
|
return { ...state, done: "cancel" };
|
|
1175
1241
|
case "erase":
|
|
@@ -1200,11 +1266,11 @@ function chosen(state) {
|
|
|
1200
1266
|
|
|
1201
1267
|
// ../kit/src/picker/terminal.ts
|
|
1202
1268
|
import { openSync } from "fs";
|
|
1203
|
-
import
|
|
1269
|
+
import process14 from "process";
|
|
1204
1270
|
import tty from "tty";
|
|
1205
1271
|
|
|
1206
1272
|
// ../kit/src/picker/preview.ts
|
|
1207
|
-
import
|
|
1273
|
+
import process13 from "process";
|
|
1208
1274
|
|
|
1209
1275
|
// ../kit/src/shell.ts
|
|
1210
1276
|
var SEPARATORS = new Set([`
|
|
@@ -1273,7 +1339,7 @@ async function runPreview(template, line, signal, hidden = []) {
|
|
|
1273
1339
|
${previewCommand(template, line, hidden)}`], { stdin: "ignore", stdout: "pipe", stderr: "ignore", detached: true });
|
|
1274
1340
|
const stop = () => {
|
|
1275
1341
|
try {
|
|
1276
|
-
|
|
1342
|
+
process13.kill(-proc.pid, "SIGKILL");
|
|
1277
1343
|
} catch {
|
|
1278
1344
|
proc.kill();
|
|
1279
1345
|
}
|
|
@@ -1361,7 +1427,8 @@ function listLines(state, layout, theme) {
|
|
|
1361
1427
|
const marks = (state.marked.size > 0 ? ` (${state.marked.size} marked)` : "") + (state.status === "" ? "" : ` ${state.status}`);
|
|
1362
1428
|
const promptWidth = Bun.stringWidth(state.prompt);
|
|
1363
1429
|
const query = fit(state.query, Math.max(1, layout.listWidth - promptWidth), identity);
|
|
1364
|
-
const
|
|
1430
|
+
const place = state.matches.length > rows ? ` (${state.cursor + 1} of ${state.matches.length})` : "";
|
|
1431
|
+
const count = fit(` ${state.matches.length}/${state.items.length}${place}${marks}`, layout.listWidth, theme.label);
|
|
1365
1432
|
const lines = [{ text: theme.accent(state.prompt) + query.text, width: promptWidth + query.width }, count, ...header];
|
|
1366
1433
|
if (state.matches.length === 0)
|
|
1367
1434
|
lines.push(fit(" no match", layout.listWidth, theme.label));
|
|
@@ -1388,6 +1455,7 @@ function render(state, layout, theme, preview = []) {
|
|
|
1388
1455
|
|
|
1389
1456
|
// ../kit/src/picker/terminal.ts
|
|
1390
1457
|
var PREVIEW_DELAY_MS = 120;
|
|
1458
|
+
var ESCAPE_WAIT_MS = 30;
|
|
1391
1459
|
var OPEN = { ctrl: "open" };
|
|
1392
1460
|
var HIDE_CURSOR = "\x1B[?25l";
|
|
1393
1461
|
var SHOW_CURSOR = "\x1B[?25h";
|
|
@@ -1405,7 +1473,7 @@ function openTerminal() {
|
|
|
1405
1473
|
} catch {
|
|
1406
1474
|
return fail("no terminal to read keys from (/dev/tty)");
|
|
1407
1475
|
}
|
|
1408
|
-
const out =
|
|
1476
|
+
const out = process14.stderr;
|
|
1409
1477
|
let resize;
|
|
1410
1478
|
return {
|
|
1411
1479
|
get columns() {
|
|
@@ -1443,20 +1511,29 @@ async function select(rows, settings, terminal = openTerminal()) {
|
|
|
1443
1511
|
let shownLine;
|
|
1444
1512
|
let finish;
|
|
1445
1513
|
let restored = false;
|
|
1514
|
+
let escape;
|
|
1515
|
+
let caret;
|
|
1446
1516
|
function draw(clear = false) {
|
|
1447
1517
|
if (restored)
|
|
1448
1518
|
return;
|
|
1449
1519
|
const layout = layoutFor({ columns: terminal.columns, rows: terminal.rows }, settings.height, settings.preview !== undefined, settings.previewPosition);
|
|
1450
1520
|
let frame = HIDE_CURSOR;
|
|
1451
1521
|
if (reserved !== layout.height || clear) {
|
|
1522
|
+
frame += clear && caret !== undefined ? up(Math.floor((caret - 1) / terminal.columns)) : "";
|
|
1452
1523
|
frame += CLEAR_BELOW + `
|
|
1453
1524
|
`.repeat(layout.height - 1) + up(layout.height - 1);
|
|
1454
1525
|
reserved = layout.height;
|
|
1455
1526
|
}
|
|
1527
|
+
caret = caretColumn(state, layout);
|
|
1456
1528
|
frame += `\r${render(state, layout, theme, preview).map((line) => `\x1B[2K${line}`).join(`
|
|
1457
|
-
`)}${up(layout.height - 1)}\r\x1B[${
|
|
1529
|
+
`)}${up(layout.height - 1)}\r\x1B[${caret}G${SHOW_CURSOR}`;
|
|
1458
1530
|
terminal.write(frame);
|
|
1459
1531
|
}
|
|
1532
|
+
function pageSize() {
|
|
1533
|
+
const headerLines = state.header === "" ? 0 : state.header.split(`
|
|
1534
|
+
`).length;
|
|
1535
|
+
return Math.max(1, layoutFor({ columns: terminal.columns, rows: terminal.rows }, settings.height, settings.preview !== undefined, settings.previewPosition).listHeight - 2 - headerLines);
|
|
1536
|
+
}
|
|
1460
1537
|
function refreshPreview() {
|
|
1461
1538
|
const at = highlighted(state);
|
|
1462
1539
|
if (settings.preview === undefined || at === shownLine)
|
|
@@ -1486,6 +1563,7 @@ async function select(rows, settings, terminal = openTerminal()) {
|
|
|
1486
1563
|
return;
|
|
1487
1564
|
restored = true;
|
|
1488
1565
|
clearTimeout(previewTimer);
|
|
1566
|
+
clearTimeout(escape);
|
|
1489
1567
|
previewing.abort();
|
|
1490
1568
|
terminal.write(`${CLEAR_BELOW}${PASTE_OFF}${SHOW_CURSOR}`);
|
|
1491
1569
|
terminal.close();
|
|
@@ -1524,7 +1602,7 @@ async function select(rows, settings, terminal = openTerminal()) {
|
|
|
1524
1602
|
if (key === OPEN || typeof key === "object" && "ctrl" in key && key.ctrl === reload?.key)
|
|
1525
1603
|
await reloadRows();
|
|
1526
1604
|
else
|
|
1527
|
-
state = press(state, key);
|
|
1605
|
+
state = press({ ...state, page: pageSize() }, key);
|
|
1528
1606
|
}
|
|
1529
1607
|
} finally {
|
|
1530
1608
|
draining = false;
|
|
@@ -1537,6 +1615,20 @@ async function select(rows, settings, terminal = openTerminal()) {
|
|
|
1537
1615
|
draw();
|
|
1538
1616
|
}
|
|
1539
1617
|
function onChunk(chunk) {
|
|
1618
|
+
if (escape !== undefined) {
|
|
1619
|
+
clearTimeout(escape);
|
|
1620
|
+
escape = undefined;
|
|
1621
|
+
chunk = `\x1B${chunk}`;
|
|
1622
|
+
} else if (chunk === "\x1B") {
|
|
1623
|
+
escape = setTimeout(() => {
|
|
1624
|
+
escape = undefined;
|
|
1625
|
+
takeKeys("\x1B");
|
|
1626
|
+
}, ESCAPE_WAIT_MS);
|
|
1627
|
+
return;
|
|
1628
|
+
}
|
|
1629
|
+
takeKeys(chunk);
|
|
1630
|
+
}
|
|
1631
|
+
function takeKeys(chunk) {
|
|
1540
1632
|
const keys = parseKeys(chunk, paste, [...settings.expect ?? [], ...reload?.key === undefined ? [] : [reload.key]]);
|
|
1541
1633
|
if (busy && keys.includes("cancel")) {
|
|
1542
1634
|
stop();
|
|
@@ -1548,9 +1640,9 @@ async function select(rows, settings, terminal = openTerminal()) {
|
|
|
1548
1640
|
const ended = new Promise((resolve) => {
|
|
1549
1641
|
finish = resolve;
|
|
1550
1642
|
});
|
|
1551
|
-
|
|
1643
|
+
process14.once("exit", restore);
|
|
1552
1644
|
for (const signal of SIGNALS)
|
|
1553
|
-
|
|
1645
|
+
process14.once(signal, stop);
|
|
1554
1646
|
try {
|
|
1555
1647
|
terminal.write(PASTE_ON);
|
|
1556
1648
|
terminal.listen(onChunk, () => draw(true));
|
|
@@ -1563,8 +1655,8 @@ async function select(rows, settings, terminal = openTerminal()) {
|
|
|
1563
1655
|
await ended;
|
|
1564
1656
|
} finally {
|
|
1565
1657
|
for (const signal of SIGNALS)
|
|
1566
|
-
|
|
1567
|
-
|
|
1658
|
+
process14.off(signal, stop);
|
|
1659
|
+
process14.off("exit", restore);
|
|
1568
1660
|
restore();
|
|
1569
1661
|
}
|
|
1570
1662
|
return state.done === "accept" ? { rows: chosen(state), key: state.key, query: state.query } : undefined;
|
|
@@ -1587,14 +1679,25 @@ Type to filter: the query matches as a subsequence, best matches first (a word s
|
|
|
1587
1679
|
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).
|
|
1588
1680
|
--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.
|
|
1589
1681
|
|
|
1590
|
-
|
|
1682
|
+
Without a terminal on stderr there is nobody to choose: it prints the first line containing the query (case-insensitive), every one with --multi.
|
|
1683
|
+
|
|
1684
|
+
Exits 0 when it printed a choice, 1 when cancelled, nothing matches or there are no lines, 2 on a usage error.`;
|
|
1591
1685
|
var OPTIONS = {
|
|
1592
1686
|
multi: { type: "boolean", short: "m", description: "tab marks several lines, enter prints them" },
|
|
1593
1687
|
prompt: { type: "string", short: "p", description: "the prompt before the query (> )" },
|
|
1594
1688
|
query: { type: "string", short: "q", description: "the query to start with" },
|
|
1595
1689
|
preview: { type: "string", description: "a command shown for the highlighted line, {} standing for it" },
|
|
1596
|
-
height: { type: "string", description: "lines or a percentage of the terminal to draw in (40%)" }
|
|
1690
|
+
height: { type: "string", description: "lines or a percentage of the terminal to draw in (40%)" },
|
|
1691
|
+
json: jsonOption("{picked: [lines]}, the choice or the marked lines in input order")
|
|
1597
1692
|
};
|
|
1693
|
+
function print(chosen, json) {
|
|
1694
|
+
if (json)
|
|
1695
|
+
printJson({ picked: chosen });
|
|
1696
|
+
else
|
|
1697
|
+
process15.stdout.write(`${chosen.join(`
|
|
1698
|
+
`)}
|
|
1699
|
+
`);
|
|
1700
|
+
}
|
|
1598
1701
|
async function main(args) {
|
|
1599
1702
|
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 {}'"] });
|
|
1600
1703
|
if (positionals.length > 0)
|
|
@@ -1602,20 +1705,24 @@ async function main(args) {
|
|
|
1602
1705
|
const height = values.height === undefined ? DEFAULT_HEIGHT : parseHeight(values.height);
|
|
1603
1706
|
if (height === undefined)
|
|
1604
1707
|
badUsage(`--height takes a number of lines or a percentage such as 40%, not ${values.height}`);
|
|
1605
|
-
if (
|
|
1606
|
-
fail("pick draws on stderr and it is not a terminal");
|
|
1607
|
-
if (process13.stdin.isTTY)
|
|
1708
|
+
if (process15.stdin.isTTY)
|
|
1608
1709
|
badUsage("pipe the lines to pick on stdin");
|
|
1609
1710
|
const lines = (await Bun.stdin.text()).split(`
|
|
1610
1711
|
`).filter((line) => line !== "");
|
|
1611
1712
|
if (lines.length === 0)
|
|
1612
1713
|
fail("nothing to pick from", 1);
|
|
1714
|
+
if (!process15.stderr.isTTY) {
|
|
1715
|
+
const query = (values.query ?? "").toLowerCase();
|
|
1716
|
+
const matching = lines.filter((line) => line.toLowerCase().includes(query));
|
|
1717
|
+
if (matching.length === 0)
|
|
1718
|
+
return 1;
|
|
1719
|
+
print(values.multi === true ? matching : matching.slice(0, 1), values.json === true);
|
|
1720
|
+
return 0;
|
|
1721
|
+
}
|
|
1613
1722
|
const picked = await choose(lines, { multi: values.multi === true, prompt: values.prompt ?? "> ", query: values.query ?? "", height, preview: values.preview });
|
|
1614
1723
|
if (picked === undefined)
|
|
1615
1724
|
return 1;
|
|
1616
|
-
|
|
1617
|
-
`)}
|
|
1618
|
-
`);
|
|
1725
|
+
print(picked, values.json === true);
|
|
1619
1726
|
return 0;
|
|
1620
1727
|
}
|
|
1621
|
-
|
|
1728
|
+
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.5.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.25.0"
|
|
39
39
|
}
|
|
40
40
|
}
|