Terminal UIs,
the Ruby way.
RatatuiRuby is a RubyGem built on Ratatui, a leading TUI library written in Rust. You get native performance with the joy of Ruby.
$ gem install ratatui_ruby
Latest release: v1.3.0
Rich Moments
Add a spinner, a progress bar, or an inline menu to your CLI script. No full-screen takeover. Your terminal history stays intact.
Inline Viewports
Standard TUIs erase themselves on exit. Your carefully formatted CLI output disappears. Users lose their scrollback.
Inline viewports solve this. They occupy a fixed number of lines, render rich UI, then leave the output in place when done.
Perfect for spinners, menus, progress indicators—any brief moment of richness.
class Spinner
def main
RatatuiRuby.(viewport: :inline, height: 1) do |tui|
until
status = tui.(text: "#{} Connecting...")
tui. { |frame| frame.(status, frame.) }
return (tui, "Canceled!", :red) if tui..
end
(tui, "Connected!", :green)
end
end
def ending(tui, message, color) = tui. do |frame|
frame.(tui.(text: message, fg: color), frame.)
end
def initialize = (@frame, @finish = 0, Time. + 2)
def connected? = Time. >= @finish # Simulate work
def spin = SPINNER[(@frame += 1) % SPINNER.]
SPINNER = %w[⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏]
end
Spinner..;
Full Example Build an Inline Menu Arrow keys select, enter confirms, ctrl+c cancels — all the UX, in one self-contained class.
require "ratatui_ruby"
# This example renders an inline menu. Arrow keys select, enter confirms.
# The menu appears in-place, preserving scrollback. When the user chooses,
# the TUI closes and the script continues with the selected value.
class RadioMenu
CHOICES = ["Production", "Staging", "Development"] # ASCII strings are universally supported.
PREFIXES = { active: "●", inactive: "○" } # Some terminals may not support Unicode.
CONTROLS = "↑/↓: Select | Enter: Choose | Ctrl+C: Cancel"# Let users know what keys you handle.
TITLES = ["Select Environment", # The default title position is top left.
{ content: CONTROLS, # Multiple titles can save space.
position: :bottom, # Titles go on the top or bottom,
alignment: :right }] # aligned left, right, or center
def call # This method blocks until a choice is made.
RatatuiRuby.(viewport: :inline, height: 5) do |tui| # RatauiRuby.run manages the terminal.
@tui = tui # The TUI instance is safe to store.
until # You can use any loop keyword you like.
end # `run` won't return until your block does,
RadioMenu::CHOICES[@choice] # so you can use it synchronously.
end
# Classes like RadioMenu are convenient for
private # CLI authors to offer "rich moments."
def show_menu = @tui. do |frame| # RatatuiRuby gives you low-level access.
widget = @tui.( # But the TUI facade makes it easy to use.
text: , # Text can be spans, lines, or paragraphs.
block: @tui.(borders: :all, titles: TITLES) # Blocks give you boxes and titles, and hold
) # one or more widgets. We only use one here,
frame.(widget, frame.) # but "area" lets you compose sub-views.
end
def chosen? # You are responsible for handling input.
interaction = @tui. # Every frame, you receive an event object:
return if interaction. # Key, Mouse, Resize, Paste, FocusGained,
# FocusLost, or None objects. They come with
(-1) if interaction. # predicates, support pattern matching, and
(1) if interaction. # can be inspected for properties directly.
if interaction. # Your application must handle every input,
false # even interrupts and other exit patterns.
end
def choose # Here, the loop is about to exit, and the
# block will return. The inline viewport
@choice # will be torn down and the terminal will
end # be restored, but you are responsible for
# positioning the cursor.
def prepare_next_line # To ensure the next output is on a new
area = @tui. # line, query the viewport area and move
RatatuiRuby. = [0, area. + area.] # the cursor to the start of the last line.
# Then print a newline.
end
def quit! # All of your familiar Ruby control flow
# keywords work as expected, so we can
exit 0 # use them to leave the TUI.
end
def move_by(line_count) # You are in full control of your UX, so
@choice = (@choice + line_count) % CHOICES. # you can implement any logic you need:
end # Would you "wrap around" here, or not?
#
def menu_items = CHOICES.. do |choice, i| # Notably, RatatuiRuby has no concept of
"#{(i)} #{choice}" # "menus" or "radio buttons". You are in
end # full control, but it also means you must
def prefix_for(choice_index) # implement the logic yourself. For larger
return PREFIXES[:active] if choice_index == @choice # applications, consider using Rooibos,
PREFIXES[:inactive] # an MVU framework built with RatatuiRuby.
end # Or, use the upcoming ratatui-ruby-kit,
# our object-oriented component library.
def initialize = @choice = 0 # However, those are both optional, and
end # designed for full-screen Terminal UIs.
# RatatuiRuby will always give you the most
choice = RadioMenu.. # control, and is enough for "rich CLI
"You chose #{choice}!" # moments" like this one.
Build Something Real
Full-screen applications with keyboard and mouse input. The managed loop sets up the terminal and restores it on exit, even after crashes.
RatatuiRuby. do |tui|
loop do
tui. do |frame|
frame.(
tui.(
text: "Hello, RatatuiRuby!",
alignment: :center,
block: tui.(
title: "My App",
titles: [{ content: "q: Quit", position: :bottom, alignment: :right }],
borders: [:all],
border_style: { fg: "cyan" }
)
),
frame.
)
end
case tui.
in { type: :key, code: "q" } | { type: :key, code: "c", modifiers: ["ctrl"] }
break
else
nil
end
end
end
The Managed Loop
RatatuiRuby.run enters raw mode, switches to the alternate screen, and restores the terminal
on exit.
Inside the block: call draw to render, and poll_event to read input.
Widgets included:
Testing Built In
TUI testing is tedious. You need a headless terminal, event injection, snapshot comparisons, and style assertions. RatatuiRuby bundles all of it.
require "ratatui_ruby/test_helper"
class TestColorPicker < Minitest::Test
include RatatuiRuby::TestHelper # Built-in mixin
def test_swatch_widget # Unit testing
(10, 3) do
RatatuiRuby. do |frame|
frame.(Swatch.(:red), frame.)
end
2, 1, char: "█", bg: :red
end
end
def test_input_hex # Integration testing
do
"#", "f", "f", "0", "0", "0", "0"
:enter, :q
ColorPicker..
"after_hex_entry"
end
end
end
One Include, Everything Works
The module sets up a headless terminal, injects events, and asserts on rendered output. Everything runs in-process with no external dependencies.
What's inside:
- Headless terminal No real TTY needed
- Snapshots Plain text and rich (ANSI colors)
- Event injection Keys, mouse, paste, resize
- Style assertions Color, bold, underline at any cell
- Test doubles Mock frames and stub rects
- UPDATE_SNAPSHOTS=1 Regenerate baselines in one command
Full App Solutions
RatatuiRuby renders. For complex applications, add a framework that manages state and composition.
Rooibos
Model-View-Update architecture. Inspired by Elm, Bubble Tea, and React + Redux. Your UI is a pure function of state.
- Functional programming with MVU
- Commands work off the main thread
- Messages, not callbacks, drive updates
Kit
Component-based architecture. Encapsulate state, input handling, and rendering in reusable pieces.
- OOP with stateful components
- Separate UI state from domain logic
- Built-in focus management & click handling
Both use the same widget library, declarative styles, and rendering engine. Both compose cleanly, test easily with snapshots and event injection, and keep state easy to reason about.
Pick the paradigm that fits your brain. You can't choose wrong.
Why RatatuiRuby?
Ruby deserves world-class terminal user interfaces. TUI developers deserve a world-class language.
RatatuiRuby wraps Rust's Ratatui via native extension. The Rust library handles rendering. Your Ruby code handles design.
Text UIs are seeing a renaissance with many new TUI libraries popping up. The Ratatui bindings have proven to be full featured and stable.
Mike Perham, creator of Sidekiq and Faktory
Why Rust? Why Ruby?
Rust excels at low-level rendering. Ruby excels at expressing domain logic and UI. RatatuiRuby puts each language where it performs best.
Versus CharmRuby
CharmRuby wraps Charm's Go libraries. Both projects give Ruby developers TUI options.
| CharmRuby | RatatuiRuby | |
|---|---|---|
| Integration | Two runtimes, one process | Native extension in Rust |
| Runtime | Go + Ruby (competing) | Ruby (Rust has no runtime) |
| Memory | Two uncoordinated GCs | One Garbage Collector |
| Style | The Elm Architecture (TEA) | TEA, OOP, or Imperative |