CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-prompts

Lightweight, beautiful and user-friendly interactive CLI prompts library with promise-based API

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

number-date-prompts.mddocs/

Number and Date Prompts

Prompts for numeric and temporal input with specialized controls, validation, and formatting. Number prompts support integer and floating-point input with increment/decrement controls, while date prompts provide interactive date selection with locale support.

const prompts = require('prompts');

Capabilities

Number Prompt

Interactive number input prompt with support for integer and floating-point numbers, min/max validation, and arrow key increment/decrement controls.

{
  /** Prompt type identifier */
  type: 'number',
  /** Property name for response object */
  name: string,
  /** Display message for user */
  message: string,
  /** Default number value */
  initial?: number,
  /** Maximum allowed value. Defaults to Infinity */
  max?: number,
  /** Minimum allowed value. Defaults to -Infinity */
  min?: number,
  /** Allow floating point inputs. Defaults to false */
  float?: boolean,
  /** Round float values to x decimals. Defaults to 2 */
  round?: number,
  /** Increment step when using arrow keys. Defaults to 1 */
  increment?: number,
  /** Render style controlling input display */
  style?: 'default' | 'password' | 'invisible' | 'emoji',
  /** Custom formatter for user input */
  format?: (value: number, values: Answers) => any,
  /** Input validation function */
  validate?: (value: number, values: Answers) => boolean | string,
  /** Render callback with kleur styling */
  onRender?: (kleur: any) => void,
  /** State change callback */
  onState?: (state: { value: number; aborted: boolean }) => void,
  /** Input stream */
  stdin?: NodeJS.ReadableStream,
  /** Output stream */
  stdout?: NodeJS.WritableStream
}

Usage Examples:

const prompts = require('prompts');

// Basic number input
const response = await prompts({
  type: 'number',
  name: 'age',
  message: 'How old are you?',
  initial: 0,
  min: 0,
  max: 120
});

// Floating-point number with custom increment
const response = await prompts({
  type: 'number',
  name: 'price',
  message: 'Enter price:',
  initial: 9.99,
  min: 0,
  float: true,
  round: 2,
  increment: 0.50,
  format: value => `$${value.toFixed(2)}`
});

// Number with validation
const response = await prompts({
  type: 'number',
  name: 'port',
  message: 'Enter port number:',
  initial: 3000,
  validate: value => {
    if (value < 1024) return 'Port must be 1024 or higher';
    if (value > 65535) return 'Port must be 65535 or lower';
    return true;
  }
});

Date Prompt

Interactive date selection prompt with keyboard navigation, locale support, and custom date formatting masks.

{
  /** Prompt type identifier */
  type: 'date',
  /** Property name for response object */
  name: string,
  /** Display message for user */
  message: string,
  /** Default date value */
  initial?: Date,
  /** Custom locales for month/day names */
  locales?: DateLocales,
  /** Date format mask. Defaults to 'YYYY-MM-DD HH:mm:ss' */
  mask?: string,
  /** Input validation function */
  validate?: (date: Date, values: Answers) => boolean | string,
  /** Custom formatter for date value */
  format?: (date: Date, values: Answers) => any,
  /** Render callback with kleur styling */
  onRender?: (kleur: any) => void,
  /** State change callback */
  onState?: (state: { value: Date; aborted: boolean }) => void,
  /** Input stream */
  stdin?: NodeJS.ReadableStream,
  /** Output stream */
  stdout?: NodeJS.WritableStream
}

interface DateLocales {
  /** Full month names */
  months?: string[];
  /** Short month names */
  monthsShort?: string[];
  /** Full weekday names */
  weekdays?: string[];
  /** Short weekday names */
  weekdaysShort?: string[];
}

Usage Examples:

// Basic date input
const response = await prompts({
  type: 'date',
  name: 'birthday',
  message: 'Enter your birthday:',
  initial: new Date(1990, 0, 1)
});

