Terminal Output During TUI Sessions
The Problem
Writing to stdout or stderr during a TUI session corrupts the display.
When your application is running inside RatatuiRuby.run, the terminal is in āraw modeā and RatatuiRuby has taken control of the display buffer. Any output via puts, warn, p, print, or direct writes to STDOUT/STDERR will:
-
Corrupt the screen layout - Characters appear in random positions
-
Mix with TUI output - Text interleaves with your widgets unpredictably
-
Trigger escape sequence errors - Partial ANSI codes can break rendering
Why This Happens
In raw mode: - The terminal doesnāt process newlines or carriage returns normally - Output bypasses the TUIās controlled buffer - Cursor position is undefined from the TUIās perspective
Safe Patterns
Defer output until after the TUI exits
messages = [] RatatuiRuby.run do |tui| # Collect messages instead of printing them messages << "Something happened" # ... TUI logic ... end # Now safe to print messages.each { |msg| puts msg }
Use debug logging to a file
DEBUG_LOG = File.open("/tmp/my_app_debug.log", "a") RatatuiRuby.run do |tui| DEBUG_LOG.puts "Debug: something happened" DEBUG_LOG.flush # ... TUI logic ... end
Display messages in the TUI itself
RatatuiRuby.run do |tui| @status_message = "Something happened" tui.draw do |frame| # Show status in the UI frame.render_widget( tui.paragraph(text: @status_message), status_area ) end end
Library Behavior
RatatuiRuby automatically defers its own warnings (like experimental feature notices) during TUI sessions. They are queued and printed after restore_terminal is called.
You donāt need to do anything special for library warningsātheyāre handled automatically.