class Kaitai::ConsoleANSI

Constants

COLORS
KEY_MAP

Attributes

cols[R]
rows[R]

Public Class Methods

new() click to toggle source
# 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

Public Instance Methods

bg_color=(col) click to toggle source
# 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() click to toggle source
# File lib/kaitai/console_ansi.rb, line 36
def clear
  print @seq_clear
end
fg_color=(col) click to toggle source
# 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() click to toggle source
# File lib/kaitai/console_ansi.rb, line 32
def get_term_size
  @rows, @cols = IO.console.winsize
end
goto(x, y) click to toggle source

Put the cursor up to screen position (x, y). First line is 0, first column is 0.

# 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) click to toggle source
# File lib/kaitai/console_ansi.rb, line 28
def on_resize=(handler)
  @on_resize = handler
end
read_char() click to toggle source

Reads keypresses from the user including 2 and 3 escape character sequences.

# 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() click to toggle source
# 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() click to toggle source
# File lib/kaitai/console_ansi.rb, line 91
def reset_colors
  print @seq_sgr0
end