Class: Checkbox
- Inherits:
-
Object
- Object
- Checkbox
- Defined in:
- lib/inquirer/prompts/checkbox.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(question = nil, elements = [], renderer = nil) ⇒ Checkbox
constructor
A new instance of Checkbox.
-
#run(clear = true) ⇒ Object
- Run the list selection, wait for the user to select an item and return the selected index Params:
clear -
Boolwhether 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.
- Run the list selection, wait for the user to select an item and return the selected index Params:
- #update_prompt ⇒ Object
Constructor Details
#initialize(question = nil, elements = [], renderer = nil) ⇒ Checkbox
Returns a new instance of Checkbox.
44 45 46 47 48 49 50 51 |
# File 'lib/inquirer/prompts/checkbox.rb', line 44 def initialize question = nil, elements = [], renderer = nil @elements = elements @question = question @pos = 0 @active = elements.map{|i| false} @prompt = "" @renderer = renderer || CheckboxDefault.new( Inquirer::Style::Default ) end |
Class Method Details
Instance Method Details
#run(clear = true) ⇒ Object
Run the list selection, wait for the user to select an item and return the selected index Params:
clear-
Boolwhether 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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/inquirer/prompts/checkbox.rb', line 72 def run clear = true # finish if there's nothing to do return @active if Array(@elements).empty? # render the IOHelper.render( update_prompt ) # loop through user input IOHelper.read_key_while do |key| @pos = (@pos - 1) % @elements.length if key == "up" @pos = (@pos + 1) % @elements.length if key == "down" @active[@pos] = !@active[@pos] if key == "space" IOHelper.rerender( update_prompt ) # we are done if the user hits return key != "return" end # clear the final prompt and the line IOHelper.clear if clear # return the index of the selected item @active end |
#update_prompt ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/inquirer/prompts/checkbox.rb', line 53 def update_prompt # transform the list into # {"value"=>..., "selected"=> true|false, "active"=> true|false } e = @elements. # attach the array position map.with_index(0). map do |c,pos| { "value"=>c, "selected" => pos == @pos, "active" => @active[pos] } end # call the renderer @prompt = @renderer.render(@question, e) end |