2.2.9 prefix

You can prefix each question asked using the :prefix option. This option can be applied either globally for all prompts or individual for each one:

prompt = TTY::Prompt.new(prefix: '[?] ')

2.2.10 active_color

All prompt types support :active_color option. In case of select, multi_select, enum_select or expand this color is used to highlight the currently selected choice. All the resulted inputs provided by user that are read in by the prompt as answer are highlighted with this color. This option can be applied either globablly for all prompts or individually.

2.2.12 interrupt

By default InputInterrupt error will be raised when the user hits the interrupt key(Control-C). However, you can customise this behaviour by passing the :interrupt option. The available options are:

:signal - sends interrupt signal

:exit - exists with status code

:noop - skips handler

custom proc

For example, to send interrupt signal do:

prompt = TTY::Prompt.new(interrupt: :signal)

2.3 keypress

In order to ask question with a single character or keypress answer use keypress:

prompt.keypress("Which one do you prefer a, b, c or d ?")

2.4 multiline

Asking for multiline input can be done with multiline method.

prompt.multiline("Provide description?")

The reading of input will terminate when empty line is submitted.

2.5 mask

If you require input of confidential information use mask method. By default each character that is printed is replaced by • symbol. All configuration options applicable to ask method can be used with mask as well.

2.6 yes?/no?

In order to display a query asking for boolean input from user use yes? like so:

prompt.yes?('Do you like Ruby?')
# =>
# Do you like Ruby? (Y/n)

You can further customize question by passing suffix, positive, negative and convert options. The suffix changes text of available options, the positive specifies display string for successful answer and negative changes display string for negative answer. The final value is a boolean provided the convert option evaluates to boolean.

It's enough to provide the suffix option for the prompt to accept matching answers with correct labels:

prompt.yes?("Are you a human?") do |q|
q.suffix 'Yup/nope'
end
# =>
# Are you a human? (Yup/nope)

2.8 expand

The expand provides a compact way to ask a question with many options.

As first argument expand takes the message to display and as a second an array of choices. Compared to the select, multi_select and enum_select, the choices need to be objects that include :key, :name and :value keys. The :key must be a single character. The help choice is added automatically as the last option and the key h.

2.9 collect

In order to collect more than one answer use collect method. Using the key you can describe the answers key name. All the methods for asking user input such as ask, mask, select can be directly invoked on the key. The key composition is very flexible by allowing nested keys. If you want the value to be automatically converted to required type use convert.