Back to Enquirer

Enquirer Cheatsheet

support/cheatsheet.md

2.4.08.1 KB
Original Source

Enquirer Cheatsheet

API

State and Status

  • state - The state contains the necessary properties for rendering each part of a prompt in the terminal.
  • status - A prompt's status is either pending, answered, or cancelled.

Styling

  • Styles - Styles are the semantic colors used consistently throughout the prompts.
  • Symbols - Unicode symbols
  • Elements - Elements combine styles, unicode symbols, and text for the various parts of a prompt.
  • Transforms - Transforms are responsible for formatting user input based on prompt state and status.
  • Themes

Elements

An element is a functions that is responsible for combining the styles, text and unicode symbols to display for a specific part of prompt, based on prompt's state and status.

prefix message separator indicator

Transforms

objects that consist of a render function and a result function. based on the state and status of the prompt.

Terminology

Whenever possible, we attempted to use familiar concepts and terminology from HTML/CSS to describe prompt behavior and attributes.

termdescription
focusedThe currently targeted choice in a visible list of choices. Similar in concept to focus in HTML and CSS.
pointerMarks the choice that currently has focus. The symbol is often used for this, but the pointer is not always visible, as with the autocomplete prompt.
indicatorDesignates whether or not a choice is checked/enabled.
indexThe zero-based position of the pointer in the visible list of choices.
cursorThe zero-based position of the cursor relative to user input.
selectedThe position of the currently choice.
enabledThe position of the currently choice.
activeThe position of the currently choice.
elementElements are the various parts of a prompt. An element may consist of styles, text and unicode symbols.

Keypresses

commanddescription
<kbd>ctrl</kbd>+<kbd>a</kdb>Move the cursor to the first character in user input.

| <kbd>ctrl</kbd>+<kbd>c</kbd> | Cancel the prompt. | | <kbd>ctrl</kbd>+<kbd>d</kbd> | Cancel the prompt. | | <kbd>ctrl</kbd>+<kbd>g</kdb> | Reset the prompt to its initial state. | | <kbd>ctrl</kbd>+<kbd>l</kdb> | Reset the prompt to its initial state. | | <kbd>ctrl</kbd>+<kbd>r</kdb> | Remove the current value from prompt history (when supported). | | <kbd>ctrl</kbd>+<kbd>s</kdb> | Save the current value to prompt history (when supported). | | <kbd>ctrl</kbd>+<kbd>z</kdb> | Undo the previous action (when supported). |

Move Cursor

The following keypresses may be used on any prompts that support user input.

commanddescription
<kbd>ctrl</kbd>+<kbd>a</kbd>Move cursor to the start of the line
<kbd>ctrl</kbd>+<kbd>e</kbd>Move cursor to the end of the line
<kbd>ctrl</kbd>+<kbd>b</kbd>Move cursor back one character
<kbd>ctrl</kbd>+<kbd>f</kbd>Move cursor forward one character
<kbd>ctrl</kbd>+<kbd>x</kbd>Toggle between first and cursor position
<kbd>left</kbd>Move the cursor forward one character.
<kbd>right</kbd>Move the cursor back one character.

Windows

Not implemented yet

commanddescription
<kbd>alt</kbd>+<kbd>b</kbd>Move cursor back one word
<kbd>alt</kbd>+<kbd>f</kbd>Move cursor forward one word

Array prompt keypresses

commanddescription
<kbd>a</kbd>Toggle all choices to be enabled or disabled.
<kbd>i</kbd>Invert the current selection of choices.
<kbd>g</kbd>Toggle the current choice group.
<kbd>space</kbd>Toggle the currently selected choice when options.multiple is true.

Hide/show Choices

| <kbd>fn</kbd>+<kbd>up</kbd> | Decrease the number of visible choices by one. | | <kbd>fn</kbd>+<kbd>down</kbd> | Increase the number of visible choices by one. |

Move Pointer

commanddescription
<kbd>number</kbd>Move the pointer to the choice at the given index. Also toggles the selected choice when options.multiple is true.
<kbd>up</kbd>Move the pointer up.
<kbd>down</kbd>Move the pointer down.
<kbd>ctrl</kbd>+<kbd>a</kbd>Move the pointer to the first visible choice.
<kbd>ctrl</kbd>+<kbd>e</kbd>Move the pointer to the last visible choice.
(mac) <kbd>fn</kbd>+<kbd>left</kbd> / (win) <kbd>home</kbd>Move the pointer to the first choice in the choices array.
(mac) <kbd>fn</kbd>+<kbd>right</kbd> / (win) <kbd>end</kbd>Move the pointer to the last choice in the choices array.
<kbd>shift</kbd>+<kbd>up</kbd>Scroll up one choice without changing pointer position.
<kbd>shift</kbd>+<kbd>down</kbd>Scroll down one choice without changing pointer position.

Styles

Styles are semantically-named functions that may be used for adding color (via ANSI style codes) to the various elements of a prompt.

Default palette

NameDefault colorsDescription
primarycyanUsed on the prompt indicator, choice pointers, and to style user input (the "answer") in the terminal after it's submitted.
dangerredUsed on error messages.
strongboldUsed on the user-defined prompt message
successgreenUsed on enabled choice indicators.
warningyellowNot used.
muteddimUsed on hints.
disabledgrayUsed on disabled choice messages.
darkdim.grayUsed by other styles.
defaultnoop (no styling is applied)Not used.
infostyles.primaryUsed by the confirm prompt for styling user-input.
inverseInverse of styles.primaryNot used.
complementComplement of styles.primaryNot used.
answeredstyles.primaryUsed in several prompts to style user input after submitted.
cancelledstyles.dangerUsed to style the prompt prefix when the prompt is cancelled.
completingstyles.defaultNot used.
pendingstyles.defaultNot used.
onstyles.successUsed on checked choice indicators.
offstyles.darkUsed on unchecked choice indicators (radio buttons, checkboxes, check marks, etc.)
activestyles.primary
selectedstyles.active.underline
placeholderstyles.primary.dim
highlightstyles.inverse

The [ansi-colors][] library is used to apply styling.

Applying styles

If you are a prompt author, styles may be accessed using prompt.styles (or this.styles inside a prompt instance), where each "style" is a function that wraps the returned string in ANSI codes.


Events

EventDescription
stateA clone of the prompt.state object is emitted for each keypress.
alertEmitted when prompt.alert() is called. This is useful for detecting invalid keypresses when output is not visible in the terminal (as in unit tests).
submitEmitted with the answer value when submitted by the user.
cancelEmitted when the prompt is terminated by the user or an error is thrown.
closeEmitted when the readline interface is closed and the input stream is paused.
runEmitted when the prompt has been initialized.