Class: Kaitai::ConsoleANSI

Inherits:
Object
  • Object
show all
Defined in:
lib/kaitai/console_ansi.rb

Constant Summary collapse

COLORS =
{
  black: 0,
  red: 1,
  green: 2,
  yellow: 3,
  blue: 4,
  magenta: 5,
  cyan: 6,
  white: 7,
  gray: 8,
  bright_red: 9,
  bright_green: 10,
  bright_yellow: 11,
  bright_blue: 12,
  bright_magenta: 13,
  bright_cyan: 14,
  bright_white: 15
}.freeze
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
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConsoleANSI

Returns a new instance of ConsoleANSI.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/kaitai/console_ansi.rb', line 10

def initialize
  load_term_size

  @seq_clear = `tput clear`
  @seq_sgr0 = `tput sgr0`

  @seq_fgcolor = []
  @seq_bgcolor = []

  @on_resize = nil

  Signal.trap('SIGWINCH', proc {
    load_term_size
    @on_resize&.call(true)
  })
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

#on_resize=(value) ⇒ Object (writeonly)

Sets the attribute on_resize

Parameters:

  • value

    the value to set the attribute on_resize to.



27
28
29
# File 'lib/kaitai/console_ansi.rb', line 27

def on_resize=(value)
  @on_resize = value
end

#rowsObject (readonly)

Returns the value of attribute rows.



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

def rows
  @rows
end

Instance Method Details

#bg_color=(col) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/kaitai/console_ansi.rb', line 70

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



33
34
35
# File 'lib/kaitai/console_ansi.rb', line 33

def clear
  print @seq_clear
end

#fg_color=(col) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/kaitai/console_ansi.rb', line 62

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.



38
39
40
41
# File 'lib/kaitai/console_ansi.rb', line 38

def goto(x, y)
  # print `tput cup #{y} #{x}`
  printf "\e[%d;%dH", y + 1, x + 1
end

#load_term_sizeObject



29
30
31
# File 'lib/kaitai/console_ansi.rb', line 29

def load_term_size
  @rows, @cols = IO.console.winsize
end

#read_charObject

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
98
99
100
101
102
103
# File 'lib/kaitai/console_ansi.rb', line 83

def read_char
  $stdin.echo = false
  $stdin.raw!

  input = $stdin.getc.chr
  if input == "\e"
    begin
      # may return less than 3 bytes because it can only read as many bytes as are
      # currently available
      up_to_3bytes = $stdin.read_nonblock(3)
      input << up_to_3bytes
    rescue IO::WaitReadable
      # not an ANSI sequence - the user probably just pressed the Esc key
    end
  end

  $stdin.echo = true
  $stdin.cooked!

  input
end

#read_char_mappedObject



105
106
107
108
109
# File 'lib/kaitai/console_ansi.rb', line 105

def read_char_mapped
  c = read_char
  c2 = KEY_MAP[c]
  c2 || c
end

#reset_colorsObject



78
79
80
# File 'lib/kaitai/console_ansi.rb', line 78

def reset_colors
  print @seq_sgr0
end