Module: RubyTUI
- Defined in:
- lib/rubytui.rb,
lib/rubytui/app.rb,
lib/rubytui/cell.rb,
lib/rubytui/rect.rb,
lib/rubytui/color.rb,
lib/rubytui/event.rb,
lib/rubytui/frame.rb,
lib/rubytui/style.rb,
lib/rubytui/buffer.rb,
lib/rubytui/errors.rb,
lib/rubytui/widget.rb,
lib/rubytui/backend.rb,
lib/rubytui/symbols.rb,
lib/rubytui/unicode.rb,
lib/rubytui/version.rb,
lib/rubytui/modifier.rb,
lib/rubytui/terminal.rb,
lib/rubytui/input/key.rb,
lib/rubytui/text/line.rb,
lib/rubytui/text/span.rb,
lib/rubytui/text/text.rb,
lib/rubytui/color_mode.rb,
lib/rubytui/layout/flex.rb,
lib/rubytui/input/parser.rb,
lib/rubytui/input/reader.rb,
lib/rubytui/widgets/list.rb,
lib/rubytui/widgets/tabs.rb,
lib/rubytui/layout/layout.rb,
lib/rubytui/widgets/block.rb,
lib/rubytui/widgets/chart.rb,
lib/rubytui/widgets/gauge.rb,
lib/rubytui/widgets/image.rb,
lib/rubytui/widgets/popup.rb,
lib/rubytui/widgets/table.rb,
lib/rubytui/widgets/canvas.rb,
lib/rubytui/stateful_widget.rb,
lib/rubytui/layout/constraint.rb,
lib/rubytui/widgets/paragraph.rb,
lib/rubytui/widgets/scrollbar.rb,
lib/rubytui/widgets/sparkline.rb,
lib/rubytui/widgets/async_image.rb,
lib/rubytui/widgets/input_field.rb,
lib/rubytui/backends/ansi_backend.rb,
lib/rubytui/backends/test_backend.rb
Overview
A pure Ruby terminal user interface library.
RubyTUI provides immediate-mode rendering with double buffering, constraint-based layouts, and a duck-typed widget protocol.
Defined Under Namespace
Modules: App, Backend, ColorMode, Input, Layout, Modifier, StatefulWidget, Symbols, Unicode, Widget, Widgets Classes: AnsiBackend, Buffer, Cell, Color, Error, Event, Frame, KeyEvent, LayoutError, Line, MouseEvent, Rect, ResizeEvent, Span, Style, Terminal, TerminalError, TestBackend, Text, UnsupportedImageFormat
Constant Summary collapse
- Constraint =
Convenience alias for Layout::Constraint
Layout::Constraint
- VERSION =
Gem version string.
"1.2.3"
Class Method Summary collapse
-
.init(output: $stdout, input: $stdin, mouse: false, alternate_screen: true) ⇒ Array(Terminal, Input::Reader)
Initialize a terminal with ANSI backend.
-
.print_box(title: nil, content: "", border: :rounded, border_style: Style::DEFAULT, style: Style::DEFAULT, text_style: Style::DEFAULT, alignment: :left, width: nil, output: $stdout) ⇒ void
Print a styled box with text content to stdout.
-
.print_gauge(ratio: 0.0, label: nil, width: nil, gauge_style: nil, title: nil, border: :rounded, border_style: Style::DEFAULT, output: $stdout) ⇒ void
Print a styled gauge/progress bar to stdout.
-
.print_table(header: nil, rows: [], widths: [], title: nil, border: :rounded, border_style: Style::DEFAULT, header_style: nil, style: Style::DEFAULT, highlight_symbol: " ", column_spacing: 1, width: nil, output: $stdout) ⇒ void
Print a styled table to stdout.
-
.render_once(width: nil, height: nil, output: $stdout) {|frame| ... } ⇒ Frame
Render widgets once and print to stdout.
-
.restore(terminal, alternate_screen: true) ⇒ void
Restore terminal to normal state.
-
.run(output: $stdout, input: $stdin, mouse: false, alternate_screen: true) {|terminal, reader| ... } ⇒ void
Run a TUI application with automatic setup and teardown.
Class Method Details
.init(output: $stdout, input: $stdin, mouse: false, alternate_screen: true) ⇒ Array(Terminal, Input::Reader)
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rubytui.rb', line 109 def init(output: $stdout, input: $stdin, mouse: false, alternate_screen: true) backend = AnsiBackend.new(output: output, input: input) terminal = Terminal.new(backend: backend) reader = Input::Reader.new(input: input) backend.enable_raw_mode backend.hide_cursor terminal.enter_alternate_screen if alternate_screen backend.enable_mouse if mouse backend.flush [terminal, reader] end |
.print_box(title: nil, content: "", border: :rounded, border_style: Style::DEFAULT, style: Style::DEFAULT, text_style: Style::DEFAULT, alignment: :left, width: nil, output: $stdout) ⇒ void
This method returns an undefined value.
Print a styled box with text content to stdout. No event loop needed.
Renders a RubyTUI::Widgets::Paragraph inside a RubyTUI::Widgets::Block with borders, then prints the ANSI output directly.
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/rubytui.rb', line 284 def print_box(title: nil, content: "", border: :rounded, border_style: Style::DEFAULT, style: Style::DEFAULT, text_style: Style::DEFAULT, alignment: :left, width: nil, output: $stdout) border_set = resolve_border_set(border) = Widgets::Block.new( title: title, borders: :all, border_set: border_set, border_style: border_style, style: style ) text = case content when String then Text.from(content) when Array then Text.from(content.join("\n")) else content end para = Widgets::Paragraph.new( text: text, style: text_style, alignment: alignment, block: ) w = width || terminal_width(output) line_count = case content when String then content.count("\n") + 1 when Array then content.length else 1 end h = line_count + 2 # +2 for borders area = Rect.new(0, 0, w, h) buf = Buffer.new(area) para.render(area, buf) output.write(buf.to_ansi) output.write("\n") end |
.print_gauge(ratio: 0.0, label: nil, width: nil, gauge_style: nil, title: nil, border: :rounded, border_style: Style::DEFAULT, output: $stdout) ⇒ void
This method returns an undefined value.
Print a styled gauge/progress bar to stdout. No event loop needed.
Renders a RubyTUI::Widgets::Gauge with optional title block, then prints the ANSI output directly.
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/rubytui.rb', line 338 def print_gauge(ratio: 0.0, label: nil, width: nil, gauge_style: nil, title: nil, border: :rounded, border_style: Style::DEFAULT, output: $stdout) = title ? Widgets::Block.new( title: title, borders: :all, border_set: resolve_border_set(border), border_style: border_style ) : nil gauge = Widgets::Gauge.new( ratio: ratio, label: label, gauge_style: gauge_style, block: ) w = width || terminal_width(output) h = title ? 3 : 1 area = Rect.new(0, 0, w, h) buf = Buffer.new(area) gauge.render(area, buf) output.write(buf.to_ansi) output.write("\n") end |
.print_table(header: nil, rows: [], widths: [], title: nil, border: :rounded, border_style: Style::DEFAULT, header_style: nil, style: Style::DEFAULT, highlight_symbol: " ", column_spacing: 1, width: nil, output: $stdout) ⇒ void
This method returns an undefined value.
Print a styled table to stdout. No event loop needed.
Renders a RubyTUI::Widgets::Table with optional header, borders, and title, then prints the ANSI output directly. Auto-calculates the required height.
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/rubytui.rb', line 232 def print_table(header: nil, rows: [], widths: [], title: nil, border: :rounded, border_style: Style::DEFAULT, header_style: nil, style: Style::DEFAULT, highlight_symbol: " ", column_spacing: 1, width: nil, output: $stdout) border_set = resolve_border_set(border) = title ? Widgets::Block.new( title: title, borders: :all, border_set: border_set, border_style: border_style ) : nil table = Widgets::Table.new( header: header, rows: rows, widths: widths, style: style, header_style: header_style, highlight_symbol: highlight_symbol, column_spacing: column_spacing, block: ) state = Widgets::TableState.new(selected: -1) # No selection for one-shot # Calculate height: header(1) + separator(1) + rows + border(2) h = rows.length + (header ? 2 : 0) + (title ? 2 : 0) w = width || terminal_width(output) area = Rect.new(0, 0, w, h) buf = Buffer.new(area) table.render(area, buf, state) output.write(buf.to_ansi) output.write("\n") end |
.render_once(width: nil, height: nil, output: $stdout) {|frame| ... } ⇒ Frame
Render widgets once and print to stdout.
No event loop, no alternate screen, no raw mode. Output stays in terminal scrollback. Ideal for CLI tools that want styled output using the widget/layout system without a full TUI.
193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/rubytui.rb', line 193 def render_once(width: nil, height: nil, output: $stdout) w = width || terminal_width(output) h = height || terminal_height(output) area = Rect.new(0, 0, w, h) buf = Buffer.new(area) frame = Frame.new(buf, area) yield frame output.write(buf.to_ansi) output.write("\n") frame end |
.restore(terminal, alternate_screen: true) ⇒ void
This method returns an undefined value.
Restore terminal to normal state.
Disables mouse, shows cursor, leaves alternate screen, and disables raw mode.
131 132 133 134 135 136 137 |
# File 'lib/rubytui.rb', line 131 def restore(terminal, alternate_screen: true) terminal.backend.disable_mouse terminal.backend.show_cursor terminal.leave_alternate_screen if alternate_screen terminal.backend.disable_raw_mode terminal.backend.flush end |
.run(output: $stdout, input: $stdin, mouse: false, alternate_screen: true) {|terminal, reader| ... } ⇒ void
This method returns an undefined value.
Run a TUI application with automatic setup and teardown.
Sets up the terminal (raw mode, hidden cursor, alternate screen) and yields a terminal and reader. Restores the terminal when the block exits.
167 168 169 170 171 172 173 |
# File 'lib/rubytui.rb', line 167 def run(output: $stdout, input: $stdin, mouse: false, alternate_screen: true) terminal, reader = init(output: output, input: input, mouse: mouse, alternate_screen: alternate_screen) yield terminal, reader ensure restore(terminal, alternate_screen: alternate_screen) if terminal end |