Skip to content

Commit

Permalink
feat(confirm): fast answer (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreDemailly committed Jan 21, 2024
1 parent d763bde commit f227827
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
2 changes: 1 addition & 1 deletion demo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { question, confirm, select, multiselect, PromptAgent } from "./index.js";
import { question, confirm, select, multiselect, PromptAgent } from "./dist/index.js";

const kTestRunner = ["node", "tap", "tape", "vitest", "mocha", "ava"];

Expand Down
19 changes: 17 additions & 2 deletions src/prompts/confirm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const kToggleKeys = new Set([
export class ConfirmPrompt extends AbstractPrompt<boolean> {
initial: boolean;
selectedValue: boolean;
fastAnswer: boolean;
#boundKeyPressEvent: (...args: any) => void;
#boundExitEvent: (...args: any) => void;

Expand Down Expand Up @@ -91,7 +92,20 @@ export class ConfirmPrompt extends AbstractPrompt<boolean> {
this.selectedValue = !this.selectedValue;
}

this.#render();
if (key.name === "y") {
this.selectedValue = true;
resolve(true);
this.fastAnswer = true;
}
else if (key.name === "n") {
this.selectedValue = false;
resolve(false);
this.fastAnswer = true;
}

if (!this.fastAnswer) {
this.#render();
}
}

#onProcessExit() {
Expand All @@ -105,9 +119,10 @@ export class ConfirmPrompt extends AbstractPrompt<boolean> {
}

#onQuestionAnswer() {
const defaultLines = this.fastAnswer ? 0 : 1;
this.stdout.moveCursor(
-this.stdout.columns,
-(Math.floor(wcwidth(stripAnsi(this.#getQuestionQuery())) / this.stdout.columns) || 1)
-(Math.floor(wcwidth(stripAnsi(this.#getQuestionQuery())) / this.stdout.columns) || defaultLines)
);
this.stdout.clearScreenDown();
this.write(`${this.selectedValue ? SYMBOLS.Tick : SYMBOLS.Cross} ${kleur.bold(this.message)}${EOL}`);
Expand Down
38 changes: 37 additions & 1 deletion test/confirm-prompt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const kInputs = {
j: { name: "j" },
k: { name: "k" },
l: { name: "l" },
y: { name: "y" },
n: { name: "n" },
space: { name: "space" },
return: { name: "return" }
};
Expand Down Expand Up @@ -63,7 +65,7 @@ describe("ConfirmPrompt", () => {
});

for (const key of Object.keys(kInputs)) {
if (key === "return") {
if (["return", "y", "n"].includes(key)) {
continue;
}

Expand Down Expand Up @@ -126,4 +128,38 @@ describe("ConfirmPrompt", () => {
"✖ Foo"
]);
});

it("should return true when pressing 'y'", async() => {
const logs: string[] = [];
const confirmPrompt = await TestingPrompt.ConfirmPrompt("Foo", {
inputs: [kInputs.y],
onStdoutWrite: (log) => {
logs.push(log);
}
});
const input = await confirmPrompt.confirm();

assert.deepEqual(input, true);
assert.deepEqual(logs, [
"? Foo Yes/No",
"✔ Foo"
]);
});

it("should return false when pressing 'n'", async() => {
const logs: string[] = [];
const confirmPrompt = await TestingPrompt.ConfirmPrompt("Foo", {
inputs: [kInputs.n],
onStdoutWrite: (log) => {
logs.push(log);
}
});
const input = await confirmPrompt.confirm();

assert.deepEqual(input, false);
assert.deepEqual(logs, [
"? Foo Yes/No",
"✖ Foo"
]);
});
});

0 comments on commit f227827

Please sign in to comment.