TTY

A toolkit for developing terminal applications. Provides numerous components including CLI parser, prompts, progress bars, and more.

rubycliterminaltoolkit

Framework

TTY

Overview

TTY is a toolkit for developing terminal applications. It provides numerous components including CLI parser, prompts, progress bars, and more. Popular among developers who value customizability due to its modular approach allowing selective use of needed features. It supports the development of beautiful and interactive terminal applications.

Details

TTY is a comprehensive toolkit for terminal application development. It consists of over 30 independent gems, allowing you to select and use only the components needed according to your project requirements. Each component can be used standalone and is easily combined with other CLI libraries.

Main Components

  • TTY::Command: System command execution
  • TTY::Prompt: Interactive prompts
  • TTY::ProgressBar: Progress bar display
  • TTY::Table: Table display
  • TTY::Box: Box drawing
  • TTY::Spinner: Spinner animation
  • TTY::Screen: Terminal information retrieval
  • TTY::Cursor: Cursor control
  • TTY::Color: Color management

Pros and Cons

Pros

  • Modular Design: Select and use only needed features
  • Rich Components: All necessary features for terminal application development
  • Beautiful UI: Visually appealing terminal display
  • Customizable: Advanced customization and styling
  • Independence: Each component can be used independently
  • Active Development: Continuous improvements and updates
  • Comprehensive Documentation: Detailed usage examples and API documentation

Cons

  • Learning Curve: Time needed to understand numerous components
  • Dependencies: Using many components increases dependencies
  • Complexity: May be excessive for simple tools
  • Ruby-specific: Cannot be used in languages other than Ruby

Key Links

Usage Examples

Installation

# All components
gem install tty

# Individual components
gem install tty-prompt
gem install tty-progressbar
gem install tty-table

TTY::Prompt Usage

require 'tty-prompt'

prompt = TTY::Prompt.new

# Basic input
name = prompt.ask('What is your name?')

# Password input
password = prompt.mask('Enter password:')

# Numeric input (with validation)
age = prompt.ask('What is your age?', convert: :int) do |q|
  q.validate(/^\d+$/, 'Please enter a valid number')
  q.validate(proc { |v| v.to_i > 0 }, 'Age must be greater than 0')
end

# Selection
framework = prompt.select('What is your favorite framework?') do |menu|
  menu.choice 'Rails'
  menu.choice 'Sinatra'
  menu.choice 'Hanami'
  menu.choice 'Roda'
end

# Multiple selection
languages = prompt.multi_select('Which languages can you use?') do |menu|
  menu.choice 'Ruby'
  menu.choice 'Python'
  menu.choice 'JavaScript'
  menu.choice 'Go'
  menu.choice 'Rust'
end

# Yes/No confirmation
confirmed = prompt.yes?('Save configuration?')

# Display results
puts "\n=== Input Results ==="
puts "Name: #{name}"
puts "Age: #{age}"
puts "Framework: #{framework}"
puts "Languages: #{languages.join(', ')}"
puts "Save: #{confirmed ? 'yes' : 'no'}"

TTY::ProgressBar Usage

require 'tty-progressbar'

# Basic progress bar
bar = TTY::ProgressBar.new("Downloading [:bar] :percent", total: 100)

100.times do |i|
  sleep(0.05)
  bar.advance(1)
end

puts "\nDownload complete!"

# Multiple progress bars
bars = TTY::ProgressBar::Multi.new("Overall progress:")

download_bar = bars.register("Download [:bar] :percent", total: 100)
process_bar = bars.register("Processing [:bar] :percent", total: 50)
upload_bar = bars.register("Upload [:bar] :percent", total: 30)

# Simulate parallel processing
threads = []

threads << Thread.new do
  100.times { download_bar.advance(1); sleep(0.02) }
end

threads << Thread.new do
  50.times { process_bar.advance(1); sleep(0.04) }
end

threads << Thread.new do
  30.times { upload_bar.advance(1); sleep(0.06) }
end

threads.each(&:join)

TTY::Table Usage

require 'tty-table'

# Basic table
header = ['Name', 'Age', 'Job', 'City']
rows = [
  ['John Doe', 30, 'Engineer', 'Tokyo'],
  ['Jane Smith', 25, 'Designer', 'Osaka'],
  ['Bob Johnson', 35, 'Manager', 'Nagoya']
]

table = TTY::Table.new(header, rows)
puts table.render(:unicode)

# Styled table
table = TTY::Table.new do |t|
  t << ['Product', 'Price', 'Stock', 'Category']
  t << :separator
  t << ['Laptop', '$800', '15', 'Computer']
  t << ['Mouse', '$25', '50', 'Accessory']  
  t << ['Keyboard', '$80', '25', 'Accessory']
end

puts table.render(:unicode, padding: [0, 1], border: {top: '=', bottom: '='})

# Alignment
table = TTY::Table.new do |t|
  t << ['Left', 'Center', 'Right']
  t << ['Left', 'Center', 'Right']
  t << ['L', 'C', 'R']
end

puts table.render do |renderer|
  renderer.alignments = [:left, :center, :right]
  renderer.padding = [0, 1]
  renderer.border.style = :thick
end

TTY::Box Usage

require 'tty-box'

# Basic box
box = TTY::Box.frame(
  width: 50,
  height: 10,
  align: :center,
  padding: 1,
  title: {top_left: " Information "}
) do
  "This is text inside the box.\nMultiple lines of text\ncan also be displayed."
end

puts box

# Styled boxes
success_box = TTY::Box.success(
  "Process completed successfully!",
  width: 40,
  align: :center,
  padding: 1
)

puts success_box

warning_box = TTY::Box.warn(
  "Warning: This operation cannot be undone.",
  width: 40,
  align: :center,
  padding: 1
)

puts warning_box

error_box = TTY::Box.error(
  "Error: File not found.",
  width: 40,
  align: :center,
  padding: 1
)

puts error_box

TTY::Spinner Usage

require 'tty-spinner'

# Basic spinner
spinner = TTY::Spinner.new("[:spinner] Loading data...")
spinner.auto_spin

sleep(3) # Simulate processing

spinner.success("Done!")

# Multiple spinners
spinners = TTY::Spinner::Multi.new("[:spinner] Running multiple tasks...")

sp1 = spinners.register("[:spinner] Task 1...")
sp2 = spinners.register("[:spinner] Task 2...")  
sp3 = spinners.register("[:spinner] Task 3...")

spinners.auto_spin

sleep(2)
sp1.success("Task 1 complete")

sleep(1)
sp2.success("Task 2 complete")

sleep(1)
sp3.success("Task 3 complete")

spinners.success("All tasks completed!")

TTY::Command Usage

require 'tty-command'

cmd = TTY::Command.new

# Basic command execution
result = cmd.run('ls -la')
puts "Exit status: #{result.exit_status}"
puts "Output: #{result.out}"

# Command execution with error handling
begin
  result = cmd.run('nonexistent-command')
rescue TTY::Command::ExitError => err
  puts "Command execution error: #{err.message}"
end

# Multiple command execution
commands = [
  'echo "Hello"',
  'date',
  'whoami'
]

commands.each do |command|
  puts "Executing: #{command}"
  result = cmd.run(command)
  puts "Result: #{result.out.chomp}"
  puts "---"
end

# Pipe processing
result = cmd.run('ps aux | grep ruby | head -5')
puts result.out

Execution Examples

# Running file manager
ruby file_manager.rb

# Using individual components
ruby -r tty-prompt -e "puts TTY::Prompt.new.ask('Your name?')"
ruby -r tty-table -e "puts TTY::Table.new([['Name', 'Age'], ['John', 30]]).render"