Class: Kaitai::ConsoleANSI

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeConsoleANSI

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

#colsObject (readonly)

Returns the value of attribute cols.



8
9
10
# File 'lib/kaitai/console_ansi.rb', line 8

def cols
  @cols
end

#rowsObject (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

#clearObject



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_sizeObject



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_charObject

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_mappedObject



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_colorsObject



91
92
93
# File 'lib/kaitai/console_ansi.rb', line 91

def reset_colors
  print @seq_sgr0
end