Class

UserInput

UserInput(name)

Constructor

# new UserInput(name)

This class provides a flexible interface for creating and managing user input prompts. It implements the PromptObject interface from the 'prompts' library and offers methods to set various properties of the prompt. The class also includes static methods for common input scenarios and argument parsing.

Represents a user input prompt with various configuration options.

sequenceDiagram participant Client participant UserInput participant PromptLibrary Client->>UserInput: new UserInput(name) Client->>UserInput: setMessage(message) Client->>UserInput: setType(type) Client->>UserInput: setInitial(initial) Client->>UserInput: Other configuration methods Client->>UserInput: ask() UserInput->>PromptLibrary: prompts(question) PromptLibrary->>Client: Display prompt Client->>PromptLibrary: User provides input PromptLibrary->>UserInput: Return answers UserInput->>Client: Return processed answers
Parameters:
Name Type Description
name

The name of the prompt, used as the key in the returned answers object.

View Source input/input.ts, line 61

Example
```typescript
import { UserInput } from '@decaf-ts/utils';

// Create a simple text input
const nameInput = new UserInput('name')
  .setMessage('What is your name?')
  .setInitial('User');

// Create a number input with validation
const ageInput = new UserInput('age')
  .setType('number')
  .setMessage('How old are you?')
  .setMin(0)
  .setMax(120);

// Ask for input and process the results
async function getUserInfo() {
  const answers = await UserInput.ask([nameInput, ageInput]);
  console.log(`Hello ${answers.name}, you are ${answers.age} years old.`);
}

getUserInfo();
```

Classes

UserInput

This class provides a flexible interface for creating and managing user input prompts. It implements the PromptObject interface from the 'prompts' library and offers methods to set various properties of the prompt. The class also includes static methods for common input scenarios and argument parsing.

Members

# active

The style applied to the currently selected option.

The active option style for select inputs.

View Source input/input.ts, line 142

string | PrevCaller.<R, (string|Falsy)> | undefined

# active

The style applied to the currently selected option.

The active option style for select inputs.

View Source input/input.ts, line 828

# choices

An array of options that the user can select from in choice-based prompts.

The available choices for select, multiselect, or autocomplete inputs.

View Source input/input.ts, line 152

Array | PrevCaller.<R, (Array|Falsy)> | undefined

# choices

An array of options that the user can select from in choice-based prompts.

The available choices for select, multiselect, or autocomplete inputs.

View Source input/input.ts, line 840

# float

If true, allows decimal numbers.

Whether to allow float values for number inputs.

View Source input/input.ts, line 117

boolean | PrevCaller.<R, (boolean|Falsy)> | undefined

# float

If true, allows decimal numbers.

Whether to allow float values for number inputs.

View Source input/input.ts, line 798

# format

A function to format the user's input before it's returned.

The format function for the input.

View Source input/input.ts, line 92

PrevCaller.<R, void> | undefined

# format

A function to format the user's input before it's returned.

The format function for the input.

View Source input/input.ts, line 768

# hint

Additional information displayed to the user.

The hint text for the prompt.

View Source input/input.ts, line 157

string | PrevCaller.<R, (string|Falsy)> | undefined

# hint

Additional information displayed to the user.

The hint text for the prompt.

View Source input/input.ts, line 846

# inactive

The style applied to non-selected options.

The inactive option style for select inputs.

View Source input/input.ts, line 147

string | PrevCaller.<R, (string|Falsy)> | undefined

# inactive

The style applied to non-selected options.

The inactive option style for select inputs.

View Source input/input.ts, line 834

# increment

The step size when increasing or decreasing the number.

The increment value for number inputs.

View Source input/input.ts, line 132

number | PrevCaller.<R, (number|Falsy)> | undefined

# increment

The step size when increasing or decreasing the number.

The increment value for number inputs.

View Source input/input.ts, line 816

# initial

The default value presented to the user.

The initial value of the prompt.

View Source input/input.ts, line 82

InitialReturnValue | PrevCaller.<R, (InitialReturnValue|Promise.<InitialReturnValue>)> | undefined

# initial

The default value presented to the user.

The initial value of the prompt.

View Source input/input.ts, line 756

# instructions

Additional guidance provided to the user.

Instructions for the user.

View Source input/input.ts, line 127

string | boolean | undefined

# instructions

Additional guidance provided to the user.

Instructions for the user.

View Source input/input.ts, line 810

# limit

The maximum number of items that can be selected.

The limit for list inputs.

View Source input/input.ts, line 168

number | PrevCaller.<R, (number|Falsy)> | undefined

# limit

The maximum number of items that can be selected.

The limit for list inputs.

View Source input/input.ts, line 858

# mask

The character used to hide the user's input.

The mask for password inputs.

View Source input/input.ts, line 173

string | PrevCaller.<R, (string|Falsy)> | undefined

