Skip to content

Commit 012be50

Browse files
committed
Feat: Add choose
1 parent e461989 commit 012be50

File tree

6 files changed

+59
-7
lines changed

6 files changed

+59
-7
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ const resp = await ask("What is your name?");
101101
console.log(resp);
102102
```
103103

104+
`choose` prompts the user to choose from the provided options.
105+
106+
```ts
107+
const resp = await choose("Would you like a foo or a bar?", ["foo", "bar"]);
108+
console.log(resp);
109+
```
110+
104111
`sleep` sleeps for specified ms.
105112

106113
```ts

deps.ts

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export {
2828
Writable,
2929
} from "https://deno.land/[email protected]/node/stream.ts";
3030

31+
//
32+
// external packages
33+
//
34+
export * as input from "https://deno.land/x/[email protected]/index.ts";
35+
3136
//
3237
// internal
3338
//

examples/choose.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env vl
2+
3+
//
4+
// Copyright 2022 Joona Piirainen
5+
// MIT License
6+
//
7+
8+
import "../globals.d.ts";
9+
10+
const age = await choose(
11+
"How old are you?",
12+
Array.from({ length: 120 }).map((_, i) => `${i}`),
13+
);
14+
15+
await $`echo You are ${age} years old!`;

globals.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
$Internal,
88
askInternal,
99
cdInternal,
10+
chooseInternal,
1011
ColorsInternal,
1112
fsInternal,
1213
noThrowInternal,
@@ -24,6 +25,7 @@ declare global {
2425
const $: $Internal;
2526
const cd: typeof cdInternal;
2627
const ask: typeof askInternal;
28+
const choose: typeof chooseInternal;
2729
const fs: typeof fsInternal;
2830
const os: typeof osInternal;
2931
const rmrf: typeof rmrfInternal;

index.ts

+25-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
child_process,
99
ChildProcess,
1010
Colors,
11+
input,
1112
inspect,
1213
nodeFs as fsInternal,
1314
os as osInternal,
@@ -37,6 +38,7 @@ export const initEnv = () =>
3738
quiet: quietInternal,
3839
retry: retryInternal,
3940
startSpinner: startSpinnerInternal,
41+
choose: chooseInternal,
4042
});
4143

4244
export interface $Internal {
@@ -378,12 +380,31 @@ export const cdInternal = (path: string) => {
378380
Deno.chdir(path);
379381
};
380382

381-
export const askInternal = async (question: string) => {
382-
console.log(question);
383+
export const getLine = async () => {
383384
for await (const line of readLines(Deno.stdin)) {
384385
return line;
385386
}
386-
return "";
387+
throw new Error("EOL");
388+
};
389+
390+
const write = (text: string) =>
391+
Deno.stdout.write(new TextEncoder().encode(text));
392+
393+
export const writeLine = (text: string) => write(text + "\n");
394+
395+
export const askInternal = async (question: string) => {
396+
const loop = new input.default();
397+
return await loop.question(question);
398+
};
399+
400+
export const chooseInternal = async (question: string, choices: string[]) => {
401+
const loop = new input.default();
402+
await writeLine(question);
403+
const idx = (await loop.choose(choices)).findIndex(Boolean);
404+
if (idx === -1) {
405+
throw new Error("Answer not in the given choices.");
406+
}
407+
return choices[idx];
387408
};
388409

389410
export const rmrfInternal = async (path: string) =>
@@ -421,10 +442,7 @@ export const retryInternal = (count = 0, delay = 0) =>
421442

422443
export const startSpinnerInternal = (title = "") => {
423444
let i = 0;
424-
const spin = () =>
425-
Deno.stdout.write(
426-
new TextEncoder().encode(` ${"⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"[i++ % 10]} ${title}\r`),
427-
);
445+
const spin = () => write(` ${"⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"[i++ % 10]} ${title}\r`);
428446
return ((id) => ({
429447
stopSpinner: () => clearInterval(id),
430448
}))(setInterval(spin, 100));

scratch.mjs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env vl
2+
3+
const token = await ask("Do you have GitHub token in env? ");
4+
const fooOrBar = await choose("foo or bar? ", ["foo", "bar"]);
5+
console.log(fooOrBar, token);

0 commit comments

Comments
 (0)