Introduction to TTY::Prompt
TTY::Prompt is a valuable component of the TTY Toolkit, designed to facilitate the creation of interactive command-line applications. This project provides a variety of prompt types to gather user input, coupled with a robust API for input validation, friendly error feedback, and an intuitive domain-specific language (DSL) for menu creation. With support for Linux, OS X, FreeBSD, and Windows, TTY::Prompt ensures a broad compatibility for developers working across different platforms.
Key Features
TTY::Prompt comes loaded with a multitude of features that make it a great tool for developers who need to gather user input through a command-line interface:
- Variety of Prompt Types: A selection of prompts are available, including basic input, select menus, multi-select menus, and masked input for sensitive information.
- Robust Input Validation: Offers built-in mechanisms to ensure that the user provides valid input.
- User-Friendly Error Messaging: Provides clear error messages to guide users in correcting their input.
- Intuitive DSL for Complex Menus: Enables easy creation of menus, including the ability to paginate long lists.
- Cross-Platform Support: Functions seamlessly across Unix-based systems and Windows, although the feature set may be more enriched on Unix systems.
Windows Support
On Windows, TTY::Prompt uses the Win32 API to emulate Unix terminal functionality as closely as possible, and while some Unix-specific features may not fully translate, workarounds such as using Git Bash or third-party terminal emulators like ConEmu can enhance the user experience.
Getting Started
The installation of TTY::Prompt is simple. You can add it to your project by including the following line in your application's Gemfile:
gem "tty-prompt"
And then run the command:
$ bundle
Alternatively, it can be installed directly using:
$ gem install tty-prompt
Usage
TTY::Prompt allows creating interactive prompts to ask users questions directly from the command line. Here's a basic example:
require "tty-prompt"
prompt = TTY::Prompt.new
# Ask a simple question
prompt.ask("What is your name?", default: ENV["USER"])
# Confirmation
prompt.yes?("Do you like Ruby?")
# Input with masking
prompt.mask("What is your secret?")
# Select from a list
prompt.select("Choose your destiny?", %w(Scorpion Kano Jax))
# Multi-select from a list
choices = %w(vodka beer wine whisky bourbon)
prompt.multi_select("Select drinks?", choices)
# Enum select for numbered options
choices = %w(emacs nano vim)
prompt.enum_select("Select an editor?", choices)
# Complex inputs with validation and conversion
prompt.ask("Provide range of numbers?", convert: :range)
Interface
TTY::Prompt offers a rich interface that supports a variety of input operations, from simple text input and confirmation to complex multi-choice and extended list selections. Each method is capable of being customized further with options like :convert
for input conversion, :default
for default values, and :validate
for input validation to meet your application needs.
Conclusion
With its extensive features and wide compatibility, TTY::Prompt is an effective tool for developers looking to enhance their command-line applications with interactive input capabilities. It maintains simplicity with the necessary flexibility, making it suitable for both small-scale scripts and larger, more complex systems.