@kaynooo/cli-pick 0.9.2 → 0.10.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 +145 -21
- 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 (100%) |
|
|
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-
|
|
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), left and right (with ctrl or alt, by a word), ctrl-a and ctrl-e move the caret in the query, ctrl-w or ctrl-backspace deletes a word before it, ctrl-u and ctrl-k erase back to the start and on to the end, delete the character under it, ctrl-/ or f1 opens a legend of the keys until the next key. The count line ends in a `ctrl-/ keys` hint, and the characters a query matched are underlined.
|
|
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
|
@@ -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
|
}
|
|
@@ -840,6 +843,97 @@ var SECRET_WORDS = new Set(["key", "pass", "pwd", "auth", "salt", "cookie", "ses
|
|
|
840
843
|
var {Glob } = globalThis.Bun;
|
|
841
844
|
var SESSION_FILES = new Glob("*.json");
|
|
842
845
|
var ACCOUNT_DIRS = new Glob("*/sessions");
|
|
846
|
+
// ../kit/src/picker/line.ts
|
|
847
|
+
var EDIT_KEYS = new Set(["left", "right", "wordleft", "wordright", "linestart", "lineend", "erase", "delete", "word", "killword", "killnext", "clear", "killend"]);
|
|
848
|
+
function isEditKey(key) {
|
|
849
|
+
return EDIT_KEYS.has(key);
|
|
850
|
+
}
|
|
851
|
+
var WORD_CHARACTER = /[\p{L}\p{N}_]/u;
|
|
852
|
+
var SPACE = /\s/;
|
|
853
|
+
function isWord(char) {
|
|
854
|
+
return char !== undefined && WORD_CHARACTER.test(char);
|
|
855
|
+
}
|
|
856
|
+
function wordStart(chars, from) {
|
|
857
|
+
let at = from;
|
|
858
|
+
while (at > 0 && !isWord(chars[at - 1]))
|
|
859
|
+
at--;
|
|
860
|
+
while (at > 0 && isWord(chars[at - 1]))
|
|
861
|
+
at--;
|
|
862
|
+
return at;
|
|
863
|
+
}
|
|
864
|
+
function wordEnd(chars, from) {
|
|
865
|
+
let at = from;
|
|
866
|
+
while (at < chars.length && !isWord(chars[at]))
|
|
867
|
+
at++;
|
|
868
|
+
while (at < chars.length && isWord(chars[at]))
|
|
869
|
+
at++;
|
|
870
|
+
return at;
|
|
871
|
+
}
|
|
872
|
+
function spacedStart(chars, from) {
|
|
873
|
+
let at = from;
|
|
874
|
+
while (at > 0 && SPACE.test(chars[at - 1] ?? ""))
|
|
875
|
+
at--;
|
|
876
|
+
while (at > 0 && !SPACE.test(chars[at - 1] ?? ""))
|
|
877
|
+
at--;
|
|
878
|
+
return at;
|
|
879
|
+
}
|
|
880
|
+
function cut(chars, from, to) {
|
|
881
|
+
return { text: [...chars.slice(0, from), ...chars.slice(to)].join(""), caret: from };
|
|
882
|
+
}
|
|
883
|
+
function editLine({ text, caret }, key) {
|
|
884
|
+
const chars = [...text];
|
|
885
|
+
const at = Math.min(Math.max(caret, 0), chars.length);
|
|
886
|
+
if (typeof key === "object") {
|
|
887
|
+
const typed = [...key.text];
|
|
888
|
+
return { text: [...chars.slice(0, at), ...typed, ...chars.slice(at)].join(""), caret: at + typed.length };
|
|
889
|
+
}
|
|
890
|
+
switch (key) {
|
|
891
|
+
case "left":
|
|
892
|
+
return { text, caret: Math.max(at - 1, 0) };
|
|
893
|
+
case "right":
|
|
894
|
+
return { text, caret: Math.min(at + 1, chars.length) };
|
|
895
|
+
case "wordleft":
|
|
896
|
+
return { text, caret: wordStart(chars, at) };
|
|
897
|
+
case "wordright":
|
|
898
|
+
return { text, caret: wordEnd(chars, at) };
|
|
899
|
+
case "linestart":
|
|
900
|
+
return { text, caret: 0 };
|
|
901
|
+
case "lineend":
|
|
902
|
+
return { text, caret: chars.length };
|
|
903
|
+
case "erase":
|
|
904
|
+
return cut(chars, Math.max(at - 1, 0), at);
|
|
905
|
+
case "delete":
|
|
906
|
+
return cut(chars, at, Math.min(at + 1, chars.length));
|
|
907
|
+
case "word":
|
|
908
|
+
return cut(chars, spacedStart(chars, at), at);
|
|
909
|
+
case "killword":
|
|
910
|
+
return cut(chars, wordStart(chars, at), at);
|
|
911
|
+
case "killnext":
|
|
912
|
+
return cut(chars, at, wordEnd(chars, at));
|
|
913
|
+
case "clear":
|
|
914
|
+
return cut(chars, 0, at);
|
|
915
|
+
case "killend":
|
|
916
|
+
return cut(chars, at, chars.length);
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
function visible({ text, caret }, room) {
|
|
920
|
+
const chars = [...text];
|
|
921
|
+
const at = Math.min(Math.max(caret, 0), chars.length);
|
|
922
|
+
const cells = at === chars.length ? [...chars, " "] : chars;
|
|
923
|
+
const widths = cells.map((char) => Bun.stringWidth(char));
|
|
924
|
+
const width = (from, to) => widths.slice(from, to).reduce((sum, size) => sum + size, 0);
|
|
925
|
+
const fits = (from, to) => (from > 0 ? 1 : 0) + width(from, to) + (to < cells.length ? 1 : 0) <= room;
|
|
926
|
+
let start = at;
|
|
927
|
+
let end = at + 1;
|
|
928
|
+
while (start > 0 && fits(start - 1, end))
|
|
929
|
+
start--;
|
|
930
|
+
while (end < cells.length && fits(start, end + 1))
|
|
931
|
+
end++;
|
|
932
|
+
const left = start > 0 ? "\u2026" : "";
|
|
933
|
+
const right = end < cells.length ? "\u2026" : "";
|
|
934
|
+
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) };
|
|
935
|
+
}
|
|
936
|
+
|
|
843
937
|
// ../kit/src/picker/score.ts
|
|
844
938
|
var MATCH = 16;
|
|
845
939
|
var CONSECUTIVE = 12;
|
|
@@ -932,7 +1026,6 @@ var RESET = /^\x1B\[0?m$/;
|
|
|
932
1026
|
var COLUMN_GAP = 2;
|
|
933
1027
|
var PASTE_START = "\x1B[200~";
|
|
934
1028
|
var PASTE_END = "\x1B[201~";
|
|
935
|
-
var LAST_WORD = /\S*\s*$/;
|
|
936
1029
|
var KEY_SEQUENCES = /\x1B\[<[0-9;]*[A-Za-z]|\x1B\[[0-9;]*[A-Z~]|\x1BO[A-Z]|\x1B[\s\S]|[\s\S]/gu;
|
|
937
1030
|
var MOUSE = /^\x1B\[<(\d+);\d+;\d+[a-z]$/i;
|
|
938
1031
|
var WHEEL_LINES = 3;
|
|
@@ -955,9 +1048,26 @@ var KEYS = {
|
|
|
955
1048
|
"\n": "enter",
|
|
956
1049
|
"\x03": "cancel",
|
|
957
1050
|
"\x1B": "cancel",
|
|
1051
|
+
"\x1B[D": "left",
|
|
1052
|
+
"\x1BOD": "left",
|
|
1053
|
+
"\x1B[C": "right",
|
|
1054
|
+
"\x1BOC": "right",
|
|
1055
|
+
"\x1B[1;5D": "wordleft",
|
|
1056
|
+
"\x1B[1;3D": "wordleft",
|
|
1057
|
+
"\x1Bb": "wordleft",
|
|
1058
|
+
"\x1B[1;5C": "wordright",
|
|
1059
|
+
"\x1B[1;3C": "wordright",
|
|
1060
|
+
"\x1Bf": "wordright",
|
|
1061
|
+
"\x01": "linestart",
|
|
1062
|
+
"\x05": "lineend",
|
|
958
1063
|
"\x7F": "erase",
|
|
959
|
-
"\
|
|
1064
|
+
"\x1B[3~": "delete",
|
|
1065
|
+
"\b": "killword",
|
|
1066
|
+
"\x1B\x7F": "killword",
|
|
1067
|
+
"\x1B[3;5~": "killnext",
|
|
1068
|
+
"\x1Bd": "killnext",
|
|
960
1069
|
"\x15": "clear",
|
|
1070
|
+
"\v": "killend",
|
|
961
1071
|
"\x17": "word",
|
|
962
1072
|
"\t": "tab",
|
|
963
1073
|
"\x1F": "help",
|
|
@@ -1034,7 +1144,7 @@ function parseKeys(input, paste = { active: false }, ctrls = []) {
|
|
|
1034
1144
|
});
|
|
1035
1145
|
}
|
|
1036
1146
|
function createState(rows, { multi = false, prompt = "> ", query = "", header = "", expect = [], searched = Number.POSITIVE_INFINITY } = {}) {
|
|
1037
|
-
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 };
|
|
1147
|
+
const state = { rows: [], items: [], styles: [], searchable: [], searched, header, expect, key: "", status: "", query, caret: [...query].length, matches: [], cursor: 0, help: false, page: 10, marked: new Set, multi, prompt, done: undefined };
|
|
1038
1148
|
return withRows(state, rows);
|
|
1039
1149
|
}
|
|
1040
1150
|
function withRows(state, rows, header = state.header) {
|
|
@@ -1044,8 +1154,9 @@ function withRows(state, rows, header = state.header) {
|
|
|
1044
1154
|
const matches = rank(state.query, searchable);
|
|
1045
1155
|
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 };
|
|
1046
1156
|
}
|
|
1047
|
-
function withQuery(state,
|
|
1048
|
-
|
|
1157
|
+
function withQuery(state, key) {
|
|
1158
|
+
const { text, caret } = editLine({ text: state.query, caret: state.caret }, key);
|
|
1159
|
+
return text === state.query ? { ...state, caret } : { ...state, query: text, caret, matches: rank(text, state.searchable), cursor: 0 };
|
|
1049
1160
|
}
|
|
1050
1161
|
function move(state, by) {
|
|
1051
1162
|
return { ...state, cursor: Math.max(0, Math.min(state.matches.length - 1, state.cursor + by)) };
|
|
@@ -1060,9 +1171,11 @@ function press(state, key) {
|
|
|
1060
1171
|
return { ...state, help: false };
|
|
1061
1172
|
if (typeof key === "object") {
|
|
1062
1173
|
if ("text" in key)
|
|
1063
|
-
return withQuery(state,
|
|
1174
|
+
return withQuery(state, key);
|
|
1064
1175
|
return state.expect.includes(key.ctrl) ? { ...state, done: "accept", key: key.ctrl } : state;
|
|
1065
1176
|
}
|
|
1177
|
+
if (isEditKey(key))
|
|
1178
|
+
return withQuery(state, key);
|
|
1066
1179
|
switch (key) {
|
|
1067
1180
|
case "up":
|
|
1068
1181
|
return move(state, -1);
|
|
@@ -1080,12 +1193,6 @@ function press(state, key) {
|
|
|
1080
1193
|
return { ...state, done: "cancel" };
|
|
1081
1194
|
case "help":
|
|
1082
1195
|
return { ...state, help: true };
|
|
1083
|
-
case "erase":
|
|
1084
|
-
return withQuery(state, [...state.query].slice(0, -1).join(""));
|
|
1085
|
-
case "clear":
|
|
1086
|
-
return withQuery(state, "");
|
|
1087
|
-
case "word":
|
|
1088
|
-
return withQuery(state, state.query.replace(LAST_WORD, ""));
|
|
1089
1196
|
case "enter":
|
|
1090
1197
|
return state.matches.length === 0 && state.marked.size === 0 ? state : { ...state, done: "accept" };
|
|
1091
1198
|
case "tab": {
|
|
@@ -1111,6 +1218,14 @@ import { openSync } from "fs";
|
|
|
1111
1218
|
import process15 from "process";
|
|
1112
1219
|
import tty from "tty";
|
|
1113
1220
|
|
|
1221
|
+
// ../kit/src/redraw.ts
|
|
1222
|
+
var SYNC_START = "\x1B[?2026h";
|
|
1223
|
+
var SYNC_END = "\x1B[?2026l";
|
|
1224
|
+
function overwrite(lines, columns) {
|
|
1225
|
+
return lines.map((line) => textWidth(line) % columns === 0 ? `\x1B[2K${line}` : `${line}\x1B[K`).join(`
|
|
1226
|
+
`);
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1114
1229
|
// ../kit/src/picker/preview.ts
|
|
1115
1230
|
import process14 from "process";
|
|
1116
1231
|
|
|
@@ -1257,13 +1372,19 @@ function fitStyled(line, width, plain = identity) {
|
|
|
1257
1372
|
const { text, styles } = styled(line);
|
|
1258
1373
|
return fit(text, width, plain, plain, new Set, styles);
|
|
1259
1374
|
}
|
|
1260
|
-
function
|
|
1261
|
-
return
|
|
1375
|
+
function queryShown(state, layout) {
|
|
1376
|
+
return visible({ text: state.query, caret: state.caret }, Math.max(1, layout.listWidth - Bun.stringWidth(state.prompt)));
|
|
1377
|
+
}
|
|
1378
|
+
function caretCell(state, layout) {
|
|
1379
|
+
const shown = queryShown(state, layout);
|
|
1380
|
+
return { column: Math.min(layout.listWidth, Bun.stringWidth(state.prompt) + shown.column + 1), char: shown.at ?? " " };
|
|
1262
1381
|
}
|
|
1263
1382
|
function helpLines(state, layout, theme) {
|
|
1264
1383
|
const lines = [
|
|
1265
1384
|
"type to filter, up, down, ctrl-p, ctrl-n, pgup, pgdn, home, end, the wheel move",
|
|
1266
|
-
|
|
1385
|
+
"left, right, ctrl-a, ctrl-e move in the query, ctrl or alt with left or right by a word",
|
|
1386
|
+
"ctrl-w or ctrl-backspace erase a word, ctrl-u back to the start, ctrl-k to the end, del ahead",
|
|
1387
|
+
`enter picks${state.multi ? ", tab marks" : ""}, esc or ctrl-c cancels`,
|
|
1267
1388
|
...state.expect.length > 0 ? [`${state.expect.join(", ")} end the pick as this command asks`] : []
|
|
1268
1389
|
];
|
|
1269
1390
|
const title = "keys, any key closes this help";
|
|
@@ -1279,7 +1400,9 @@ function listLines(state, layout, theme) {
|
|
|
1279
1400
|
const top = Math.max(0, Math.min(state.cursor - Math.floor(rows / 2), state.matches.length - rows));
|
|
1280
1401
|
const marks = (state.marked.size > 0 ? ` (${state.marked.size} marked)` : "") + (state.status === "" ? "" : ` ${state.status}`);
|
|
1281
1402
|
const promptWidth = Bun.stringWidth(state.prompt);
|
|
1282
|
-
const
|
|
1403
|
+
const shown = queryShown(state, layout);
|
|
1404
|
+
const text = `${shown.before}${shown.at ?? ""}${shown.after}`;
|
|
1405
|
+
const query = { text, width: Bun.stringWidth(text) };
|
|
1283
1406
|
const place = state.matches.length > rows ? ` (${state.cursor + 1} of ${state.matches.length})` : "";
|
|
1284
1407
|
const count = fit(` ${state.matches.length}/${state.items.length}${place}${marks} ctrl-/ keys`, layout.listWidth, theme.label);
|
|
1285
1408
|
const lines = [{ text: theme.accent(state.prompt) + query.text, width: promptWidth + query.width }, count, ...header];
|
|
@@ -1312,7 +1435,8 @@ var ESCAPE_WAIT_MS = 30;
|
|
|
1312
1435
|
var OPEN = { ctrl: "open" };
|
|
1313
1436
|
var HIDE_CURSOR = "\x1B[?25l";
|
|
1314
1437
|
var SHOW_CURSOR = "\x1B[?25h";
|
|
1315
|
-
var
|
|
1438
|
+
var REVERSE = "\x1B[7m";
|
|
1439
|
+
var REVERSE_END = "\x1B[27m";
|
|
1316
1440
|
var PASTE_ON = "\x1B[?2004h";
|
|
1317
1441
|
var PASTE_OFF = "\x1B[?2004l";
|
|
1318
1442
|
var MOUSE_ON = "\x1B[?1000h\x1B[?1006h";
|
|
@@ -1383,14 +1507,14 @@ async function select(rows, settings, terminal = openTerminal()) {
|
|
|
1383
1507
|
if (restored)
|
|
1384
1508
|
return;
|
|
1385
1509
|
const layout = layoutFor({ columns: terminal.columns, rows: terminal.rows }, settings.height, settings.preview !== undefined, settings.previewPosition);
|
|
1386
|
-
let frame = HIDE_CURSOR;
|
|
1510
|
+
let frame = SYNC_START + HIDE_CURSOR;
|
|
1387
1511
|
if (reserved !== layout.height || clear) {
|
|
1388
1512
|
frame += CLEAR_BELOW + `
|
|
1389
1513
|
`.repeat(layout.height - 1) + up(layout.height - 1);
|
|
1390
1514
|
reserved = layout.height;
|
|
1391
1515
|
}
|
|
1392
|
-
|
|
1393
|
-
|
|
1516
|
+
const caret = caretCell(state, layout);
|
|
1517
|
+
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}`;
|
|
1394
1518
|
terminal.write(frame);
|
|
1395
1519
|
}
|
|
1396
1520
|
function pageSize() {
|
|
@@ -1539,7 +1663,7 @@ async function choose(lines, settings) {
|
|
|
1539
1663
|
// src/pick.ts
|
|
1540
1664
|
var USAGE = `pick [options]: a fuzzy selector, the lines on stdin in, the chosen one on stdout out
|
|
1541
1665
|
|
|
1542
|
-
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-
|
|
1666
|
+
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, left, right, ctrl-a and ctrl-e move in the query (ctrl or alt for a word), ctrl-u and ctrl-k erase back to its start and on to its end, ctrl-/ or f1 shows the keys.
|
|
1543
1667
|
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).
|
|
1544
1668
|
--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.
|
|
1545
1669
|
|
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.10.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.39.0"
|
|
39
39
|
}
|
|
40
40
|
}
|