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
-
#goto(x, y) ⇒ Object
Put the cursor up to screen position (x, y).
-
#initialize ⇒ ConsoleANSI
constructor
A new instance of ConsoleANSI.
-
#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 |
# File 'lib/kaitai/console_ansi.rb', line 11 def initialize # Normal POSIX way to determine console parameters @cols = `tput cols`.to_i @rows = `tput lines`.to_i @seq_clear = `tput clear` @seq_sgr0 = `tput sgr0` @seq_fgcolor = [] @seq_bgcolor = [] 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
71 72 73 74 75 76 |
# File 'lib/kaitai/console_ansi.rb', line 71 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
23 24 25 |
# File 'lib/kaitai/console_ansi.rb', line 23 def clear print @seq_clear end |
#fg_color=(col) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/kaitai/console_ansi.rb', line 64 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 |
#goto(x, y) ⇒ Object
Put the cursor up to screen position (x, y). First line is 0, first column is 0.
30 31 32 33 |
# File 'lib/kaitai/console_ansi.rb', line 30 def goto(x, y) #print `tput cup #{y} #{x}` printf "\e[%d;%dH", y + 1, x + 1 end |
#read_char ⇒ Object
Reads keypresses from the user including 2 and 3 escape character sequences.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/kaitai/console_ansi.rb', line 83 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
99 100 101 102 103 |
# File 'lib/kaitai/console_ansi.rb', line 99 def read_char_mapped c = read_char c2 = KEY_MAP[c] c2 ? c2 : c end |
#reset_colors ⇒ Object
78 79 80 |
# File 'lib/kaitai/console_ansi.rb', line 78 def reset_colors print @seq_sgr0 end |