// Date with validation (no future dates)
const response = await prompts({
  type: 'date',
  name: 'eventDate',
  message: 'Enter event date:',
  validate: date => {
    const now = new Date();
    return date > now ? 'Date cannot be in the future' : true;
  }
});

// Date with custom format and locales
const response = await prompts({
  type: 'date',
  name: 'appointmentDate',
  message: 'Select appointment date:',
  mask: 'DD/MM/YYYY',
  locales: {
    months: [
      'Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio',
      'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'
    ],
    weekdays: [
      'Domingo', 'Lunes', 'Martes', 'Miércoles', 
      'Jueves', 'Viernes', 'Sábado'
    ]
  },
  format: date => date.toLocaleDateString('es-ES')
});

Date Formatting Masks

The date prompt supports various formatting masks for display:

Common Mask Patterns

// Year formats
'YYYY' // 2023 (4-digit year)
'YY'   // 23 (2-digit year)

// Month formats  
'MM'   // 01-12 (2-digit month)
'M'    // 1-12 (1-digit month)
'MMMM' // January (full month name)
'MMM'  // Jan (short month name)

// Day formats
'DD'   // 01-31 (2-digit day)
'D'    // 1-31 (1-digit day)
'dddd' // Monday (full weekday name)
'ddd'  // Mon (short weekday name)

// Time formats
'HH'   // 00-23 (24-hour format)
'hh'   // 01-12 (12-hour format)
'mm'   // 00-59 (minutes)
'ss'   // 00-59 (seconds)
'SSS'  // 000-999 (milliseconds)
'A'    // AM/PM

Usage Examples:

// Different date format examples
const formats = [
  'YYYY-MM-DD',           // 2023-12-25
  'DD/MM/YYYY',           // 25/12/2023
  'MMMM D, YYYY',         // December 25, 2023
  'dddd, MMM D, YYYY',    // Monday, Dec 25, 2023
  'YYYY-MM-DD HH:mm:ss',  // 2023-12-25 14:30:00
  'hh:mm A'               // 02:30 PM
];

Default Date Locales

The default English locales provided by the date prompt:

const defaultLocales = {
  months: [
    'January', 'February', 'March', 'April',
    'May', 'June', 'July', 'August', 
    'September', 'October', 'November', 'December'
  ],
  monthsShort: [
    'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
    'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
  ],
  weekdays: [
    'Sunday', 'Monday', 'Tuesday', 'Wednesday',
    'Thursday', 'Friday', 'Saturday'
  ],
  weekdaysShort: [
    'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
  ]
};

Number Prompt Behavior

Input Controls

  • Arrow Up/Down: Increment/decrement by the increment value
  • Tab key: Autocomplete to initial value when provided
  • Number keys: Direct numeric input
  • Decimal point: For float inputs when float: true
  • Backspace: Delete characters
  • Enter: Submit current value
  • Escape: Cancel prompt

Number Validation

  • Input is automatically parsed as integer or float based on float setting
  • Min/max validation is applied automatically
  • Custom validation function receives the parsed number value
  • Invalid numeric input shows error message and allows retry

Date Prompt Behavior

Navigation Controls

  • Left/Right Arrow: Navigate between date components (day/month/year/time)
  • Up/Down Arrow: Increment/decrement the selected component
  • Tab: Move to next date component
  • Enter: Submit current date
  • Escape: Cancel prompt

Date Components

  • Year: 4-digit year input with arrow key increment/decrement
  • Month: Month selection with full/short name display
  • Day: Day of month with validation for month/year
  • Hour: 24-hour or 12-hour format based on mask
  • Minute/Second: Time components with 60-value range
  • Meridiem: AM/PM for 12-hour time format

Date Validation

  • Automatic validation for valid dates (e.g., no February 30)
  • Custom validation function receives Date object
  • Invalid dates are prevented during navigation
  • Custom validation messages for business logic (e.g., no weekends)

Install with Tessl CLI

npx tessl i tessl/npm-prompts

docs

choice-prompts.md

confirmation-prompts.md

core-prompting.md

index.md

number-date-prompts.md

testing.md

text-prompts.md

tile.json