Class: Kaitai::ConsoleANSI
- Inherits:
-
Object
- Object
- Kaitai::ConsoleANSI
- Defined in:
- lib/kaitai/console_ansi.rb
Constant Summary collapse
- COLORS =
{ :black => 0, :gray => 7, :gray0 => 232, :gray1 => 233, :gray2 => 234, :gray3 => 235, :gray4 => 236, :gray5 => 237, :gray6 => 238, :gray7 => 239, :gray8 => 240, :gray9 => 241, :gray10 => 242, :gray11 => 243, :gray12 => 244, :gray13 => 245, :gray14 => 246, :gray15 => 247, :gray16 => 248, :gray17 => 249, :gray18 => 250, :gray19 => 251, :gray20 => 252, :gray21 => 253, :gray22 => 254, :gray23 => 255, }
- KEY_MAP =
{ "\t" => :tab, "\r" => :enter, "\e[A" => :up_arrow, "\e[B" => :down_arrow, "\e[C" => :right_arrow, "\e[D" => :left_arrow, "\e[5~" => :pg_up, "\e[6~" => :pg_dn, "\e[H" => :home, "\e[F" => :end, }
Instance Attribute Summary collapse
-
#cols ⇒ Object
readonly
Returns the value of attribute cols.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
Instance Method Summary collapse
- #bg_color=(col) ⇒ Object
- #clear ⇒ Object
- #fg_color=(col) ⇒ Object
- #get_term_size ⇒ Object
-
#goto(x, y) ⇒ Object
Put the cursor up to screen position (x, y).
-
#initialize ⇒ ConsoleANSI
constructor
A new instance of ConsoleANSI.
- #on_resize=(handler) ⇒ Object
-
#read_char ⇒ Object
Reads keypresses from the user including 2 and 3 escape character sequences.
- #read_char_mapped ⇒ Object
- #reset_colors ⇒ Object
Constructor Details
#initialize ⇒ ConsoleANSI
Returns a new instance of ConsoleANSI.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/kaitai/console_ansi.rb', line 11 def initialize get_term_size @seq_clear = `tput clear` @seq_sgr0 = `tput sgr0` @seq_fgcolor = [] @seq_bgcolor = [] @on_resize = nil Signal.trap('SIGWINCH', proc { get_term_size @on_resize.call if @on_resize }) end |
Instance Attribute Details
#cols ⇒ Object (readonly)
Returns the value of attribute cols.
8 9 10 |
# File 'lib/kaitai/console_ansi.rb', line 8 def cols @cols end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
9 10 11 |
# File 'lib/kaitai/console_ansi.rb', line 9 def rows @rows end |
Instance Method Details
#bg_color=(col) ⇒ Object
84 85 86 87 88 89 |
# File 'lib/kaitai/console_ansi.rb', line 84 def bg_color=(col) #print @seq_bgcolor[col] ||= `tput setab #{col}` code = COLORS[col] raise "Invalid color: #{col}" unless code print "\e[48;5;#{code}m" end |
#clear ⇒ Object
36 37 38 |
# File 'lib/kaitai/console_ansi.rb', line 36 def clear print @seq_clear end |
#fg_color=(col) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/kaitai/console_ansi.rb', line 77 def fg_color=(col) #print @seq_fgcolor[col] ||= `tput setaf #{col}` code = COLORS[col] raise "Invalid color: #{col}" unless code print "\e[38;5;#{code}m" end |
#get_term_size ⇒ Object
32 33 34 |
# File 'lib/kaitai/console_ansi.rb', line 32 def get_term_size @rows, @cols = IO.console.winsize end |
#goto(x, y) ⇒ Object
Put the cursor up to screen position (x, y). First line is 0, first column is 0.
43 44 45 46 |
# File 'lib/kaitai/console_ansi.rb', line 43 def goto(x, y) #print `tput cup #{y} #{x}` printf "\e[%d;%dH", y + 1, x + 1 end |
#on_resize=(handler) ⇒ Object
28 29 30 |
# File 'lib/kaitai/console_ansi.rb', line 28 def on_resize=(handler) @on_resize = handler end |
#read_char ⇒ Object
Reads keypresses from the user including 2 and 3 escape character sequences.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/kaitai/console_ansi.rb', line 96 def read_char $stdin.echo = false $stdin.raw! input = $stdin.getc.chr if input == "\e" then input << $stdin.read_nonblock(3) rescue nil input << $stdin.read_nonblock(2) rescue nil end ensure $stdin.echo = true $stdin.cooked! return input end |
#read_char_mapped ⇒ Object
112 113 114 115 116 |
# File 'lib/kaitai/console_ansi.rb', line 112 def read_char_mapped c = read_char c2 = KEY_MAP[c] c2 ? c2 : c end |
#reset_colors ⇒ Object
91 92 93 |
# File 'lib/kaitai/console_ansi.rb', line 91 def reset_colors print @seq_sgr0 end |