class Doublespeak::Display

Attributes

format_result[R]
format_result_textmatch[R]
format_selected_result[R]
istream[R]
origin_col[R]
origin_row[R]
ostream[R]
reader[R]
screen_width[R]

Public Class Methods

new(options) click to toggle source
# File lib/doublespeak/display.rb, line 10
def initialize(options)
  @ostream = options[:ostream] || $stdout
  @istream = options[:istream] || $stdin
  @reader = TTY::Reader.new

  colorizer = Pastel::new
  @format_result = options[:format_result] || colorizer.white.dim.detach
  @format_result_textmatch = options[:format_result_textmatch] || colorizer.cyan.dim.detach
  @format_selected_result = options[:format_selected_result] || colorizer.green.dim.detach

  c, r = *cursor_position
  @origin_col = options[:origin_col] || c
  @origin_row = options[:origin_row] || r
  @screen_width = options[:screen_width] || IO.console.winsize[1]
end

Public Instance Methods

clear_to_end_of_line() click to toggle source
# File lib/doublespeak/display.rb, line 34
def clear_to_end_of_line
  write_escaped "[K"
end
move_to_origin() click to toggle source
# File lib/doublespeak/display.rb, line 30
def move_to_origin
  write_escaped "[#{origin_row};#{origin_col}H"
end
read() click to toggle source
# File lib/doublespeak/display.rb, line 46
def read
  reader.read_char
end
set_cursor_visible(visible=true) click to toggle source
# File lib/doublespeak/display.rb, line 38
def set_cursor_visible(visible=true)
  write_escaped(visible ? "[?25h" : "[?25l")
end
width() click to toggle source
# File lib/doublespeak/display.rb, line 26
def width
  screen_width - origin_col
end
write(str) click to toggle source
# File lib/doublespeak/display.rb, line 42
def write(str)
  ostream << str
end

Private Instance Methods

cursor_position() click to toggle source
# File lib/doublespeak/display.rb, line 59
def cursor_position
  res = ''
  istream.raw do |stream|
    write_escaped("[6n")
    while (c = stream.getc) != 'R'
      res << c if c
    end
  end
  m = res.match(/(?<row>\d+);(?<column>\d+)/)
  [Integer(m[:column]), Integer(m[:row])]
end
write_escaped(seq) click to toggle source
# File lib/doublespeak/display.rb, line 54
def write_escaped(seq)
  ostream << "\e#{seq}"
  ostream.flush
end