# mask

The character used to hide the user's input.

The mask for password inputs.

View Source input/input.ts, line 864

# max

The highest number the user can input.

The maximum value for number inputs.

View Source input/input.ts, line 112

number | PrevCaller.<R, (number|Falsy)> | undefined

# max

The highest number the user can input.

The maximum value for number inputs.

View Source input/input.ts, line 792

# message

The question or instruction presented to the user.

The message displayed to the user.

View Source input/input.ts, line 77

ValueOrFunc.<string> | undefined

# message

The question or instruction presented to the user.

The message displayed to the user.

View Source input/input.ts, line 750

# min

The lowest number the user can input.

The minimum value for number inputs.

View Source input/input.ts, line 107

number | PrevCaller.<R, (number|Falsy)> | undefined

# min

The lowest number the user can input.

The minimum value for number inputs.

View Source input/input.ts, line 786

# name

Used as the key in the returned answers object.

The name of the prompt.

View Source input/input.ts, line 72

ValueOrFunc.<R>

# name

Used as the key in the returned answers object.

The name of the prompt.

View Source input/input.ts, line 744

# onState

A function called when the state of the prompt changes.

The onState callback function.

View Source input/input.ts, line 102

PrevCaller.<R, void> | undefined

# onState

A function called when the state of the prompt changes.

The onState callback function.

View Source input/input.ts, line 780

# round

Determines the precision of float inputs.

The number of decimal places to round to for float inputs.

View Source input/input.ts, line 122

number | PrevCaller.<R, (number|Falsy)> | undefined

# round

Determines the precision of float inputs.

The number of decimal places to round to for float inputs.

View Source input/input.ts, line 804

# separator

The character used to separate list items.

The separator for list inputs.

View Source input/input.ts, line 137

string | PrevCaller.<R, (string|Falsy)> | undefined

# separator

The character used to separate list items.

The separator for list inputs.

View Source input/input.ts, line 822

# stdin

The input stream used by the prompt.

The stdin stream for the prompt.

View Source input/input.ts, line 183

Readable | undefined

# stdin

The input stream used by the prompt.

The stdin stream for the prompt.

View Source input/input.ts, line 876

# stdout

The output stream used by the prompt.

The stdout stream for the prompt.

View Source input/input.ts, line 178

Writable | undefined

# stdout

The output stream used by the prompt.

The stdout stream for the prompt.

View Source input/input.ts, line 870

# style

Determines the visual style of the prompt.

The style of the prompt.

View Source input/input.ts, line 87

string | PrevCaller.<R, (string|Falsy)> | undefined

# style

Determines the visual style of the prompt.

The style of the prompt.

View Source input/input.ts, line 762

# type

Determines the input method (e.g., text, number, confirm).

The type of the prompt.

View Source input/input.ts, line 67

PromptType | Falsy | PrevCaller.<R, (PromptType|Falsy)>

# type

Determines the input method (e.g., text, number, confirm).

The type of the prompt.

View Source input/input.ts, line 738

# validate

A function to validate the user's input.

The validation function for the input.

View Source input/input.ts, line 97

PrevCaller.<R, (boolean|string|Promise.<(boolean|string)>)> | undefined

# validate

A function to validate the user's input.

The validation function for the input.

View Source input/input.ts, line 774

# warn

A warning message displayed to the user.

The warning text for the prompt.

View Source input/input.ts, line 162

string | PrevCaller.<R, (string|Falsy)> | undefined

# warn

A warning message displayed to the user.

The warning text for the prompt.

View Source input/input.ts, line 852

Methods

# async ask()

Prompts the user and returns their response as a single value.

Asks the user for input based on the current UserInput configuration.

View Source input/input.ts, line 1099

A Promise that resolves to the user's answer.

# setActive(value)

Configures the style applied to the currently selected option.

Sets the active option style for select inputs.

Parameters:
Name Type Description
value

The active option style.

View Source input/input.ts, line 1011

This UserInput instance for method chaining.

# setChoices(value)

Configures the available options that the user can select from in choice-based prompts.

Sets the choices for select, multiselect, or autocomplete inputs.

Parameters:
Name Type Description
value

The array of choices or a function to determine the choices.

View Source input/input.ts, line 1029

This UserInput instance for method chaining.

# setFloat(value)

Configures whether decimal numbers are allowed.

Sets whether to allow float values for number inputs.

Parameters:
Name Type Description
value

Whether to allow float values.

View Source input/input.ts, line 966

This UserInput instance for method chaining.

# setFormat(value)

Configures a function to format the user's input before it's returned.

Sets the format function of the prompt.

Parameters:
Name Type Description
value

The format function.

View Source input/input.ts, line 921

This UserInput instance for method chaining.

# setHint(value)

Configures additional information displayed to the user.

Sets the hint text for the prompt.

Parameters:
Name Type Description
value

