Module: Terminal
- Defined in:
- lib/terminal.rb,
lib/terminal/ansi.rb,
lib/terminal/text.rb,
lib/terminal/input.rb,
lib/terminal/shell.rb,
lib/terminal/detect.rb,
lib/terminal/output.rb,
lib/terminal/version.rb,
lib/terminal/input/ansi.rb,
lib/terminal/input/dumb.rb,
lib/terminal/output/ansi.rb,
lib/terminal/output/dumb.rb,
lib/terminal/text/formatter.rb,
lib/terminal/input/key_event.rb,
lib/terminal/text/char_width.rb,
lib/terminal/ansi/named_colors.rb,
lib/terminal/ansi/screen_viewer.rb
Overview
Terminal I/O with ANSI support, BBCode markup, Unicode display width, keyboard/mouse events, and text formatting.
The appropriate output and input modes are selected automatically at load time based on terminal capabilities. Use the class-level methods for all interaction.
Defined Under Namespace
Modules: Ansi, Text Classes: KeyEvent
Constant Summary collapse
- VERSION =
The version number of the gem.
'1.0.3'
Attributes collapse
-
.application ⇒ Symbol?
readonly
Detect the terminal emulator application.
-
.input_mode ⇒ Symbol
readonly
Detect and return the current input mode.
-
.output_mode ⇒ :ansi, ...
readonly
Current output mode.
Output Attributes collapse
-
.ansi? ⇒ true, false
readonly
Whether ANSI escape codes are supported.
-
.colors ⇒ Integer
readonly
Number of colors supported by the terminal.
-
.columns ⇒ Integer
Terminal width in columns.
-
.pos ⇒ Array<Integer, Integer>?
Current cursor position.
-
.resized? ⇒ true, false
readonly
Whether the terminal has been resized since the last size query.
-
.rows ⇒ Integer
Terminal height in rows.
-
.size ⇒ Array<Integer, Integer>
The terminal size.
-
.true_color? ⇒ true, false
readonly
Whether the terminal supports true color (24-bit, 16.7 million colors).
-
.tui? ⇒ true, false
readonly
Whether the terminal supports full TUI interaction (ANSI output with CSI-u or legacy keyboard input).
Tool methods collapse
-
.sh(*cmd) ⇒ Object
Execute a shell command.
Input methods collapse
-
.on_key_event(mouse: false, mouse_move: false, focus: false) {|event| ... } ⇒ nil, false
Enter a key event loop, yielding each event to the block.
-
.read_key_event ⇒ KeyEvent, false
Read a single key event (blocking).
Output Methods collapse
-
.<<(object) ⇒ self
Output an object with BBCode markup processing.
-
.fputs(*objects, bbcode: true, spaces: true, eol: true, align: nil, width: nil, height: nil, padding: nil, prefix: nil, suffix: nil) ⇒ nil
Formatted output with word-wrapping, alignment, and padding.
-
.hide_alt_screen ⇒ self
Switch back from the alternate screen buffer.
-
.hide_cursor ⇒ self
Hide the cursor.
-
.on_resize { ... } ⇒ self
Register a callback to be invoked when the terminal is resized.
-
.print(*objects, bbcode: true) ⇒ nil
Print objects to the terminal.
-
.puts(*objects, bbcode: true) ⇒ nil
Print objects to the terminal followed by newlines.
-
.raw_write(object) ⇒ Integer?
Write raw bytes directly to the terminal without BBCode or ANSI processing.
-
.show_alt_screen ⇒ self
Switch to the alternate screen buffer.
-
.show_cursor ⇒ self
Show the cursor.
Class Attribute Details
.ansi? ⇒ true, false (readonly)
Whether ANSI escape codes are supported.
|
|
# File 'lib/terminal/output.rb', line 39
|
.application ⇒ Symbol? (readonly)
Detect the terminal emulator application.
Known applications: +:alacritty+, +:amiga+, +:code_edit+, +:dg_unix+, +:docker+, +:fluent+, +:hpterm+, +:hyper+, +:iterm+, +:kitty+, +:macos+, +:mintty+, +:ms_terminal+, +:ncr260+, +:nsterm+, +:terminator+, +:terminology+, +:termite+, +:vscode+, +:vt100+, +:warp+, +:wyse+, +:xnuppc+
Any unrecognized +TERM_PROGRAM+ value is converted to a sanitized symbol. Returns +nil+ when no terminal can be identified.
65 |
# File 'lib/terminal.rb', line 65 def application = (@application ||= Detect.application) |
.colors ⇒ Integer (readonly)
Number of colors supported by the terminal.
|
|
# File 'lib/terminal/output.rb', line 48
|
.columns ⇒ Integer
Terminal width in columns.
77 |
# File 'lib/terminal/output.rb', line 77 def columns = size[1] |
.input_mode ⇒ Symbol (readonly)
Detect and return the current input mode.
|
|
# File 'lib/terminal/input.rb', line 10
|
.output_mode ⇒ :ansi, ... (readonly)
Current output mode.
|
|
# File 'lib/terminal/output.rb', line 9
|
.pos ⇒ Array<Integer, Integer>?
Current cursor position. This is only available when ANSI is supported (ansi? return true).
|
|
# File 'lib/terminal/output.rb', line 87
|
.resized? ⇒ true, false (readonly)
Whether the terminal has been resized since the last size query.
101 |
# File 'lib/terminal/output.rb', line 101 def resized? = @size.nil? |
.rows ⇒ Integer
Terminal height in rows.
85 |
# File 'lib/terminal/output.rb', line 85 def rows = size[0] |
.size ⇒ Array<Integer, Integer>
The terminal size.
|
|
# File 'lib/terminal/output.rb', line 65
|
.true_color? ⇒ true, false (readonly)
Whether the terminal supports true color (24-bit, 16.7 million colors).
63 |
# File 'lib/terminal/output.rb', line 63 def true_color? = (colors == 16_777_216) |
.tui? ⇒ true, false (readonly)
Whether the terminal supports full TUI interaction (ANSI output with CSI-u or legacy keyboard input).
|
|
# File 'lib/terminal/output.rb', line 30
|
Class Method Details
.<<(object) ⇒ self
Output an object with BBCode markup processing.
|
|
# File 'lib/terminal/output.rb', line 109
|
.fputs(*objects, bbcode: true, spaces: true, eol: true, align: nil, width: nil, height: nil, padding: nil, prefix: nil, suffix: nil) ⇒ nil
Formatted output with word-wrapping, alignment, and padding.
|
|
# File 'lib/terminal/output.rb', line 142
|
.hide_alt_screen ⇒ self
Reference-counted; safe for nested use.
Switch back from the alternate screen buffer.
|
|
# File 'lib/terminal/output.rb', line 201
|
.hide_cursor ⇒ self
Reference-counted; safe for nested use.
Hide the cursor.
Calls are reference-counted — nested calls are safe; the cursor is only hidden on the first call and shown when the last matching show_cursor is called.
|
|
# File 'lib/terminal/output.rb', line 167
|
.on_key_event(mouse: false, mouse_move: false, focus: false) {|event| ... } ⇒ nil, false
Enter a key event loop, yielding each event to the block.
Enables optional mouse and focus event reporting. The block receives KeyEvent instances until input ends or the block breaks.
|
|
# File 'lib/terminal/input.rb', line 26
|
.on_resize { ... } ⇒ self
Register a callback to be invoked when the terminal is resized.
218 219 220 221 |
# File 'lib/terminal/output.rb', line 218 def on_resize(&block) @on_resize = block self end |
.print(*objects, bbcode: true) ⇒ nil
Print objects to the terminal.
|
|
# File 'lib/terminal/output.rb', line 119
|
.puts(*objects, bbcode: true) ⇒ nil
Print objects to the terminal followed by newlines.
|
|
# File 'lib/terminal/output.rb', line 131
|
.raw_write(object) ⇒ Integer?
Write raw bytes directly to the terminal without BBCode or ANSI processing.
|
|
# File 'lib/terminal/output.rb', line 159
|
.read_key_event ⇒ KeyEvent, false
Read a single key event (blocking).
|
|
# File 'lib/terminal/input.rb', line 48
|
.sh(*cmd, **options) ⇒ Array<Process::Status, Array<String>, Array<String>> .sh(*cmd, **options) {|line, type| ... } ⇒ Process::Status
Execute a shell command.
99 100 101 102 103 104 105 |
# File 'lib/terminal.rb', line 99 def sh(*cmd, **, &) raise(ArgumentError, 'command expected') if cmd.empty? return Shell.run(*cmd, **, &) if block_given? out = [] err = [] [Shell.run(*cmd, **) { |l, t| (t == :error ? err : out) << l }, out, err] end |
.show_alt_screen ⇒ self
Reference-counted; safe for nested use.
Switch to the alternate screen buffer.
|
|
# File 'lib/terminal/output.rb', line 191
|
.show_cursor ⇒ self
Reference-counted; safe for nested use.
Show the cursor.
|
|
# File 'lib/terminal/output.rb', line 181
|