Class: Input
- Inherits:
-
Object
- Object
- Input
- Defined in:
- lib/inquirer/prompts/input.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(question = nil, default = nil, renderer = nil, responseRenderer = nil) ⇒ Input
constructor
A new instance of Input.
-
#run(clear, response) ⇒ Object
- Run the list selection, wait for the user to select an item and return the selected index Params:
clear
Bool
whether to clear the selection prompt once this is done defaults to true; set it to false if you want the prompt to remain after the user is done with selectingresponse
-
Bool
whether show the rendered response when this is done defaults to true; set it to false if you want the prompt to remain after the user is done with selecting.
- Run the list selection, wait for the user to select an item and return the selected index Params:
- #update_cursor ⇒ Object
- #update_prompt ⇒ Object
- #update_response ⇒ Object
Constructor Details
#initialize(question = nil, default = nil, renderer = nil, responseRenderer = nil) ⇒ Input
Returns a new instance of Input.
47 48 49 50 51 52 53 54 55 |
# File 'lib/inquirer/prompts/input.rb', line 47 def initialize question = nil, default = nil, renderer = nil, responseRenderer = nil @question = question @value = "" @default = default @prompt = "" @pos = 0 @renderer = renderer || InputDefault.new( Inquirer::Style::Default ) @responseRenderer = responseRenderer = InputResponseDefault.new() end |
Class Method Details
Instance Method Details
#run(clear, response) ⇒ Object
Run the list selection, wait for the user to select an item and return the selected index Params:
clear
-
Bool
whether to clear the selection prompt once this is done
defaults to true; set it to false if you want the prompt to remain after
the user is done with selecting
response
-
Bool
whether show the rendered response when this is done
defaults to true; set it to false if you want the prompt to remain after
the user is done with selecting
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 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 121 122 123 |
# File 'lib/inquirer/prompts/input.rb', line 79 def run clear, response # render the IOHelper.render( update_prompt ) # loop through user input # IOHelper.read_char IOHelper.read_key_while true do |key| raw = IOHelper.char_to_raw(key) case raw when "backspace" @value = @value.chop IOHelper.rerender( update_prompt ) update_cursor when "left" if @pos < @value.length @pos = @pos + 1 print IOHelper.char_left end when "right" if @pos > 0 @pos = @pos - 1 print IOHelper.char_right end when "return" if not @default.nil? and @value == "" @value = @default end else unless ["up", "down"].include?(raw) @value = @value.insert(@value.length - @pos, key) IOHelper.rerender( update_prompt ) update_cursor end end raw != "return" end # clear the final prompt and the line IOHelper.clear if clear # show the answer IOHelper.render( update_response ) if response # return the value @value end |
#update_cursor ⇒ Object
66 67 68 |
# File 'lib/inquirer/prompts/input.rb', line 66 def update_cursor print IOHelper.char_left * @pos end |
#update_prompt ⇒ Object
57 58 59 60 |
# File 'lib/inquirer/prompts/input.rb', line 57 def update_prompt # call the renderer @prompt = @renderer.render(@question, @value, @default) end |
#update_response ⇒ Object
62 63 64 |
# File 'lib/inquirer/prompts/input.rb', line 62 def update_response @prompt = @responseRenderer.renderResponse(@question, @value) end |