The hint text.

View Source input/input.ts, line 1038

This UserInput instance for method chaining.

# setInactive(value)

Configures the style applied to non-selected options.

Sets the inactive option style for select inputs.

Parameters:
Name Type Description
value

The inactive option style.

View Source input/input.ts, line 1020

This UserInput instance for method chaining.

# setIncrement(value)

Configures the step size when increasing or decreasing the number.

Sets the increment value for number inputs.

Parameters:
Name Type Description
value

The increment value.

View Source input/input.ts, line 993

This UserInput instance for method chaining.

# setInitial(value)

Configures the default value presented to the user.

Sets the initial value of the prompt.

Parameters:
Name Type Description
value

The initial value.

View Source input/input.ts, line 903

This UserInput instance for method chaining.

# setInstructions(value)

Configures additional guidance provided to the user.

Sets the instructions for the user.

Parameters:
Name Type Description
value

The instructions.

View Source input/input.ts, line 984

This UserInput instance for method chaining.

# setLimit(value)

Configures the maximum number of items that can be selected in list-type prompts.

Sets the limit for list inputs.

Parameters:
Name Type Description
value

The maximum number of items that can be selected, or a function to determine this value.

View Source input/input.ts, line 1065

This UserInput instance for method chaining.

# setMask(value)

Configures the character used to hide the user's input in password-type prompts.

Sets the mask for password inputs.

Parameters:
Name Type Description
value

The character used to mask the input, or a function to determine this value.

View Source input/input.ts, line 1074

This UserInput instance for method chaining.

# setMax(value)

Configures the highest number the user can input.

Sets the maximum value for number inputs.

Parameters:
Name Type Description
value

The maximum value.

View Source input/input.ts, line 957

This UserInput instance for method chaining.

# setMessage(value)

Configures the question or instruction presented to the user.

Sets the message of the prompt.

Parameters:
Name Type Description
value

The message to be displayed.

View Source input/input.ts, line 894

This UserInput instance for method chaining.

# setMin(value)

Configures the lowest number the user can input.

Sets the minimum value for number inputs.

Parameters:
Name Type Description
value

The minimum value.

View Source input/input.ts, line 948

This UserInput instance for method chaining.

# setOnState(value)

Configures a function to be called when the state of the prompt changes.

Sets the onState callback of the prompt.

Parameters:
Name Type Description
value

The onState callback function.

View Source input/input.ts, line 939

This UserInput instance for method chaining.

# setRound(value)

Configures the precision of float inputs.

Sets the number of decimal places to round to for float inputs.

Parameters:
Name Type Description
value

The number of decimal places.

View Source input/input.ts, line 975

This UserInput instance for method chaining.

# setSeparator(value)

Configures the character used to separate list items.

Sets the separator for list inputs.

Parameters:
Name Type Description
value

The separator character.

View Source input/input.ts, line 1002

This UserInput instance for method chaining.

# setStdin(value)

Configures the input stream used by the prompt for receiving user input.

Sets the stdin stream for the prompt.

Parameters:
Name Type Description
value

The Readable stream to be used as stdin.

View Source input/input.ts, line 1090

This UserInput instance for method chaining.

# setStdout(value)

Configures the output stream used by the prompt for displaying messages and results.

Sets the stdout stream for the prompt.

Parameters:
Name Type Description
value

The Writable stream to be used as stdout.

View Source input/input.ts, line 1082

This UserInput instance for method chaining.

# setStyle(value)

Configures the visual style of the prompt.

Sets the style of the prompt.

Parameters:
Name Type Description
value

The style to be applied.

View Source input/input.ts, line 912

This UserInput instance for method chaining.

# setSuggest(value)

Configures a function that provides suggestions based on the user's input and available choices.

Sets the suggestion function for autocomplete inputs.

Parameters:
Name Type Description
value

A function that takes the current input and available choices and returns a Promise resolving to suggestions.

View Source input/input.ts, line 1056

This UserInput instance for method chaining.

# setType(type)

Configures the input method for the prompt.

Sets the type of the prompt.

Parameters:
Name Type Description
type

The type of the prompt.

View Source input/input.ts, line 885

This UserInput instance for method chaining.

# setValidate(value)

Configures a function to validate the user's input.

Sets the validation function of the prompt.

Parameters:
Name Type Description
value

The validation function.

View Source input/input.ts, line 930

This UserInput instance for method chaining.

# setWarn(value)

Configures a warning message displayed to the user.

Sets the warning text for the prompt.

Parameters:
Name Type Description
value

The warning text.

View Source input/input.ts, line 1047

This UserInput instance for method chaining.

# async static ask(question)

Prompts the user with one or more questions and returns their answers as an object.

Asks the user one or more questions based on the provided UserInput configurations.

