Module: IOHelper

Extended by:
IOHelper
Included in:
IOHelper
Defined in:
lib/inquirer/utils/iohelper.rb

Constant Summary collapse

KEYS =
{
  " " => "space",
  "\t" => "tab",
  "\r" => "return",
  "\n" => "linefeed",
  "\e" => "escape",
  "\e[A" => "up",
  "\e[B" => "down",
  "\e[C" => "right",
  "\e[D" => "left",
  "\177" => "backspace",
  # ctrl + c
  "\003" => "ctrl-c",
  # ctrl + d
  "\004" => "ctrl-d"
}

Instance Method Summary collapse

Instance Method Details

#carriage_returnObject



123
# File 'lib/inquirer/utils/iohelper.rb', line 123

def carriage_return;  "\r"    end

#char_leftObject



126
# File 'lib/inquirer/utils/iohelper.rb', line 126

def char_left;        "\e[D" end

#char_rightObject



127
# File 'lib/inquirer/utils/iohelper.rb', line 127

def char_right;       "\e[C" end

#char_to_raw(char) ⇒ Object



119
120
121
# File 'lib/inquirer/utils/iohelper.rb', line 119

def char_to_raw char
  KEYS.fetch char, char
end

#clearObject

clear the console based on the last text rendered



97
98
99
100
101
102
103
104
# File 'lib/inquirer/utils/iohelper.rb', line 97

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_lineObject



125
# File 'lib/inquirer/utils/iohelper.rb', line 125

def clear_line;       "\e[0K" end

#line_upObject



124
# File 'lib/inquirer/utils/iohelper.rb', line 124

def line_up;          "\e[A"  end

#read_charObject

Read a character the user enters on console. This call is synchronous blocking. This is taken from: www.alecjacobson.com/weblog/?p=75



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/inquirer/utils/iohelper.rb', line 27

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) ⇒ Object

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)

Raises:

  • (Interrupt)


59
60
61
62
63
# File 'lib/inquirer/utils/iohelper.rb', line 59

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) ⇒ Object

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



69
70
71
72
73
74
75
76
# File 'lib/inquirer/utils/iohelper.rb', line 69

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(prompt) ⇒ Object

Render a text to the prompt



85
86
87
88
# File 'lib/inquirer/utils/iohelper.rb', line 85

def render prompt
  @rendered = prompt
  print prompt
end

#rerender(prompt) ⇒ Object

Clear the prompt and render the update



91
92
93
94
# File 'lib/inquirer/utils/iohelper.rb', line 91

def rerender prompt
  clear
  render prompt
end

#winsizeObject

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



80
81
82
# File 'lib/inquirer/utils/iohelper.rb', line 80

def winsize
  STDIN.winsize
end

#without_cursorObject

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



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/inquirer/utils/iohelper.rb', line 107

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