Skip to content

Commit

Permalink
fix confirm and toggle components logic
Browse files Browse the repository at this point in the history
  • Loading branch information
blefnk committed Dec 7, 2024
1 parent 1c3a6dc commit 12c113c
Show file tree
Hide file tree
Showing 37 changed files with 57 additions and 113 deletions.
2 changes: 0 additions & 2 deletions examples/2-mono.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// 2-mono-example.ts: A fun example of a quiz game. Inspired by CLI-game created by Fireship. The example demonstrates how to use a mono prompt() component.

import relinka from "@reliverse/relinka";

import { prompt } from "~/components/mono/mono.js";
import { spinner } from "~/components/spinner/index.js";
import { createAsciiArt } from "~/components/visual/ascii-art/ascii-art.js";
Expand Down
2 changes: 0 additions & 2 deletions examples/5-args-a.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import relinka from "@reliverse/relinka";

import { defineCommand, createMain } from "~/main.js";

const command = defineCommand({
Expand Down
3 changes: 1 addition & 2 deletions examples/6-args-b.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import relinka from "@reliverse/relinka";

import { defineCommand, runMain } from "~/main.js";

import packageJson from "../package.json" with { type: "json" };
import relinka from "@reliverse/relinka";

const main = defineCommand({
meta: {
Expand Down
2 changes: 0 additions & 2 deletions examples/src/commands/build.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import relinka from "@reliverse/relinka";

import { defineCommand } from "~/main.js";

export default defineCommand({
Expand Down
2 changes: 0 additions & 2 deletions examples/src/commands/debug.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import relinka from "@reliverse/relinka";

import { defineCommand } from "~/main.js";

export default defineCommand({
Expand Down
2 changes: 0 additions & 2 deletions examples/src/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import relinka from "@reliverse/relinka";

import { defineCommand } from "~/main.js";

export default defineCommand({
Expand Down
7 changes: 2 additions & 5 deletions examples/src/prompts-new.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import relinka from "@reliverse/relinka";
import { detect } from "detect-package-manager";
import { emojify } from "node-emoji";
import pc from "picocolors";
Expand Down Expand Up @@ -542,17 +541,15 @@ export async function showTogglePrompt() {
const result = await togglePrompt({
title: "[togglePrompt] Do you like @reliverse/prompts library?",
options: ["Yes", "No"],
initial: "Yes",
});

const agree = result === "Yes";
msg({
type: "M_INFO",
title: "Your response:",
content: agree ? "You like it! 🥰" : "You don't like it... 😔",
content: result ? "You like it! 🥰" : "You don't like it... 😔",
});

return agree;
return result;
}

export async function showConfirmPrompt(
Expand Down
10 changes: 5 additions & 5 deletions examples/src/prompts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import relinka from "@reliverse/relinka";
import { detect } from "detect-package-manager";
import { emojify } from "node-emoji";
import pc from "picocolors";
Expand Down Expand Up @@ -535,17 +534,18 @@ export async function showTogglePrompt(): Promise<UserInput["toggle"]> {
const result = await togglePrompt({
title: "[togglePrompt] Do you like @reliverse/prompts library?",
options: ["Yes", "No"],
initial: "Yes",
defaultValue: "Yes",
});

const agree = result === "Yes";
msg({
type: "M_INFO",
title: "Your response:",
content: agree ? "You like it! 🥰" : "You don't like it... 😔",
titleColor: "bgCyan",
content: result ? "You like it! 🥰" : "You don't like it... 😔",
contentColor: result ? "greenBright" : "redBright",
});

return agree;
return result;
}

export async function showConfirmPrompt(
Expand Down
1 change: 0 additions & 1 deletion examples/src/simple/checkbox.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import relinka from "@reliverse/relinka";
import * as url from "node:url";

import { checkbox, Separator } from "~/components/prompts/index.js";
Expand Down
1 change: 0 additions & 1 deletion examples/src/simple/confirm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import relinka from "@reliverse/relinka";
import * as url from "node:url";

import { confirm } from "~/components/prompts/index.js";
Expand Down
1 change: 0 additions & 1 deletion examples/src/simple/editor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import relinka from "@reliverse/relinka";
import * as url from "node:url";

import { editor } from "~/components/prompts/index.js";
Expand Down
1 change: 0 additions & 1 deletion examples/src/simple/expand.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import relinka from "@reliverse/relinka";
import * as url from "node:url";

import { expand } from "~/components/prompts/index.js";
Expand Down
1 change: 0 additions & 1 deletion examples/src/simple/input.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import relinka from "@reliverse/relinka";
import * as url from "node:url";
import pc from "picocolors";

Expand Down
1 change: 0 additions & 1 deletion examples/src/simple/number.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import relinka from "@reliverse/relinka";
import * as url from "node:url";

import { number } from "~/components/prompts/index.js";
Expand Down
1 change: 0 additions & 1 deletion examples/src/simple/password.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import relinka from "@reliverse/relinka";
import * as url from "node:url";

import { password } from "~/components/prompts/index.js";
Expand Down
1 change: 0 additions & 1 deletion examples/src/simple/rawlist.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import relinka from "@reliverse/relinka";
import * as url from "node:url";

import { rawlist, Separator } from "~/components/prompts/index.js";
Expand Down
1 change: 0 additions & 1 deletion examples/src/simple/search.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import relinka from "@reliverse/relinka";
import { promises as fs } from "node:fs";
import path from "node:path";
import * as url from "node:url";
Expand Down
1 change: 0 additions & 1 deletion examples/src/simple/select.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import relinka from "@reliverse/relinka";
import * as url from "node:url";

import { select, Separator } from "~/components/prompts/index.js";
Expand Down
1 change: 0 additions & 1 deletion examples/src/simple/timeout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import relinka from "@reliverse/relinka";
import * as url from "node:url";

import { input } from "~/components/prompts/index.js";
Expand Down
2 changes: 1 addition & 1 deletion jsr.jsonc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reliverse/prompts",
"version": "1.3.12",
"version": "1.3.13",
"author": "blefnk",
"license": "MIT",
"exports": "./dist-jsr/main.ts",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reliverse/prompts",
"version": "1.3.12",
"version": "1.3.13",
"author": "blefnk",
"type": "module",
"description": "@reliverse/prompts is a powerful library that enables seamless, typesafe, and resilient prompts for command-line applications. Crafted with simplicity and elegance, it provides developers with an intuitive and robust way to build interactive CLIs.",
Expand Down
30 changes: 10 additions & 20 deletions src/components/confirm/confirm-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { countLines, deleteLastLines } from "~/utils/terminal.js";

export type ConfirmPromptOptions = {
title: string;
defaultValue?: boolean;
defaultValue?: boolean; // true = Y, false = N
content?: string;
titleColor?: ColorName;
titleTypography?: TypographyName;
Expand All @@ -27,12 +27,6 @@ export type ConfirmPromptOptions = {
action?: () => Promise<void>;
};

/**
* Prompts the user for a yes/no confirmation.
*
* @param options - Configuration options for the confirmation prompt.
* @returns A promise that resolves to a boolean based on user input.
*/
export async function confirmPrompt(
options: ConfirmPromptOptions,
): Promise<boolean> {
Expand All @@ -52,6 +46,9 @@ export async function confirmPrompt(
action,
} = options;

// If no defaultValue is passed, default to true (Y)
const effectiveDefault = defaultValue !== undefined ? defaultValue : true;

const rl = readline.createInterface({ input, output });

let linesToDelete = 0;
Expand Down Expand Up @@ -79,14 +76,8 @@ export async function confirmPrompt(
errorMessage,
});

let defaultHint = "";
if (defaultValue) {
defaultHint = "[Y/n]";
} else if (!defaultValue) {
defaultHint = "[y/N]";
} else {
defaultHint = "[y/n]";
}
// Adjust the default hint based on defaultValue
const defaultHint = effectiveDefault ? "[Y/n]" : "[y/N]";

const fullPrompt = `${question}${colorize(defaultHint, hintColor)}: `;

Expand All @@ -102,14 +93,13 @@ export async function confirmPrompt(
const answer = (await rl.question(formattedPrompt)).toLowerCase().trim();

let value: boolean;

const formattedBar = bar({ borderColor });

if (!answer && defaultValue !== undefined) {
// Inject the used answer into the console
const injectedAnswer = defaultValue ? "y" : "n";
if (!answer) {
// User pressed Enter, use the default value
const injectedAnswer = effectiveDefault ? "y" : "n";
process.stdout.write(`${formattedBar} ${injectedAnswer}\n`);
value = defaultValue;
value = effectiveDefault;
} else if (answer === "y" || answer === "yes") {
value = true;
} else if (answer === "n" || answer === "no") {
Expand Down
3 changes: 1 addition & 2 deletions src/components/flags/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import relinka from "@reliverse/relinka";

import type { ArgsDef, CommandDef } from "./types.js";

import { CLIError } from "./_utils.js";
import { resolveSubCommand, runCommand } from "./command.js";
import { showUsage as _showUsage } from "./usage.js";
import relinka from "@reliverse/relinka";

export type RunMainOptions = {
rawArgs?: string[];
Expand Down
2 changes: 1 addition & 1 deletion src/components/flags/usage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import relinka from "@reliverse/relinka";
import pc from "picocolors";

import type { ArgsDef, CommandDef } from "./types.js";

import { formatLineColumns, resolveValue } from "./_utils.js";
import { resolveArgs } from "./args.js";
import relinka from "@reliverse/relinka";

export async function showUsage<T extends ArgsDef = ArgsDef>(
cmd: CommandDef<T>,
Expand Down
1 change: 0 additions & 1 deletion src/components/input/input-main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { TSchema } from "@sinclair/typebox";

import relinka from "@reliverse/relinka";
import { Value } from "@sinclair/typebox/value";
import { stdin as input, stdout as output } from "node:process";
import readline from "node:readline/promises";
Expand Down
1 change: 0 additions & 1 deletion src/components/multiselect/multiselect-main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import relinka from "@reliverse/relinka";
import { stdin as input, stdout as output } from "node:process";
import readline from "node:readline";
import pc from "picocolors";
Expand Down
1 change: 0 additions & 1 deletion src/components/multiselect/num-multiselect-main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import relinka from "@reliverse/relinka";
import { stdin as input, stdout as output } from "process";
import readline from "readline/promises";

Expand Down
1 change: 0 additions & 1 deletion src/components/number/number-main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { TSchema, Static } from "@sinclair/typebox";

import relinka from "@reliverse/relinka";
import { Value } from "@sinclair/typebox/value";
import { stdin as input, stdout as output } from "node:process";
import readline from "node:readline/promises";
Expand Down
1 change: 0 additions & 1 deletion src/components/password/password-main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// import type { Static, TSchema } from "@sinclair/typebox";

import relinka from "@reliverse/relinka";
import { Value } from "@sinclair/typebox/value";

import type { ColorName, PromptOptions } from "~/types/general.js";
Expand Down
3 changes: 1 addition & 2 deletions src/components/progressbar/helper.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import relinka from "@reliverse/relinka";

import { msg } from "~/utils/messages.js";

import type { ProgressBarOptions } from "./ProgressBar.js";

import { ProgressBar } from "./ProgressBar.js";
import relinka from "@reliverse/relinka";

/**
* Options for the progressbar helper function.
Expand Down
1 change: 0 additions & 1 deletion src/components/select/num-select-main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import relinka from "@reliverse/relinka";
import { stdin as input, stdout as output } from "process";
import readline from "readline/promises";

Expand Down
1 change: 0 additions & 1 deletion src/components/select/select-main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import relinka from "@reliverse/relinka";
import { stdin as input, stdout as output } from "node:process";
import readline from "node:readline";
import pc from "picocolors";
Expand Down
2 changes: 1 addition & 1 deletion src/components/select/select-two.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Key } from "node:readline";

import relinka from "@reliverse/relinka";
import { Value } from "@sinclair/typebox/value";
import { stdout } from "node:process";
import pc from "picocolors";
Expand All @@ -10,6 +9,7 @@ import type { PromptOptions } from "~/types/general.js";
import { useKeyPress } from "~/components/core/useKeyPress.js";
import { colorize } from "~/utils/colorize.js";
import { resetCursorAndClear, moveCursorAndClear } from "~/utils/readline.js";
import relinka from "@reliverse/relinka";

type SelectPromptOptions = PromptOptions & {
defaultValue?: string;
Expand Down
3 changes: 1 addition & 2 deletions src/components/st-end/start.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import relinka from "@reliverse/relinka";

import type { PromptOptions } from "~/types/general.js";

import { getCurrentTerminalName } from "~/main.js";
import { msg } from "~/utils/messages.js";
import { pkg, pm, pmv } from "~/utils/platforms.js";
import relinka from "@reliverse/relinka";

export async function startPrompt({
title = "",
Expand Down
Loading

0 comments on commit 12c113c

Please sign in to comment.