sequenceDiagram participant U as User participant A as ask method participant P as prompts library A->>P: Call prompts with question(s) P->>U: Display prompt(s) U->>P: Provide input P->>A: Return answers A->>A: Process answers A-->>Caller: Return processed answers
Parameters:
Name Type Description
question

A single UserInput instance or an array of UserInput instances.

View Source input/input.ts, line 1120

A Promise that resolves to an object containing the user's answers.

# async static askConfirmation(name, question, initial)

Prompts the user with a yes/no question and returns a boolean result.

Asks the user for a confirmation (yes/no).

Parameters:
Name Type Description
name

The name of the prompt, used as the key in the returned answers object.

question

The message displayed to the user.

initial

The initial value presented to the user (optional).

View Source input/input.ts, line 1156

A Promise that resolves to a boolean representing the user's answer.

# async static askNumber(name, question, min, max, initial)

Prompts the user to enter a number, with optional minimum, maximum, and initial values.

Asks the user for a number input.

Parameters:
Name Type Description
name

The name of the prompt, used as the key in the returned answers object.

question

The message displayed to the user.

min

The minimum allowed value (optional).

max

The maximum allowed value (optional).

initial

The initial value presented to the user (optional).

View Source input/input.ts, line 1133

A Promise that resolves to the number entered by the user.

# async static askText(name, question, mask, initial)

Prompts the user to enter text, with optional masking and initial value.

Asks the user for a text input.

Parameters:
Name Type Description
name

The name of the prompt, used as the key in the returned answers object.

question

The message displayed to the user.

mask

The character used to mask the input (optional, for password-like inputs).

initial

The initial value presented to the user (optional).

View Source input/input.ts, line 1145

A Promise that resolves to the text entered by the user.

# async static insist(input, test, defaultConfirmation, limit)

This method insists on getting a valid input from the user, allowing for a specified number of attempts.

Repeatedly asks for input until a valid response is given or the limit is reached.

sequenceDiagram participant U as User participant I as insist method participant A as ask method participant T as test function participant C as askConfirmation method loop Until valid input or limit reached I->>A: Call ask with input A->>U: Prompt user U->>A: Provide input A->>I: Return result I->>T: Test result alt Test passes I->>C: Ask for confirmation C->>U: Confirm input U->>C: Provide confirmation C->>I: Return confirmation alt Confirmed I-->>Caller: Return valid result else Not confirmed I->>I: Continue loop end else Test fails I->>I: Continue loop end end I-->>Caller: Return undefined if limit reached
Parameters:
Name Type Description
input

The UserInput instance to use for prompting.

test

A function to validate the user's input.

defaultConfirmation

The default value for the confirmation prompt (true for yes, false for no).

limit

The maximum number of attempts allowed (default is 1).

View Source input/input.ts, line 1199

A Promise that resolves to the valid input or undefined if the limit is reached.

# async static insistForNumber(name, question, test, min, max, initial, defaultConfirmation, limit)

This method insists on getting a valid number input from the user, allowing for a specified number of attempts.

Repeatedly asks for number input until a valid response is given or the limit is reached.

Parameters:
Name Type Description
name

The name of the prompt, used as the key in the returned answers object.

question

The message displayed to the user.

test

A function to validate the user's input.

min

The minimum allowed value (optional).

max

The maximum allowed value (optional).

initial

The initial value presented to the user (optional).

defaultConfirmation

The default value for the confirmation prompt (true for yes, false for no).

limit

The maximum number of attempts allowed (default is -1, meaning unlimited).

View Source input/input.ts, line 1232

A Promise that resolves to the valid input or undefined if the limit is reached.

# async static insistForText(name, question, test, mask, initial, defaultConfirmation, limit)

This method insists on getting a valid text input from the user, allowing for a specified number of attempts.

Repeatedly asks for text input until a valid response is given or the limit is reached.

Parameters:
Name Type Description
name

The name of the prompt, used as the key in the returned answers object.

question

The message displayed to the user.

test

A function to validate the user's input.

mask

The character used to mask the input (optional, for password-like inputs).

initial

The initial value presented to the user (optional).

defaultConfirmation

The default value for the confirmation prompt (true for yes, false for no).

limit

The maximum number of attempts allowed (default is -1, meaning unlimited).

View Source input/input.ts, line 1215

A Promise that resolves to the valid input or undefined if the limit is reached.

# static parseArgs(options)

Uses Node.js's util.parseArgs to parse command-line arguments and return the result.

Parses command-line arguments based on the provided options.

sequenceDiagram participant C as Caller participant P as parseArgs method participant U as util.parseArgs C->>P: Call with options P->>P: Prepare args object P->>U: Call parseArgs with prepared args U->>P: Return parsed result P-->>C: Return ParseArgsResult
Parameters:
Name Type Description
options

Configuration options for parsing arguments.

View Source input/input.ts, line 1250

An object containing the parsed arguments.