module IOHelper

Constants

KEYS

Public Instance Methods

carriage_return() click to toggle source
# File lib/mad_clibs/util/iohelper.rb, line 129
def carriage_return;  "\r"    end
char_left() click to toggle source
# File lib/mad_clibs/util/iohelper.rb, line 132
def char_left;        "\e[D" end
char_right() click to toggle source
# File lib/mad_clibs/util/iohelper.rb, line 133
def char_right;       "\e[C" end
char_to_raw(char) click to toggle source
# File lib/mad_clibs/util/iohelper.rb, line 125
def char_to_raw char
  KEYS.fetch char, char
end
clear() click to toggle source

clear the console based on the last text rendered

# File lib/mad_clibs/util/iohelper.rb, line 103
def clear
  # get console window height and width
  h,w = IOHelper.winsize
  # determine how many lines to move up
  n = @rendered.scan(/\n/).length
  # jump back to the first position and clear the line
  print carriage_return + ( line_up + clear_line ) * n + clear_line
end
clear_line() click to toggle source
# File lib/mad_clibs/util/iohelper.rb, line 131
def clear_line;       "\e[0K" end
line_up() click to toggle source
# File lib/mad_clibs/util/iohelper.rb, line 130
def line_up;          "\e[A"  end
read_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

# File lib/mad_clibs/util/iohelper.rb, line 33
def read_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"
    c = STDIN.getc.chr
    # gather next two characters of special keys
    if(c=="\e")
      extra_thread = Thread.new{
        c = c + STDIN.getc.chr
        c = c + STDIN.getc.chr
      }
      # wait just long enough for special keys to get swallowed
      extra_thread.join(0.00001)
      # kill thread so not-so-long special keys don't wait on getc
      extra_thread.kill
    end
  rescue => ex
    puts "#{ex.class}: #{ex.message}"
    puts ex.backtrace
  ensure
    # restore previous state of stty
    system "stty #{old_state}"
  end
  return c
end
read_key(with_exit_codes = true, return_char = false) click to toggle source

Read a keypress on console. Return the key name (e.g. “space”, “a”, “B”) Params:

with_exit_codes

Bool whether to throw Interrupts when the user presses

ctrl-c and ctrl-d. (true by default)
# File lib/mad_clibs/util/iohelper.rb, line 65
def read_key with_exit_codes = true, return_char = false
  char = read_char
  raise Interrupt if with_exit_codes and ( char == "\003" or char == "\004" )
  if return_char then char else char_to_raw char end
end
read_key_while(return_char = false, &block) click to toggle source

Get each key the user presses and hand it one by one to the block. Do this as long as the block returns truthy Params:

+&block+

Proc a block that receives a user key and returns truthy or falsy

# File lib/mad_clibs/util/iohelper.rb, line 75
def read_key_while return_char = false, &block
  STDIN.noecho do
    # as long as the block doen't return falsy,
    # read the user input key and sned it to the block
    while block.( IOHelper.read_key true, return_char )
    end
  end
end
render(s) click to toggle source

Render a text to the prompt

# File lib/mad_clibs/util/iohelper.rb, line 91
def render(s)
  @rendered = s
  print s
end
rerender(s) click to toggle source

Clear the prompt and render the update

# File lib/mad_clibs/util/iohelper.rb, line 97
def rerender(s)
  clear
  render s
end
winsize() click to toggle source

Get the console window size Returns: [width, height]

# File lib/mad_clibs/util/iohelper.rb, line 86
def winsize
  STDIN.winsize
end
without_cursor() { || ... } click to toggle source

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

# File lib/mad_clibs/util/iohelper.rb, line 113
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