Module: IOHelper
- Extended by:
- IOHelper
- Included in:
- IOHelper
- Defined in:
- lib/inquirer/spec_helpers.rb,
lib/inquirer/utils/iohelper.rb
Instance Attribute Summary collapse
-
#bottomline ⇒ Object
Returns the value of attribute bottomline.
-
#keys ⇒ Object
Returns the value of attribute keys.
-
#output ⇒ Object
Returns the value of attribute output.
-
#winsize ⇒ Object
Returns the value of attribute winsize.
Instance Method Summary collapse
-
#clear ⇒ Object
clear the console based on the last text rendered.
-
#get_char ⇒ Object
read a character the user enters on console.
- #output_plain ⇒ Object
- #plain_chars(string) ⇒ Object
-
#read_char(&block) ⇒ Object
Get each key the user presses and hand it one by one to the block.
-
#render(prompt, bottomline = nil) ⇒ Object
render a text to the prompt.
- #reset ⇒ Object
-
#without_cursor ⇒ Object
hides the cursor and ensure the cursor be visible at the end.
- #wrap(string) ⇒ Object
Instance Attribute Details
#bottomline ⇒ Object
Returns the value of attribute bottomline.
4 5 6 |
# File 'lib/inquirer/spec_helpers.rb', line 4 def bottomline @bottomline end |
#keys ⇒ Object
Returns the value of attribute keys.
4 5 6 |
# File 'lib/inquirer/spec_helpers.rb', line 4 def keys @keys end |
#output ⇒ Object
Returns the value of attribute output.
4 5 6 |
# File 'lib/inquirer/spec_helpers.rb', line 4 def output @output end |
#winsize ⇒ Object
Returns the value of attribute winsize.
4 5 6 |
# File 'lib/inquirer/spec_helpers.rb', line 4 def winsize @winsize end |
Instance Method Details
#clear ⇒ Object
clear the console based on the last text rendered
65 66 67 |
# File 'lib/inquirer/utils/iohelper.rb', line 65 def clear @output = '' end |
#get_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 and: gist.github.com/acook/4190379
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# 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 ⇒ Object
23 24 25 |
# File 'lib/inquirer/spec_helpers.rb', line 23 def output_plain self.plain_chars(output) end |
#plain_chars(string) ⇒ Object
122 123 124 |
# File 'lib/inquirer/utils/iohelper.rb', line 122 def plain_chars(string) string.gsub(/\e\[([;\dA-Z]+)?m?/, '') end |
#read_char(&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
14 15 16 17 18 |
# File 'lib/inquirer/utils/iohelper.rb', line 14 def read_char &block Array(@keys).each do |key| break unless block.(key) end end |
#render(prompt, bottomline = nil) ⇒ Object
render a text to the prompt
57 58 59 60 61 62 |
# File 'lib/inquirer/utils/iohelper.rb', line 57 def render(prompt, bottomline = nil) @rendered = wrap(prompt) @bottomline = bottomline @output = rendered_output end |
#reset ⇒ Object
27 28 29 30 31 |
# File 'lib/inquirer/spec_helpers.rb', line 27 def reset clear @winsize = [10, 2000] @keys = nil end |
#without_cursor ⇒ Object
hides the cursor and ensure the cursor be visible at the end
82 83 84 85 86 87 88 89 90 91 92 |
# 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) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# 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 |