Skip to content

Commit

Permalink
improve end prompt and borders logic
Browse files Browse the repository at this point in the history
  • Loading branch information
blefnk committed Jan 14, 2025
1 parent 893fee8 commit 458e56a
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 56 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/src/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const pkg = packageJson;

// const pkg = {
// name: "@reliverse/prompts",
// version: "1.4.6",
// version: "1.4.7",
// 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
4 changes: 2 additions & 2 deletions jsr.jsonc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reliverse/prompts",
"version": "1.4.6",
"version": "1.4.7",
"author": "blefnk",
"license": "MIT",
"exports": "./dist-jsr/main.ts",
Expand Down Expand Up @@ -34,4 +34,4 @@
"cspell.json"
]
}
}
}
28 changes: 10 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reliverse/prompts",
"version": "1.4.6",
"version": "1.4.7",
"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 Expand Up @@ -42,22 +42,14 @@
"url": "https://github.com/reliverse/prompts/issues",
"email": "[email protected]"
},
"files": [
"package.json",
"README.md",
"LICENSE.md",
"dist-npm"
],
"files": ["package.json", "README.md", "LICENSE.md", "dist-npm"],
"homepage": "https://github.com/reliverse/prompts",
"keywords": [
"cli",
"reliverse"
],
"keywords": ["cli", "reliverse"],
"license": "MIT",
"dependencies": {
"@alcalzone/ansi-tokenize": "^0.1.3",
"@figliolia/chalk-animation": "^1.0.4",
"@reliverse/relinka": "^1.2.5",
"@reliverse/relinka": "^1.2.8",
"@sinclair/typebox": "^0.34.13",
"ansi-diff-stream": "^1.2.1",
"ansi-escapes": "^7.0.0",
Expand Down Expand Up @@ -115,22 +107,22 @@
"zod": "^3.24.1"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.17.2",
"@arethetypeswrong/cli": "^0.17.3",
"@biomejs/biome": "1.9.4",
"@cspell/dict-npm": "^5.1.22",
"@eslint/js": "^9.18.0",
"@eslint/json": "^0.9.0",
"@eslint/markdown": "^6.2.1",
"@faker-js/faker": "^9.3.0",
"@stylistic/eslint-plugin": "^2.12.1",
"@stylistic/eslint-plugin": "^2.13.0",
"@types/ansi-diff-stream": "^1.2.3",
"@types/bun": "^1.1.16",
"@types/chalk-animation": "^1.6.3",
"@types/eslint__js": "^8.42.3",
"@types/figlet": "^1.7.0",
"@types/fs-extra": "^11.0.4",
"@types/mute-stream": "^0.0.4",
"@types/node": "^22.10.5",
"@types/node": "^22.10.6",
"@types/sentencer": "^0.2.3",
"@types/signal-exit": "^3.0.4",
"@types/strip-comments": "^2.0.4",
Expand All @@ -149,8 +141,8 @@
"strip-comments": "^2.0.1",
"tsx": "^4.19.2",
"typescript": "^5.7.3",
"typescript-eslint": "^8.19.1",
"unbuild": "^3.3.0",
"typescript-eslint": "^8.20.0",
"unbuild": "^3.3.1",
"vitest": "^2.1.8"
}
}
}
9 changes: 7 additions & 2 deletions src/confirm/confirm-main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { ColorName, TypographyName } from "@reliverse/relinka";
import type {
BorderColorName,
ColorName,
TypographyName,
} from "@reliverse/relinka";
import type { VariantName } from "@reliverse/relinka";

