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/input/key_event.rb,
lib/terminal/text/char_width.rb,
lib/terminal/ansi/named_colors.rb
Overview
Terminal access with support for ANSI control codes and BBCode-like embedded text attribute syntax (see Ansi.bbcode). It automagically detects whether your terminal supports ANSI features, like coloring (see Terminal.colors) or the CSIu protocol support (see Terminal.read_key_event, Terminal.on_key_event and Terminal.input_mode). It calculates the display width for Unicode chars (see Text.width) and help you to display text with word-wise line breaks (see Text.each_line).
Defined Under Namespace
Modules: Ansi, Text Classes: KeyEvent
Constant Summary collapse
- VERSION =
The version number of the gem.
'0.18.0'
Attributes collapse
-
.application ⇒ Symbol?
readonly
Terminal application identifier.
-
.input_mode ⇒ :csi_u, ...
readonly
Supported input mode.
-
.output_mode ⇒ :ansi, ...
readonly
Supported output mode.
-
.tui? ⇒ true, false
readonly
Return
trueif the current terminal supports ANSI control codes for input and output.
Output attributes collapse
-
.ansi? ⇒ true, false
readonly
Whether ANSI control codes are supported for output.
-
.colors ⇒ Integer
readonly
Number of supported colors.
-
.columns ⇒ Integer
Screen column count.
-
.pos ⇒ [Integer, Integer]?
Current cursor position.
-
.rows ⇒ Integer
Screen row count.
-
.size ⇒ [Integer, Integer]
Screen size as a tuple of Terminal.rows and Terminal.columns.
-
.true_color? ⇒ true, false
readonly
Whether true colors are supported.
Tool methods collapse
-
.sh(*cmd, **options, &block) ⇒ Object
Execute a command and report command output line by line or capture the output.
Input methods collapse
-
.on_key_event(mouse: false, mouse_move: false, focus: false) {|event| ... } ⇒ true, ...
Event loop for key and mouse events.
-
.read_key_event ⇒ KeyEvent?
Read next KeyEvent from standard input.
Output methods collapse
-
.<<(object) ⇒ Terminal
Writes the given object to the terminal.
-
.print(*objects, bbcode: true) ⇒ nil
Writes the given objects to the terminal.
-
.puts(*objects, bbcode: true) ⇒ nil
Writes the given objects to the terminal.
Output helper methods collapse
-
.hide_alt_screen ⇒ Terminal
Hide the alternate screen.
-
.hide_cursor ⇒ Terminal
Hide the cursor.
-
.show_alt_screen ⇒ Terminal
Show the alternate screen.
-
.show_cursor ⇒ Terminal
Show the cursor.
Class Attribute Details
.ansi? ⇒ true, false (readonly)
Returns whether ANSI control codes are supported for output.
|
|
# File 'lib/terminal/output.rb', line 41
|
.application ⇒ Symbol? (readonly)
Terminal application identifier.
The detection supports a wide range of terminal emulators but may return
nil if an unsupported terminal was found. These are the supported
application IDs:
:alacritty, :amiga, :code_edit, :dg_unix, :docker, :fluent,
:hpterm, :hyper, :iterm, :macos, :mintty, :ms_terminal,
:ncr260, :nsterm, :terminator, :terminology, :termite,
:vt100, :warp, :wezterm, :wyse, :xnuppc
34 |
# File 'lib/terminal.rb', line 34 def application = (@application ||= Detect.application) |
.colors ⇒ Integer (readonly)
Number of supported colors. The detection checks various conditions to find the correct value. The most common values are
16_777_216for 24-bit encoding (true_color? return true)256for 8-Bit encoding52for some Unix terminals with an extended color palette8for 3-/4-bit encoding2if ANSI is not supported in general (ansi? return false)
|
|
# File 'lib/terminal/output.rb', line 49
|
.columns ⇒ Integer
Screen column count. See size for support and detection details.
79 80 81 |
# File 'lib/terminal/output.rb', line 79 def columns = size[1] # # @attribute [w] columns |
.input_mode ⇒ :csi_u, ... (readonly)
Supported input mode.
|
|
# File 'lib/terminal/input.rb', line 8
|
.output_mode ⇒ :ansi, ... (readonly)
Supported output mode.
|
|
# File 'lib/terminal/output.rb', line 22
|
.pos ⇒ [Integer, Integer]?
Current cursor position. This is only available when ANSI is supported (ansi? return true).
|
|
# File 'lib/terminal/output.rb', line 94
|
.rows ⇒ Integer
Screen row count. See size for support and detection details.
90 91 92 |
# File 'lib/terminal/output.rb', line 90 def rows = size[0] # # @attribute [w] rows |
.size ⇒ [Integer, Integer]
Screen size as a tuple of rows and columns.
If the terminal does not support the report of it's dimension or ANSI
is not supported in general then environment variables COLUMNS and
LINES will be used.
If this failed [25, 80] will be returned as default.
Setting the terminal size is not widely supported.
|
|
# File 'lib/terminal/output.rb', line 106
|
.true_color? ⇒ true, false (readonly)
Returns whether true colors are supported.
70 |
# File 'lib/terminal/output.rb', line 70 def true_color? = (colors == 16_777_216) |
.tui? ⇒ true, false (readonly)
Return true if the current terminal supports ANSI control codes for
input and output.
In this case not only all output methods (<<, print, puts) will
forward ANSI control codes to the terminal and translate BBCode
(see Terminal::Ansi.bbcode). But also the input methods read_key_event and
on_key_event will support extended key codes, mouse and focus events.
|
|
# File 'lib/terminal/output.rb', line 7
|
Class Method Details
.<<(object) ⇒ Terminal
Writes the given object to the terminal. Interprets embedded BBCode (see Terminal::Ansi.bbcode).
|
|
# File 'lib/terminal/output.rb', line 129
|
.hide_alt_screen ⇒ Terminal
Hide the alternate screen. Will not send the control code if the alternate screen is not used.
When you called show_alt_screen n-times you need to call hide_alt_screen n-times to show the default screen again.
|
|
# File 'lib/terminal/output.rb', line 193
|
.hide_cursor ⇒ Terminal
Hide the cursor. Will not send the control code if the cursor is already hidden.
When you called hide_cursor n-times you need to call show_cursor n-times to show the cursor again.
|
|
# File 'lib/terminal/output.rb', line 163
|
.on_key_event(mouse: false, mouse_move: false, focus: false) {|event| ... } ⇒ true, ...
Event loop for key and mouse events.
|
|
# File 'lib/terminal/input.rb', line 32
|
.print(*objects, bbcode: true) ⇒ nil
Writes the given objects to the terminal. Optionally interprets embedded BBCode (see Terminal::Ansi.bbcode).
|
|
# File 'lib/terminal/output.rb', line 137
|
.puts(*objects, bbcode: true) ⇒ nil
Writes the given objects to the terminal. Writes a newline after each object that does not already end with a newline sequence in it's String represenation. If called without any arguments, writes a newline only.
Optionally interprets embedded BBCode (see Terminal::Ansi.bbcode).
|
|
# File 'lib/terminal/output.rb', line 146
|
.read_key_event ⇒ KeyEvent?
Read next KeyEvent from standard input.
|
|
# File 'lib/terminal/input.rb', line 26
|
.sh(*cmd, **options) ⇒ [Process::Status, output, error]? .sh(*cmd, **options) {|line, type| ... } ⇒ Process::Status?
Execute a command and report command output line by line or capture the output.
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/terminal.rb', line 91 def sh(*cmd, **, &block) raise(ArgumentError, 'command expected') if cmd.empty? return Shell.exec(cmd, **, &block) if block_given? out = [] err = [] [ Shell.exec(cmd, **) do |line, type| (type == :error ? err : out) << line end, out, err ] end |
.show_alt_screen ⇒ Terminal
Show the alternate screen. Will not send the control code if the alternate screen is already used.
When you called show_alt_screen n-times you need to call hide_alt_screen n-times to show the default screen again.
|
|
# File 'lib/terminal/output.rb', line 183
|
.show_cursor ⇒ Terminal
Show the cursor. Will not send the control code if the cursor is not hidden.
When you called hide_cursor n-times you need to call show_cursor n-times to show the cursor again.
|
|
# File 'lib/terminal/output.rb', line 173
|