Module: Terminal
- Defined in:
- lib/terminal.rb,
lib/terminal/ansi.rb,
lib/terminal/text.rb,
lib/terminal/input.rb,
lib/terminal/detect.rb,
lib/terminal/version.rb,
lib/terminal/ansi/attributes.rb,
lib/terminal/input/csiu_mode.rb,
lib/terminal/text/char_width.rb,
lib/terminal/ansi/named_colors.rb,
lib/terminal/input/legacy_mode.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 and Terminal.input_mode). It calculates the display width for Unicode chars (see Text.width) and help you to display text with line formatting (see Text.each_line).
Defined Under Namespace
Constant Summary collapse
- VERSION =
The version number of the gem.
'0.9.3'
Class Attribute Summary collapse
-
.ansi? ⇒ Boolean
readonly
Return true when the current terminal supports ANSI control codes.
-
.application ⇒ Symbol?
readonly
Terminal application identifier.
-
.colors ⇒ Integer
readonly
Number of supported colors.
-
.columns ⇒ Integer
Screen column count.
-
.input_mode ⇒ :csi_u, ...
readonly
Supported input mode.
-
.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? ⇒ Boolean
readonly
Whether true colors are supported.
Class Method Summary collapse
-
.<<(object) ⇒ Terminal
Writes the given object to the terminal.
-
.hide_cursor ⇒ Terminal
Hide the cursor.
-
.print(*objects, bbcode: true) ⇒ nil
Writes the given objects to the terminal.
-
.puts(*objects, bbcode: true) ⇒ nil
Writes the given objects to the terminal.
-
.read_key(mode: :named) ⇒ String, [String, String]
Read next keyboard input.
-
.show_cursor ⇒ Terminal
Show the cursor.
Class Attribute Details
.ansi? ⇒ Boolean (readonly)
27 |
# File 'lib/terminal.rb', line 27 def ansi? = (@mode == :ansi) |
.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
,:fluent
,:ghostty
,:hpterm
,:hyper
,:iterm
,:macos
,:mintty
,:ms_terminal
,:ncr260
,:nsterm
,:rio
,:tabby
,:terminator
,:terminology
,:terminus
,:termite
,:tmux
,:vscode
,:vt100
,:warp
,:wezterm
,:wyse
,:xnuppc
65 |
# File 'lib/terminal.rb', line 65 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_216
for 24-bit encoding (true_color? return true)256
for 8-Bit encoding52
for some Unix terminals with an extended color palette8
for 3-/4-bit encoding2
if ANSI is not supported in general (ansi? return false)
79 |
# File 'lib/terminal.rb', line 79 def colors = (@colors ||= ansi? ? Detect.colors : 2) |
.columns ⇒ Integer
Screen column count. See size for support and detection details.
86 |
# File 'lib/terminal.rb', line 86 def columns = size[1] |
.input_mode ⇒ :csi_u, ... (readonly)
Supported input mode.
14 15 16 17 18 |
# File 'lib/terminal/input.rb', line 14 def input_mode return @inp_mode.type if (@inp ||= find_inp) == READ_TTY_INP return :dumb if @inp == READ_DUMB_INP :error end |
.pos ⇒ [Integer, Integer]?
Current cursor position. This is only available when ANSI is supported (ansi? return true).
99 100 101 102 103 |
# File 'lib/terminal.rb', line 99 def pos @con&.cursor rescue IOError @con = nil end |
.rows ⇒ Integer
Screen row count. See size for support and detection details.
117 |
# File 'lib/terminal.rb', line 117 def rows = size[0] |
.size ⇒ [Integer, Integer]
137 138 139 140 141 142 |
# File 'lib/terminal.rb', line 137 def size @size ||= @inf&.winsize || _default_size rescue IOError @inf = nil @size = _default_size end |
.true_color? ⇒ Boolean (readonly)
Returns whether true colors are supported.
154 |
# File 'lib/terminal.rb', line 154 def true_color? = (colors == 16_777_216) |
Class Method Details
.<<(object) ⇒ Terminal
Writes the given object to the terminal. Interprets embedded BBCode.
184 185 186 187 188 189 190 |
# File 'lib/terminal.rb', line 184 def <<(object) @out.write(@bbcode[object]) unless object.nil? || @out.nil? self rescue IOError @out = nil self end |
.hide_cursor ⇒ Terminal
Hide the cursor. Will not send the control code if the cursor is already hidden.
To show the cursor again call show_cursor.
162 163 164 165 |
# File 'lib/terminal.rb', line 162 def hide_cursor _write(Ansi::CURSOR_HIDE) if ansi? && (@cc += 1) == 1 self end |
.print(*objects, bbcode: true) ⇒ nil
Writes the given objects to the terminal. Optionally interprets embedded BBCode.
198 199 200 201 202 203 204 205 |
# File 'lib/terminal.rb', line 198 def print(*objects, bbcode: true) return unless @out bbcode = bbcode ? @bbcode : @nobbcode objects.flatten.each { @out.write(bbcode[_1]) unless _1.nil? } nil rescue IOError @out = nil end |
.puts(*objects, bbcode: true) ⇒ nil
Writes the given objects to the terminal. Writes a newline after each objects 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.
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/terminal.rb', line 216 def puts(*objects, bbcode: true) return unless @out objects.flatten! if objects.empty? @out.write("\n") return end bbcode = bbcode ? @bbcode : @nobbcode objects.each do |s| next @out.write("\n") if s.nil? @out.write(s = bbcode[s]) @out.write("\n") if s[-1] != "\n" end nil rescue IOError @out = nil end |
.read_key(mode: :named) ⇒ String, [String, String]
Read next keyboard input.
The input will be returned as named key codes like "Ctrl+c" by default.
This can be changed by mode
.
29 30 31 32 33 34 |
# File 'lib/terminal/input.rb', line 29 def read_key(mode: :named) key = (@inp ||= find_inp).call or return return key if mode == :raw return key, @inp_mode.key_name(key) if mode == :both @inp_mode.key_name(key) || key end |
.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.
174 175 176 177 |
# File 'lib/terminal.rb', line 174 def show_cursor _write(Ansi::CURSOR_SHOW) if @cc > 0 && (@cc -= 1).zero? self end |