Class: Checkbox

Inherits:
Object
  • Object
show all
Defined in:
lib/inquirer/prompts/checkbox.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(question = nil, elements = [], renderer = nil, responseRenderer = nil) ⇒ Checkbox

Returns a new instance of Checkbox.



61
62
63
64
65
66
67
68
69
# File 'lib/inquirer/prompts/checkbox.rb', line 61

def initialize question = nil, elements = [], renderer = nil, responseRenderer = nil
  @elements = elements
  @question = question
  @pos = 0
  @active = elements.map{|i| false}
  @prompt = ""
  @renderer = renderer || CheckboxDefault.new( Inquirer::Style::Default )
  @responseRenderer = responseRenderer = CheckboxResponseDefault.new()
end

Class Method Details

.ask(question = nil, elements = [], opts = {}) ⇒ Object



130
131
132
133
# File 'lib/inquirer/prompts/checkbox.rb', line 130

def self.ask question = nil, elements = [], opts = {}
  l = Checkbox.new question, elements, opts[:renderer], opts[:rendererResponse]
  l.run opts.fetch(:clear, true), opts.fetch(:response, true)
end

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


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/inquirer/prompts/checkbox.rb', line 101

def run clear, response
  # finish if there's nothing to do
  return @active if Array(@elements).empty?

  # hides the cursor while prompting
  IOHelper.without_cursor do
    # 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
  end

  # clear the final prompt and the line
  IOHelper.clear if clear

  # show the answer
  IOHelper.render( update_response ) if response

  # return the index of the selected item
  @active
end

#update_promptObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/inquirer/prompts/checkbox.rb', line 71

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

#update_responseObject



84
85
86
87
88
89
90
# File 'lib/inquirer/prompts/checkbox.rb', line 84

def update_response
  e = @elements
    .map.with_index(0)
    .select {|f, pos| @active[pos] }
    .map {|f, pos| f }
  @prompt = @responseRenderer.renderResponse(@question, e.join(", "))
end