TTY::Prompt

repository·master·Indexed 23 days ago

https://github.com/piotrmurach/tty-prompt

An interactive command line prompt library for Ruby. It provides a robust API for gathering user input through various prompt types, including basic text input, boolean confirmations, masked passwords, and complex menus such as single and multi-selection lists, enumerated lists, and quick-key expand menus. Features include input validation, type conversion (e.g., :int, :date, :uri), multi-line input, and the ability to collect multiple answers into structured hashes.

Tokens
11.5K
Snippets
27
Records
71
Agent score
80%

What's inside tty-prompt

  1. Disable choices in menus

    master

    You can prevent specific items from being selected by providing a :disabled key. This is useful for items that are out of stock or otherwise unavailable. Disabled choices are visually marked with a character and the reason provided in the :disabled value is displayed next to the choice name.

    In an array of hashes:

    choices = [
      {name: "small", value: 1},
      {name: "medium", value: 2, disabled: "(out of stock)"}
    ]

    In a DSL block:

    menu.choice "medium", 2, disabled: "(out of stock)"
  2. Quickstart: Basic Usage

    master

    To start using tty-prompt, require the library and initialize a new prompt instance. You can then use various methods to gather user input.

    require "tty-prompt"
    
    prompt = TTY::Prompt.new
    
    # Simple text input
    name = prompt.ask("What is your name?", default: "Piotr")
    
    # Boolean confirmation
    likes_ruby = prompt.yes?("Do you like Ruby?")
    
    # Masked input (for passwords/secrets)
    secret = prompt.mask("What is your secret?")
    
    # Single selection from a list
    choice = prompt.select("Choose your destiny?", %w(Scorpion Kano Jax))
    
    # Multiple selection from a list
    drinks = prompt.multi_select("Select drinks?", %w(vodka beer wine))
  3. Handle interrupt signals (Control-C)

    master

    By default, hitting Control-C raises an InputInterrupt error. You can customize this behavior using the :interrupt option in TTY::Prompt.new.

    Available options:

    • :signal: Sends an interrupt signal.
    • :exit: Exits the program with a status code.
    • :noop: Skips the handler (no action taken).
    • custom proc: A custom block to handle the interrupt.
    prompt = TTY::Prompt.new(interrupt: :signal)
  4. Configure an EnumList prompt

    master

    An EnumList is a type of prompt used to display a static choice menu where users select an item by typing its corresponding index (e.g., '1', '2'). It is typically used by the main TTY::Prompt instance to present enumerated lists.

    You can customize the behavior of an EnumList instance using several configuration methods:

    • enum(value): Sets the symbol used between the index and the choice name (defaults to ")").
    • default(value): Sets the default option selected. The value can be an integer index or the name of the choice.
    • per_page(value): Sets the number of items to display per page when pagination is active.
    • page_help(text): Sets the help text displayed when the list is paginated.
    • quiet(value): Enables quiet mode (boolean).
    • symbols(new_symbols): Merges new symbols into the existing prompt symbols.
    • choices(values): Adds multiple choices at once.
    • choice(*value, &block): Adds a single choice. If a block is provided, it can be used to define the choice's behavior or properties.
  5. Configure prompt symbols globally or individually

    master

    You can customize the symbols used by various prompts (like ticks, crosses, or markers) by passing a :symbols hash to TTY::Prompt.new. This allows you to switch between Unicode and ASCII characters for better compatibility or aesthetic preference.

    Available symbols to overwrite:

    • tick (default: / )
    • cross (default: / x)
    • marker (default: / >)
    • dot (default: / .)
    • bullet (default: / O)
    • line (default: / -)
    • radio_on (default: / (*))
    • radio_off (default: / ( ))
    • arrow_up (default: )
    • arrow_down (default: )
    • arrow_left (default: )
    • arrow_right (default: )
    prompt = TTY::Prompt.new(symbols: {marker: ">"})
  6. Disable history tracking for line input

    master

    Prompts that accept line input (like multiline or ask) track a history buffer by default, allowing users to navigate previous entries using the up/down arrow keys. To disable this behavior, set track_history: false in the TTY::Prompt constructor.

    prompt = TTY::Prompt.new(track_history: false)
  7. Disable re-echoing of answers with :quiet

    master
    For prompts like select, multi_select, expand, and slider, you can use the :quiet option to prevent the prompt from re-printing the question and the selected answer after the selection is made. This can be configured globally or per-prompt.
  8. Customize help text color

    master

    The :help_color option allows you to change the color of help text (e.g., instructions on how to use arrow keys). This is supported by select, multi_select, and expand prompts.

    Like :active_color, it can be applied globally or per-prompt and accepts a color symbol, a Pastel object, or a callable.

  9. Customize the active color for prompts

    master

    The :active_color option controls the color used to highlight currently selected choices in select, multi_select, enum_select, and expand prompts, as well as the user's final answer.

    This can be set globally during initialization or individually for a specific prompt call. The value can be a color symbol, a Pastel object, or a callable (proc).

    Supported colors can be found in the pastel documentation.