Class: Brut::TUI::Terminal
- Inherits:
-
Object
- Object
- Brut::TUI::Terminal
- Defined in:
- lib/brut/tui/terminal.rb
Overview
Stores metdata about the current temrinal in use. This should provide any metadata about the terminal that can be reliably determined.
Instance Method Summary collapse
-
#background_color ⇒ Object
Best attempt to guess the background color of the current terminal.
-
#cols ⇒ Object
Number of columns for the current terminal.
-
#initialize ⇒ Terminal
constructor
A new instance of Terminal.
-
#io ⇒ Object
The IO used for outputing information to the terminal.
-
#rows ⇒ Object
Number of rows for the current terminal.
-
#stdin ⇒ Object
The IO to use for requesting input from the user.
Constructor Details
#initialize ⇒ Terminal
Returns a new instance of Terminal.
9 10 11 12 13 |
# File 'lib/brut/tui/terminal.rb', line 9 def initialize trap("WINCH") do @winsize = IO.console.winsize end end |
Instance Method Details
#background_color ⇒ Object
Best attempt to guess the background color of the current terminal. This will not refresh if that color is changed, and its behavior will default to black if determining the color doesn't work or isn't supported.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/brut/tui/terminal.rb', line 29 def background_color @bakground_color ||= begin io.write "\e]11;?\a" io.flush result = nil begin # Read the reply, which should look like: # ESC ] 11 ; rgb:0000/0000/0000 BEL Timeout.timeout(0.1) do buf = +"" loop do ch = stdin.getch buf << ch break if ch == "\a" || buf.end_with?("\e\\") # BEL or ST terminator end result = buf end rescue Timeout::Error # no reply within 100ms — terminal probably doesn’t support it end rgb = [ 0, 0, 0 ] if result if result =~ /\e\]11;([^ \a\e]*)[\a\e\\]/ color = Regexp.last_match(1) parts = color[/rgb:(.*)/, 1]&.split("/") if parts rgb = parts.map { it.to_i(16) / 0xffff.to_f * 256 }.map(&:to_i) end end end rgb end end |
#cols ⇒ Object
Number of columns for the current terminal. Should be consistent even after a resize
18 |
# File 'lib/brut/tui/terminal.rb', line 18 def cols = winsize[1] |
#io ⇒ Object
The IO used for outputing information to the terminal. Prefer this over STDOUT.
21 |
# File 'lib/brut/tui/terminal.rb', line 21 def io = $stdout |
#rows ⇒ Object
Number of rows for the current terminal. Should be consistent even after a resize
16 17 |
# File 'lib/brut/tui/terminal.rb', line 16 def rows = winsize[0] # Number of columns for the current terminal. Should be consistent even after a resize |
#stdin ⇒ Object
The IO to use for requesting input from the user
24 |
# File 'lib/brut/tui/terminal.rb', line 24 def stdin = $stdin |