promptify.ts is a modern, interactive command-line input utility for Node.js applications. It provides a rich and user-friendly experience for gathering various types of input, such as text, numbers, and selections, from the terminal. This library leverages the readline
module to handle input efficiently and supports custom formatting and validation.
- promptify.ts
To install promptify.ts
, use npm:
npm install promptify.ts
import { Input } from 'promptify.ts';
const input = new Input();
(async () => {
const response = await input.prompt({
type: "text",
q: "What is your name?",
required: true
});
console.log(response);
})();
const input = new Input();
(async () => {
const response = await input.prompt({
type: "number",
q: "Enter your age:",
required: true
});
console.log(response);
})();
const input = new Input();
(async () => {
const choices = ['Option 1', 'Option 2', 'Option 3'];
const response = await input.selection({
type: "single",
choices,
q: "Choose an option:"
});
console.log(response);
})();
const input = new Input();
(async () => {
const response = await input.pwd({
q: "Enter your password",
});
console.log(response);
})();
const input = new Input();
(async () => {
const response = await input.bool({
q: "Do you want to continue?",
default_bool: true
});
console.log(response);
})();
const input = new Input();
(async () => {
const response = await input.filedialog({
type: "file",
q: "Select your config file"
});
console.log(response);
})();
const input = new Input();
(async () => {
const response = await input.date({
q: "Select your favourite date"
});
console.log(response);
})();
const input = new Input();
(async () => {
const response = await input.url({
q: "Input your favourite youtube video",
protocol: "https"
});
console.log(response);
})();
The design
setting allows you to customize the appearance of the prompt. The available design options are:
Design.Simple
: A basic design with minimal styling.Design.Modern
: A stylish design with shadows and colors.Design.Colorful
: A vibrant design with multiple colors.
You can customize the colors used in the prompt box and its shadow:
box_color
: The color of the prompt box.shadow_color
: The color of the shadow effect.
Example configuration for design parameter:
{
header: Design.Modern,
body: Design.Modern,
colors: {
box_color: Colors.foreground.white,
shadow_color: Colors.foreground.gray
}
}
const input = new Input();
(async () => {
const response = await input.prompt({
type: "text",
q: "Enter your favorite color:",
format: "text",
design: {
header: Design.Modern,
body: Design.Modern,
colors: {
box_color: Colors.foreground.white,
shadow_color: Colors.foreground.gray
}
}
});
console.log(`Your favorite color is ${response}`);
})();
The prompt
method is used to gather text or number input from the user.
- type:
"text" | "number"
- The type of input to prompt for. - q:
string
- The question to display to the user. - required:
boolean
- Whether the input is required. - format:
"json" | "text"
- The format of the returned data (default: "json"). - design:
InputPromptDesignSettings
- Custom design settings for the prompt.
Promise<string | InputJsonOutput | null>
- The user's input in the specified format.
The selection
method is used to gather a selection from a list of choices.
- type:
"single"
- The type of selection (currently supports only "single"). - choices:
string[]
- The list of choices to present to the user. - q:
string
- The question to display to the user. - format:
"json" | "text"
- The format of the returned data (default: "json"). - design:
InputSelectionDesignSettings
- Custom design settings for the prompt.
Promise<string | InputJsonOutput | null>
- The selected choice in the specified format.
The pwd
method is used to gather a password.
- q:
string
- The question to display to the user. - required:
boolean
- Whether the input is required. - format:
"json" | "text"
- The format of the returned data (default: "json"). - design:
InputPwdDesignSettings
- Custom design settings for the prompt.
Promise<string | InputJsonOutput | null>
- The password in the specified format.
The bool
method is used to gather a boolean.
- q:
string
- The question to display to the user. - required:
boolean
- Whether the input is required. - default_bool:
boolean
- What the default boolean should be. - format:
"json" | "text"
- The format of the returned data (default: "json"). - design:
InputBoolDesignSettings
- Custom design settings for the prompt.
Promise<string | InputJsonOutput | null>
- The boolean in the specified format.
The filedialog
method is used to gather a file path.
- type:
string
- The type you want to gather ("file" or "folder") - q:
string
- The question or prompt to display to the user. - startPath:
string
- Where the dialog will start at (default: "__dirname" (current file path) ). - extensions:
string
- What extensions you want to filter. (default: "*", example: ".jpg,.png,.jpeg") - showHiddenFolders:
boolean
- Whether to show hidden folders (start with ".") (default: false). - format:
"json" | "text"
- The format of the returned data (default: "json"). - design:
InputFiledialogDesignSettings
- Custom design settings for the prompt.
Promise<string | InputJsonOutput | null>
- The file path in the specified format.
The date
method is used to gather a date.
- q:
string
- The question or prompt to display to the user. - startDate:
string
- Which date that should be selected by default (default: new Date()) - format:
"json" | "text"
- The format of the returned data (default: "json"). - design:
InputDateDesignSettings
- Custom design settings for the prompt.
Promise<string | InputJsonOutput | null>
- The date in the specified format.
The date
method is used to gather a url.
- q:
string
- The question or prompt to display to the user. - protocol:
UrlProtocol
- What protocol should be accepted (default: https(s) ) - format:
"json" | "text"
- The format of the returned data (default: "json"). - design:
InputUrlDesignSettings
- Custom design settings for the prompt.
Promise<string | InputJsonOutput | null>
- The url in the specified format.
The library supports formatting the output as either JSON or plain text. By default, the output is formatted as JSON, but this can be changed by setting the format
parameter in the methods.
The JSON format provides structured output with additional metadata.
Example:
{
"answer": "your_input_here",
"index": 1,
"choices": ["Option 1", "Option 2", "Option 3"]
}
Example for boolean:
{
"answer": "true",
"bool": true
}
The text format returns the raw input as a string.
Example:
your_input_here
const input = new Input();
(async () => {
const response = await input.prompt({
type: "text",
q: "Enter your favorite color:",
format: "text"
});
console.log(`Your favorite color is ${response}`);
})();
const input = new Input();
(async () => {
const choices = ['Red', 'Blue', 'Green'];
const response = await input.selection({
type: "single",
choices,
q: "Choose a color:",
format: "text"
});
console.log(`You chose ${response}`);
})();
const input = new Input();
(async () => {
const response = await input.pwd({
q: "Enter your password",
format: "text"
});
console.log(`Your password is ${response}`);
})();
const input = new Input();
(async () => {
const response = await input.bool({
q: "Do you want to continue?",
default_bool: true,
format: "text"
});
console.log(`Your boolean is ${response}`);
})();
const input = new Input();
(async () => {
const response = await input.filedialog({
type: "file",
q: "Choose your favourite image",
startPath: "E:/users/YOUR_USER/pictures",
showHiddenFolders: true,
extensions: ".png,.jpg,.jpeg,.avif",
format: "text"
});
console.log(`You chose ${response}`);
})();
This project is licensed under the MIT License.
By OGMatrix
Feel free to contribute, raise issues, or submit pull requests to improve this library. Happy coding!