import { bar, msg } from "@reliverse/relinka";
Expand All @@ -20,7 +24,7 @@ export type ConfirmPromptOptions = {
contentColor?: ColorName;
contentTypography?: TypographyName;
contentVariant?: VariantName;
borderColor?: ColorName;
borderColor?: BorderColorName;
hintPlaceholderColor?: ColorName;
variantOptions?: any;
action?: () => Promise<void>;
Expand Down Expand Up @@ -248,6 +252,7 @@ export async function confirmPrompt(
}

return await completePrompt(
"confirm",
false,
endTitle,
endTitleColor,
Expand Down
6 changes: 4 additions & 2 deletions src/input/input-main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {
BorderColorName,
ColorName,
MsgType,
SymbolName,
Expand Down Expand Up @@ -44,7 +45,7 @@ function getMaskChar(customMask?: string): string {
*/
export type InputPromptOptions = {
border?: boolean;
borderColor?: ColorName;
borderColor?: BorderColorName;
content?: string;
contentColor?: ColorName;
contentTypography?: TypographyName;
Expand Down Expand Up @@ -78,7 +79,7 @@ export type InputPromptOptions = {

type RenderParams = {
border: boolean;
borderColor?: ColorName;
borderColor?: BorderColorName;
content?: string;
contentColor?: ColorName;
contentTypography?: TypographyName;
Expand Down Expand Up @@ -353,6 +354,7 @@ export async function inputPrompt(
*/
async function endPrompt(isCtrlC: boolean): Promise<void> {
await completePrompt(
"input",
isCtrlC,
endTitle,
endTitleColor,
Expand Down
6 changes: 4 additions & 2 deletions src/multiselect/multiselect-main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { VariantName } from "@reliverse/relinka";
import type { BorderColorName, VariantName } from "@reliverse/relinka";

import {
deleteLastLine,
Expand Down Expand Up @@ -38,7 +38,7 @@ type MultiselectPromptParams<T extends string> = {
content?: string;
options: (SelectOption<T> | SeparatorOption)[];
defaultValue?: T[];
borderColor?: ColorName;
borderColor?: BorderColorName;
titleColor?: ColorName;
titleTypography?: TypographyName;
titleVariant?: VariantName;
Expand Down Expand Up @@ -440,6 +440,7 @@ export async function multiselectPrompt<T extends string>(

async function endPrompt(isCtrlC = false) {
await completePrompt(
"multiselect",
isCtrlC,
endTitle,
endTitleColor,
Expand Down Expand Up @@ -471,6 +472,7 @@ export async function multiselectPrompt<T extends string>(

deleteLastLine();
await completePrompt(
"multiselect",
false,
endTitle,
endTitleColor,
Expand Down
4 changes: 2 additions & 2 deletions src/number/number-main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FmtMsgOptions } from "@reliverse/relinka";
import type { BorderColorName, FmtMsgOptions } from "@reliverse/relinka";

import {
bar,
Expand Down Expand Up @@ -30,7 +30,7 @@ type NumberPromptOptions = {
contentColor?: ColorName;
contentTypography?: TypographyName;
contentVariant?: VariantName;
borderColor?: ColorName;
borderColor?: BorderColorName;
variantOptions?: any;
endTitle?: string;
endTitleColor?: ColorName;
Expand Down
8 changes: 5 additions & 3 deletions src/select/select-main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { VariantName } from "@reliverse/relinka";
import type { BorderColorName, VariantName } from "@reliverse/relinka";

import { deleteLastLine, symbols } from "@reliverse/relinka";
import { msg, type ColorName, type TypographyName } from "@reliverse/relinka";
Expand Down Expand Up @@ -34,7 +34,7 @@ type SelectPromptParams<T extends string> = {
options: (SelectOption<T> | SeparatorOption)[];
defaultValue?: T;
required?: boolean;
borderColor?: ColorName;
borderColor?: BorderColorName;
titleColor?: ColorName;
titleTypography?: TypographyName;
titleVariant?: VariantName;
Expand Down Expand Up @@ -257,7 +257,7 @@ export async function selectPrompt<T extends string>(
contentTypography = "italic",
border = true,
endTitle = "",
endTitleColor = "retroGradient",
endTitleColor = "dim",
maxItems,
debug = false,
terminalWidth: customTerminalWidth = 90,
Expand Down Expand Up @@ -419,6 +419,7 @@ export async function selectPrompt<T extends string>(

deleteLastLine();
await completePrompt(
"select",
false,
endTitle,
endTitleColor,
Expand All @@ -433,6 +434,7 @@ export async function selectPrompt<T extends string>(

async function endPrompt(isCtrlC = false) {
await completePrompt(
"select",
isCtrlC,
endTitle,
endTitleColor,
Expand Down
4 changes: 2 additions & 2 deletions src/st-end/end.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function endPrompt({

if (titleAnimation) {
await animateText({
title,
title: title ? title : " ",
anim: titleAnimation,
delay: titleAnimationDelay,
type: "M_END",
Expand All @@ -37,7 +37,7 @@ export async function endPrompt({
} else {
msg({
type: "M_END",
title,
title: title ? title : " ",
titleColor,
titleTypography,
titleVariant,
Expand Down
9 changes: 7 additions & 2 deletions src/toggle/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { ColorName, TypographyName } from "@reliverse/relinka";
import type {
BorderColorName,
ColorName,
TypographyName,
} from "@reliverse/relinka";
import type { VariantName } from "@reliverse/relinka";

import { deleteLastLine, msg } from "@reliverse/relinka";
Expand All @@ -13,7 +17,7 @@ export type TogglePromptParams<T extends string> = {
content?: string;
options?: [T, T];
defaultValue?: T;
borderColor?: ColorName;
borderColor?: BorderColorName;
titleColor?: ColorName;
titleTypography?: TypographyName;
titleVariant?: VariantName;
Expand Down Expand Up @@ -263,6 +267,7 @@ export async function togglePrompt<T extends string>(
// Return boolean: first option = true, second option = false
const booleanValue = selectedIndex === 0;
void completePrompt(
"toggle",
false,
endTitle,
endTitleColor,
Expand Down
72 changes: 54 additions & 18 deletions src/utils/prompt-end.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,59 @@
import type { ColorName, TypographyName } from "@reliverse/relinka";
import type { VariantName } from "@reliverse/relinka";
import type { BorderColorName, VariantName } from "@reliverse/relinka";

import { msg } from "@reliverse/relinka";
import {
getExactTerminalWidth,
msg,
symbols,
type ColorName,
type TypographyName,
} from "@reliverse/relinka";
import pc from "picocolors";

/**
* Ends the prompt by optionally displaying an end message and running the action if confirmed.
* Preserves the last prompt state unless there's an endTitle.
*/
export async function completePrompt(
prompt: "input" | "confirm" | "select" | "multiselect" | "toggle",
isCtrlC: boolean,
endTitle = "",
endTitleColor: ColorName = "dim",
titleTypography: TypographyName = "none",
titleVariant: VariantName | undefined = undefined,
border = true,
borderColor: ColorName = "dim",
_endTitle = "",
_endTitleColor: ColorName = "dim",
_titleTypography: TypographyName = "none",
_titleVariant: VariantName | undefined = undefined,
_border = true,
borderColor: BorderColorName = "dim",
action?: () => Promise<void>,
value?: boolean,
): Promise<boolean> {
if (action && value) {
await action();
}

if (prompt === "input") {
renderEndLineInput();
return value ?? false;
}

if (isCtrlC) {
msg({
type: "M_END",
title: endTitle,
titleColor: endTitleColor,
titleTypography,
...(titleVariant ? { titleVariant } : {}),
border,
borderColor,
});
renderEndLine();
// if (endTitle !== "") {
// await endPrompt({
// title: endTitle,
// titleColor: endTitleColor,
// titleTypography,
// ...(titleVariant ? { titleVariant } : {}),
// border,
// });
// } else {
// await endPrompt({
// title: " ",
// titleColor: endTitleColor,
// titleTypography,
// ...(titleVariant ? { titleVariant } : {}),
// border,
// borderColor,
// });
// }
} else {
msg({
type: "M_BAR",
Expand All @@ -41,3 +63,17 @@ export async function completePrompt(

return value ?? false;
}

export function renderEndLine() {
const lineLength = getExactTerminalWidth() - 2;
console.log(pc.dim(symbols.middle));
console.log(pc.dim(`${symbols.end}${symbols.line.repeat(lineLength)}⊱`));
console.log();
}

export function renderEndLineInput() {
const lineLength = getExactTerminalWidth() - 2;
console.log();
console.log(pc.dim(`${symbols.end}${symbols.line.repeat(lineLength)}⊱`));
console.log();
}
2 changes: 1 addition & 1 deletion src/utils/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const pm = await detect();
export const pmv = await getNpmVersion(pm);
export const pkg = {
name: "@reliverse/prompts",
version: "1.4.6",
version: "1.4.7",
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.",
};
Loading

0 comments on commit 458e56a

Please sign in to comment.