@odarino/haren 1.2.1 → 1.2.2
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/index.js +68 -22
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -959,29 +959,75 @@ async function prompt(question, defaultValue) {
|
|
|
959
959
|
const value = await readLine();
|
|
960
960
|
return value || defaultValue || "";
|
|
961
961
|
}
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
for (const
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
}
|
|
962
|
+
function parseSelectKeys(data) {
|
|
963
|
+
const keys = [];
|
|
964
|
+
for (const [token] of data.matchAll(/\x1b\[[A-D]|\x1bO[A-D]|\r\n|[\s\S]/g)) {
|
|
965
|
+
if (token === "\x1B[A" || token === "\x1BOA")
|
|
966
|
+
keys.push("up");
|
|
967
|
+
else if (token === "\x1B[B" || token === "\x1BOB")
|
|
968
|
+
keys.push("down");
|
|
969
|
+
else if (token === "\r" || token === `
|
|
970
|
+
` || token === `\r
|
|
971
|
+
`)
|
|
972
|
+
keys.push("enter");
|
|
973
|
+
else if (token === "\x03")
|
|
974
|
+
keys.push("abort");
|
|
975
|
+
}
|
|
976
|
+
return keys;
|
|
977
|
+
}
|
|
978
|
+
function moveSelection(index, key, count) {
|
|
979
|
+
return key === "up" ? (index - 1 + count) % count : (index + 1) % count;
|
|
980
|
+
}
|
|
981
|
+
function listRows(lines, columns) {
|
|
982
|
+
const width = columns && columns > 0 ? columns : Infinity;
|
|
983
|
+
return lines.reduce((rows, l) => {
|
|
984
|
+
const visible = l.replace(/\x1b\[[0-9;?]*[A-Za-z]|\r/g, "").length;
|
|
985
|
+
return rows + Math.max(1, Math.ceil(visible / width));
|
|
986
|
+
}, 0);
|
|
976
987
|
}
|
|
977
|
-
function
|
|
978
|
-
const
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
988
|
+
async function promptSelect(question, options, descriptions, defaultValue) {
|
|
989
|
+
const out = process.stdout;
|
|
990
|
+
const stdin = process.stdin;
|
|
991
|
+
let index = Math.max(0, options.indexOf(defaultValue));
|
|
992
|
+
const line = (i) => {
|
|
993
|
+
const opt = options[i];
|
|
994
|
+
const marker = i === index ? "\x1B[36m>\x1B[0m" : " ";
|
|
995
|
+
const label = i === index ? `\x1B[1m${opt}\x1B[0m` : opt;
|
|
996
|
+
return `\r\x1B[2K ${marker} ${label} \x1B[2m\u2014 ${descriptions[opt]}\x1B[0m`;
|
|
997
|
+
};
|
|
998
|
+
const draw = () => out.write(options.map((_, i) => line(i)).join(`
|
|
999
|
+
`));
|
|
1000
|
+
out.write(` \x1B[36m?\x1B[0m ${question} \x1B[2m(\u2191/\u2193, Enter)\x1B[0m
|
|
1001
|
+
\x1B[?25l`);
|
|
1002
|
+
draw();
|
|
1003
|
+
return new Promise((resolve) => {
|
|
1004
|
+
const finish = (abort) => {
|
|
1005
|
+
stdin.removeListener("data", onData);
|
|
1006
|
+
stdin.setRawMode(false);
|
|
1007
|
+
stdin.pause();
|
|
1008
|
+
out.write(`\x1B[?25h
|
|
1009
|
+
`);
|
|
1010
|
+
if (abort)
|
|
1011
|
+
process.exit(130);
|
|
1012
|
+
resolve(options[index]);
|
|
1013
|
+
};
|
|
1014
|
+
const onData = (data) => {
|
|
1015
|
+
for (const key of parseSelectKeys(data.toString())) {
|
|
1016
|
+
if (key === "abort")
|
|
1017
|
+
return finish(true);
|
|
1018
|
+
if (key === "enter")
|
|
1019
|
+
return finish(false);
|
|
1020
|
+
index = moveSelection(index, key, options.length);
|
|
1021
|
+
const up = listRows(options.map((_, i) => line(i)), out.columns) - 1;
|
|
1022
|
+
if (up > 0)
|
|
1023
|
+
out.write(`\x1B[${up}A`);
|
|
1024
|
+
draw();
|
|
1025
|
+
}
|
|
1026
|
+
};
|
|
1027
|
+
stdin.setRawMode(true);
|
|
1028
|
+
stdin.resume();
|
|
1029
|
+
stdin.on("data", onData);
|
|
1030
|
+
});
|
|
985
1031
|
}
|
|
986
1032
|
function parseRepoSelection(value, count, defaults) {
|
|
987
1033
|
const answer = value.trim().toLowerCase();
|