Module: IOHelper
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
- #carriage_return ⇒ Object
-
#clear ⇒ Object
clear the console based on the last text rendered.
- #clear_line ⇒ Object
- #line_up ⇒ Object
-
#read_char ⇒ Object
Read a character the user enters on console.
-
#read_key(with_exit_codes = true) ⇒ Object
Read a keypress on console.
-
#read_key_while(&block) ⇒ Object
Get each key the user presses and hand it one by one to the block.
-
#render(prompt) ⇒ Object
Render a text to the prompt.
-
#rerender(prompt) ⇒ Object
Clear the prompt and render the update.
-
#winsize ⇒ Object
Get the console window size Returns: [width, height].
Instance Method Details
#carriage_return ⇒ Object
106 |
# File 'lib/inquirer/utils/iohelper.rb', line 106 def carriage_return; "\r" end |
#clear ⇒ Object
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 * n ) + clear_line end |
#clear_line ⇒ Object
108 |
# File 'lib/inquirer/utils/iohelper.rb', line 108 def clear_line; "\e[0K" end |
#line_up ⇒ Object
107 |
# File 'lib/inquirer/utils/iohelper.rb', line 107 def line_up; "\e[A" end |
#read_char ⇒ Object
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.}" puts ex.backtrace ensure # restore previous state of stty system "stty #{old_state}" end return c end |
#read_key(with_exit_codes = true) ⇒ Object
Read a keypress on console. Return the key name (e.g. “space”, “a”, “B”) Params:
with_exit_codes-
Boolwhether to throw Interrupts when the user presses
ctrl-c and ctrl-d. (true by default)
59 60 61 62 63 |
# File 'lib/inquirer/utils/iohelper.rb', line 59 def read_key with_exit_codes = true raw = read_key_raw raise Interrupt if with_exit_codes and ( raw == "ctrl-c" or raw == "ctrl-d" ) raw end |
#read_key_while(&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
-
Proca 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 &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 ) 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 |
#winsize ⇒ Object
Get the console window size Returns: [width, height]
80 81 82 |
# File 'lib/inquirer/utils/iohelper.rb', line 80 def winsize STDIN.winsize end |