💅
Denoで@cliffy/promptを色々試したサンプルメモ
DenoでCLIを作成する際に@clack/prompts
のようなものを探してみたところ、@cliffy/prompt
が使用できそうでした。公式サイトを見ると基本的な使い方は理解できるのですが、カスタマイズ例が十分に紹介されておらず、使用した結果がどのように見えるかもわかりませんでした。そのあたりを試した結果の備忘メモです。
前提
-
@cliffy/prompt
のバージョン@1.0.0-rc.7
で確認 -
@cliffy/ansi/colors
を使用する場合、事前に以下コマンド実行が必要deno add jsr:@cliffy/ansi@^1.0.0-rc.7
主な共通オプション
サンプルとしてInput
を使用しているが、全種類のプロンプトで使用可能(のはず)。
import { Input, type InputOptions } from "jsr:@cliffy/prompt@1.0.0-rc.7";
async function showPrompt(): Promise<string> {
const args: InputOptions = {
message: "general options",
default: "default text",
// hideDefault: true, don't show indicated default value
hint: "prompt description",
info: true,
suggestions: ["foo", "bar"],
transform: (value) => value.toUpperCase(),
};
return await Input.prompt(args);
}
const result = await showPrompt();
console.log("---------- values ----------");
console.log(result);
- 実行直後
-
arrow up
キー押下
-
tab
キー押下
- 入力クリア後、
enter
キー押下
-
foo
入力後、enter
キー押下
単一選択
Toggle
シンプルな使用
import { Toggle } from "jsr:@cliffy/prompt@1.0.0-rc.7";
async function showPrompt(): Promise<boolean> {
return await Toggle.prompt({ message: "simple toggle message" });
}
const result = await showPrompt();
console.log("---------- values ----------");
console.log(result);
- 実行直後
-
enter
キー押下
-
y
キー押下
-
enter
キー押下
全体的なカスタム
import { Toggle, type ToggleOptions } from "jsr:@cliffy/prompt@1.0.0-rc.7";
async function showPrompt(): Promise<boolean> {
const args: ToggleOptions = {
message: "custom toggle message",
active: "hello",
inactive: "goodbye",
};
return await Toggle.prompt(args);
}
const result = await showPrompt();
console.log("---------- values ----------");
console.log(result);
- 実行直後
-
g
キー押下
-
enter
キー押下
Confirm
単一選択に分類したが、自由入力に近い。
シンプルな使用
import { Confirm } from "jsr:@cliffy/prompt@1.0.0-rc.7";
async function showPrompt(): Promise<boolean> {
return await Confirm.prompt({ message: "simple confirm message" });
}
const result = await showPrompt();
console.log("---------- values ----------");
console.log(result);
- 実行直後
-
enter
キー押下
-
y
キー押下
-
enter
キー押下
全体的なカスタム
import { Confirm, type ConfirmOptions } from "jsr:@cliffy/prompt@1.0.0-rc.7";
async function showPrompt(): Promise<boolean> {
const args: ConfirmOptions = {
message: "custom confirm message",
active: "hello",
inactive: "goodbye",
};
return await Confirm.prompt(args);
}
const result = await showPrompt();
console.log("---------- values ----------");
console.log(result);
- 実行直後
-
g
キー押下
-
enter
キー押下
Select
シンプルな使用
import { Select, type SelectOptions } from "jsr:@cliffy/prompt@1.0.0-rc.7";
async function showPrompt(): Promise<string> {
const message = "simple select message";
const options = ["foo", "bar", "baz"];
const args: SelectOptions<string> = { message, options };
return await Select.prompt(args);
}
const result = await showPrompt();
console.log("---------- values ----------");
console.log(result);
- 実行直後
-
enter
キー押下
選択肢のカスタム
import { Select, type SelectOptions } from "jsr:@cliffy/prompt@1.0.0-rc.7";
async function showPrompt(): Promise<string | number> {
const message = "complex select message";
const options = [{
name: "alfa",
value: 1,
}, {
name: "bravo",
value: 2,
}, {
name: "group charlie",
options: ["delta", "echo", "foxtrot"],
}];
const args: SelectOptions<string | number> = { message, options };
return await Select.prompt(args);
}
const result = await showPrompt();
console.log("---------- values ----------");
console.log(result);
- 実行直後
-
group charlie
まで移動後、arrow right
キー押下
-
delta
まで移動後、enter
キー押下
複数選択
Checkbox
シンプルな使用
import { Checkbox, type CheckboxOptions } from "jsr:@cliffy/prompt@1.0.0-rc.7";
async function showPrompt(): Promise<string[]> {
const message = "simple checkbox message";
const options = ["foo", "bar", "baz"];
const args: CheckboxOptions<string> = { message, options };
return await Checkbox.prompt(args);
}
const result = await showPrompt();
console.log("---------- values ----------");
console.log(result);
- 実行直後
-
space
キー押下
-
enter
キー押下
-
enter
キー押下
選択肢のカスタム
import { Checkbox, type CheckboxOptions } from "jsr:@cliffy/prompt@1.0.0-rc.7";
async function showPrompt(): Promise<(string | number)[]> {
const message = "complex checkbox message";
const options = [{
name: "alfa",
value: 1,
}, {
name: "bravo",
value: 2,
checked: true,
}, {
name: "charlie",
value: 3,
icon: false,
}, {
name: "group delta",
options: ["echo", "foxtrot"],
}, {
name: "group golf",
options: ["hotel", "india"],
// checked: true, no effect
icon: false,
}];
const args: CheckboxOptions<string | number> = { message, options };
return await Checkbox.prompt(args);
}
const result = await showPrompt();
console.log("---------- values ----------");
console.log(result);
- 実行直後
-
group delta
まで移動後、space
キー押下
-
arrow right
キー押下
-
echo
まで移動後、space
キー押下
-
arrow left
キー2回押下
-
group golf
まで移動後、space
キー押下
-
hotel
まで移動後、space
キー押下
-
arrow left
キー2回押下後、enter
キー押下
-
enter
キー押下
全体的なカスタム
import { Checkbox, type CheckboxOptions } from "jsr:@cliffy/prompt@1.0.0-rc.7";
import { colors } from "@cliffy/ansi/colors";
async function showPrompt(): Promise<string[]> {
const args: CheckboxOptions<string> = {
message: "custom checkbox message",
options: [{ name: "group", options: ["foo", "bar", "baz"] }],
confirmSubmit: false,
check: colors.green("☑"),
uncheck: "☐",
partialCheck: colors.rgb24("▣", 0x8fbc8f),
minOptions: 1,
maxOptions: 2,
keys: {
check: ["space", "y"],
// checkAll: ["a"], no effect?
},
};
return await Checkbox.prompt(args);
}
const result = await showPrompt();
console.log("---------- values ----------");
console.log(result);
- 実行直後
-
enter
キー押下
-
space
キー押下後、enter
キー押下
-
arrow right
キーを押下しfoo
まで移動後、y
キー押下
-
enter
キー押下
自由入力
Input
シンプルな使用
import { Input } from "jsr:@cliffy/prompt@1.0.0-rc.7";
async function showPrompt(): Promise<string> {
return await Input.prompt({ message: "simple input message" });
}
const result = await showPrompt();
console.log("---------- values ----------");
console.log(result);
- 実行直後
-
foo,bar
を入力
-
enter
キー押下
全体的なカスタム
import { Input, type InputOptions } from "jsr:@cliffy/prompt@1.0.0-rc.7";
async function showPrompt(): Promise<string> {
const args: InputOptions = {
message: "custom input message",
minLength: 3,
maxLength: 4,
};
return await Input.prompt(args);
}
const result = await showPrompt();
console.log("---------- values ----------");
console.log(result);
- 実行直後
-
enter
キー押下
-
foo
を入力
-
enter
キー押下
Number
シンプルな使用
import { Number } from "jsr:@cliffy/prompt@1.0.0-rc.7";
async function showPrompt(): Promise<number> {
return await Number.prompt({ message: "simple number message" });
}
const result = await showPrompt();
console.log("---------- values ----------");
console.log(result);
- 実行直後
-
foo
を入力
-
123
を入力
-
enter
キー押下
全体的なカスタム
import { Number, type NumberOptions } from "jsr:@cliffy/prompt@1.0.0-rc.7";
async function showPrompt(): Promise<number> {
const args: NumberOptions = {
message: "custom number message",
float: true,
round: 2,
min: -1,
max: 1,
};
return await Number.prompt(args);
}
const result = await showPrompt();
console.log("---------- values ----------");
console.log(result);
- 実行直後
-
enter
キー押下
-
3
を入力後、enter
キー押下
- 入力をクリアし
-0.111
を入力後、enter
キー押下
Secret
シンプルな使用
import { Secret } from "jsr:@cliffy/prompt@1.0.0-rc.7";
async function showPrompt(): Promise<string> {
return await Secret.prompt({ message: "simple secret message" });
}
const result = await showPrompt();
console.log("---------- values ----------");
console.log(result);
- 実行直後
-
foo
を入力
-
enter
キー押下
全体的なカスタム
async function showPrompt(): Promise<string> {
const args: SecretOptions = {
message: "custom secret message",
label: "xxx",
hidden: true,
minLength: 3,
maxLength: 4,
};
return await Secret.prompt(args);
}
const result = await showPrompt();
console.log("---------- values ----------");
console.log(result);
- 実行直後
-
enter
キー押下
-
foo
を入力
-
enter
キー押下
複数自由入力
List
シンプルな使用
import { List } from "jsr:@cliffy/prompt@1.0.0-rc.7";
async function showPrompt(): Promise<string[]> {
return await List.prompt({ message: "simple list message" });
}
const result = await showPrompt();
console.log("---------- values ----------");
console.log(result);
- 実行直後
-
foo,bar,baz
を入力
-
enter
キー押下
全体的なカスタム
import { List, type ListOptions } from "jsr:@cliffy/prompt@1.0.0-rc.7";
async function showPrompt(): Promise<string[]> {
const args: ListOptions = {
message: "custom toggle message",
separator: ";",
minLength: 3,
maxLength: 3,
minTags: 1,
maxTags: 2,
};
return await List.prompt(args);
}
const result = await showPrompt();
console.log("---------- values ----------");
console.log(result);
- 実行直後
-
enter
キー押下
-
a
を入力後、enter
キー押下
-
backspace
,foo;bar
を入力後、enter
キー押下
雑記
今後spinnerが同梱されることを期待してます。
Discussion