module RatatuiRuby::Terminal::Capabilities
Environment-based terminal capability detection.
TUI applications need to know what the terminal supports. Color depth varies. Some terminals lack escape sequence support entirely. Users set environment variables like NO_COLOR to express preferences.
This module detects terminal capabilities from environment variables. It checks TERM, COLORTERM, NO_COLOR, and FORCE_COLOR to determine what the terminal supports.
Use these methods before initializing a Terminal instance to decide whether TUI mode is appropriate for the current environment.
Example
if RatatuiRuby::Terminal.interactive?
RatatuiRuby.run { |tui| ... }
else
puts "TUI mode not available"
end
Public Instance Methods
Source
# File lib/ratatui_ruby/terminal/capabilities.rb, line 204 def available_color_count _available_color_count end
Queries how many colors the terminal can display.
Modern terminals vary wildly in capability. Some only support 8 ANSI colors. Others display 256. High-end terminals render 16 million truecolor shades. If your app uses rich color palettes without checking, users on basic terminals see garbled output or crashes.
This method queries crossterm (which checks COLORTERM and TERM) and returns the raw count. Use it to select color palettes or degrade gracefully.
Returns 8, 256, or 65535 (truecolor).
Example
colors = RatatuiRuby::Terminal.available_color_count palette = colors >= 256 ? :rich : :basic
Source
# File lib/ratatui_ruby/terminal/capabilities.rb, line 236 def color_support return :none if no_color? return :none if dumb? count = available_color_count return :truecolor if count >= 65_535 return :ansi256 if count >= 256 :basic end
Returns the terminalâs color capability as a symbol.
Comparing integers is annoying. You want to know: can I use gradients? Do I need a fallback palette? This method translates the raw count into semantic symbols.
Returns :none when NO_COLOR is set or terminal is dumb. Returns :basic (8 colors), :ansi256 (256), or :truecolor (16M) based on capability.
Use it to switch rendering strategies or skip color entirely.
Example
case RatatuiRuby::Terminal.color_support when :truecolor then use_gradient_theme when :ansi256 then use_256_palette when :basic then use_ansi_colors when :none then use_monochrome end
Source
# File lib/ratatui_ruby/terminal/capabilities.rb, line 95 def dumb? ENV["TERM"] == "dumb" end
Checks if the terminal declared itself âdumb.â
Dumb terminals exist. Emacs shell-mode sets TERM=dumb. Serial consoles do too. These terminals cannot interpret escape sequences. If your app sends cursor movements or colors, output becomes unreadable.
This method checks for explicit TERM=dumb. Empty or unset TERM means âunknown,â not âdumb.â Use it to fall back to plain text rendering.
Example
if RatatuiRuby::Terminal.dumb? render_plain_table(data) else render_styled_table(data) end
Source
# File lib/ratatui_ruby/terminal/capabilities.rb, line 146 def force_color? ENV.key?("FORCE_COLOR") end
Checks if color output is explicitly forced.
Some CI systems and logging pipelines detect non-TTY and strip colors. Users want colors anyway for readability. FORCE_COLOR overrides the TTY check.
This method checks for FORCE_COLOR in the environment. When set, your app should emit colors even when tty? returns false.
Example
use_color = RatatuiRuby::Terminal.tty? || RatatuiRuby::Terminal.force_color?
Source
# File lib/ratatui_ruby/terminal/capabilities.rb, line 309 def force_color_output(enable) _force_color_output(enable) end
Globally override NO_COLOR detection.
The NO_COLOR environment variable tells applications to disable color. Sometimes users want colors anyway, like in CI pipelines that log to files but display logs with color. Passing true forces color output regardless of NO_COLOR.
This method calls crosstermâs global override. The effect is immediate and affects all subsequent color detection queries. Pass false to restore normal NO_COLOR behavior.
Example
if options[:color] == :always RatatuiRuby::Terminal.force_color_output(true) end
Source
# File lib/ratatui_ruby/terminal/capabilities.rb, line 175 def interactive? tty? && !dumb? end
Checks if the terminal supports interactive TUI mode.
A TUI needs a real terminal. Piped output breaks cursor control. Dumb terminals corrupt escape sequences. Starting TUI mode in these environments wastes resources and confuses users.
This method combines tty? and dumb? checks. Returns true only when both conditions allow TUI operation. Use it as the gatekeeper before calling run.
Example
if RatatuiRuby::Terminal.interactive? RatatuiRuby.run { |tui| main_loop(tui) } else abort "Interactive terminal required" end
Source
# File lib/ratatui_ruby/terminal/capabilities.rb, line 120 def no_color? ENV.key?("NO_COLOR") end
Checks if the user disabled color output.
Users with visual impairments or screen readers often disable colors. The NO_COLOR standard (no-color.org) provides a universal way to request this. Ignoring it frustrates accessibility-conscious users.
This method checks for NO_COLOR in the environment. The value does not matter; presence alone disables color. Respect it.
Example
style = RatatuiRuby::Terminal.no_color? ? :plain : :colored
Source
# File lib/ratatui_ruby/terminal/capabilities.rb, line 277 def supports_keyboard_enhancement? return false unless tty? Timeout.timeout(0.5) { _supports_keyboard_enhancement } rescue false end
Checks for Kitty keyboard protocol support.
Standard terminal input is ambiguous. Escape key and arrow keys share prefixes. Modifier keys get lost. Applications that need precise key handling (editors, games) struggle with the limitations.
The Kitty keyboard protocol solves this. Terminals that support it report key presses unambiguously, with full modifier information. This method queries support so you can enable enhanced input or fall back gracefully.
Returns false immediately if tty? returns false. Otherwise queries crossterm with a 0.5s timeout. Returns true only if the terminal responds affirmatively.
Example
if RatatuiRuby::Terminal.supports_keyboard_enhancement? enable_vim_style_keybindings else use_simple_navigation end
Source
# File lib/ratatui_ruby/terminal/capabilities.rb, line 65 def tty? $stdout.tty? end
Checks if stdout connects to a terminal device.
Terminal apps render escape sequences. Piped output or log files cannot interpret them. If your app writes ANSI codes to a non-TTY, logs fill with garbage like [32m instead of green text.
This method checks $stdout.tty?. Use it to skip TUI mode when output is redirected. Print plain text instead.
Example
if RatatuiRuby::Terminal.tty? start_tui else print_plain_output end