module IOHelper

Attributes

bottomline[RW]
keys[RW]
output[RW]
winsize[RW]

Public Instance Methods

clear() click to toggle source
# File lib/inquirer/spec_helpers.rb, line 19
def clear
  @output = ''
end
get_char() click to toggle source

read a character the user enters on console. This call is synchronous blocking. this is taken from: www.alecjacobson.com/weblog/?p=75 and: gist.github.com/acook/4190379

# File lib/inquirer/utils/iohelper.rb, line 26
def get_char
  begin
    # save previous state of stty
    old_state = `stty -g`
    # disable echoing and enable raw (not having to press enter)
    system 'stty raw -echo'
    char = STDIN.getc.chr
    # gather next two characters of special keys
    if char == "\e"
      char << STDIN.read_nonblock(3) rescue nil
      char << STDIN.read_nonblock(2) rescue nil
    end

    # restore previous state of stty
    system "stty #{old_state}"
  end

  key = IOChar.char_to_key(char)

  if key == 'ctrl-c' or key == 'ctrl-d'
    raise Interrupt
  end

  char
end
output_plain() click to toggle source
# File lib/inquirer/spec_helpers.rb, line 23
def output_plain
  self.plain_chars(output)
end
plain_chars(string) click to toggle source
# File lib/inquirer/utils/iohelper.rb, line 122
def plain_chars(string)
  string.gsub(/\e\[([;\dA-Z]+)?m?/, '')
end
read_char(&block) click to toggle source
# File lib/inquirer/spec_helpers.rb, line 6
def read_char &block
  Array(@keys).each do |key|
    break unless block.(key)
  end
end
render(prompt, bottomline = nil) click to toggle source
# File lib/inquirer/spec_helpers.rb, line 12
def render(prompt, bottomline = nil)
  @rendered   = wrap(prompt)
  @bottomline = bottomline

  @output = rendered_output
end
reset() click to toggle source
# File lib/inquirer/spec_helpers.rb, line 27
def reset
  clear
  @winsize = [10, 2000]
  @keys    = nil
end
without_cursor() { || ... } click to toggle source

hides the cursor and ensure the cursor be visible at the end

# File lib/inquirer/utils/iohelper.rb, line 82
def without_cursor
  # tell the terminal to hide the cursor
  print `tput civis`
  begin
    # run the block
    yield
  ensure
    # tell the terminal to show the cursor
    print `tput cnorm`
  end
end
wrap(string) click to toggle source

inspired by apidock.com/rails/ActionView/Helpers/TextHelper/word_wrap maybe interesting: www.safaribooksonline.com/library/view/ruby-cookbook/0596523696/ch01s15.html

# File lib/inquirer/utils/iohelper.rb, line 96
def wrap(string)

  height, width = IOHelper.winsize

  keep_trailing_newline = false
  if string[-1, 1] == IOChar.newline
    keep_trailing_newline = true
  end

  result = string.split(IOChar.newline).collect! do |line|
    if line.length > width
      line.gsub(/(.{1,#{width}})(\s+|$)/, "\\1#{ IOChar.newline }")
    else
      line
    end
  end * IOChar.newline

  if keep_trailing_newline
    result += IOChar.newline
  else
    result.chomp!
  end

  result + IOChar.clear_line
end

Private Instance Methods

rendered_output() click to toggle source
# File lib/inquirer/utils/iohelper.rb, line 128
def rendered_output

  output = ''

  if @bottomline
    # determine how many lines to move up
    lines   = @rendered.scan(/\n/).size + 1
    output += IOChar.newline * lines + @bottomline + IOChar.cursor_left * @bottomline.size + IOChar.cursor_up * lines
  end
  output += @rendered

  plain_last_line = plain_chars(@rendered.lines.last)
  output += IOChar.newline + IOChar.cursor_up + IOChar.cursor_right * plain_last_line.length

  output
end