class Question::CheckboxList
Public Class Methods
new(question, choices, default: [])
click to toggle source
# File lib/question/checkbox_list.rb, line 3 def initialize(question, choices, default: []) @question = question @choices = choices @active_index = 0 @finished = false @modified = false default_values = default.map { |choice| value_for_choice(choice) } @selected_choices = choices.select do |choice| default_values.include? value_for_choice(choice) end end
Public Instance Methods
ask()
click to toggle source
# File lib/question/checkbox_list.rb, line 24 def ask TTY.interactive do while !@finished render handle_input end render # render the results a final time and clear the screen end @selected_choices.map { |choice| value_for_choice(choice) } end
handle_input()
click to toggle source
# File lib/question/checkbox_list.rb, line 36 def handle_input input = TTY.input case input when TTY::CODE::SIGINT exit 130 when TTY::CODE::RETURN @finished = true when TTY::CODE::DOWN, TTY::CODE::CTRL_J, TTY::CODE::CTRL_N @active_index += 1 @active_index = 0 if @active_index >= @choices.length when TTY::CODE::UP, TTY::CODE::CTRL_K, TTY::CODE::CTRL_P @active_index -= 1 @active_index = @choices.length - 1 if @active_index < 0 when TTY::CODE::SPACE @modified = true active_choice = @choices[@active_index] if @selected_choices.include? active_choice @selected_choices.delete(active_choice) else @selected_choices.push(active_choice) end end end
instructions()
click to toggle source
# File lib/question/checkbox_list.rb, line 60 def instructions "(Use <space>, <up>, <down> – Press <enter> when finished)" end
label_for_choice(choice)
click to toggle source
# File lib/question/checkbox_list.rb, line 16 def label_for_choice(choice) choice.is_a?(Hash) ? choice[:label] : choice end
render()
click to toggle source
# File lib/question/checkbox_list.rb, line 64 def render TTY.clear print "? ".cyan print @question print ": " if @finished print @selected_choices.map { |choice| label_for_choice(choice) }.join(", ").green elsif @modified print @selected_choices.map { |choice| label_for_choice(choice) }.join(", ") else print instructions.light_white end print "\n" unless @finished @choices.each_with_index do |choice, index| print index == @active_index ? TTY::UI::SELECTED : TTY::UI::UNSELECTED print " " if @selected_choices.include?(choice) print TTY::UI::CHECKBOX_CHECKED.green else print TTY::UI::CHECKBOX_UNCHECKED.red end print " " print label_for_choice(choice) print "\n" end print TTY::CODE::NOOP end end
value_for_choice(choice)
click to toggle source
# File lib/question/checkbox_list.rb, line 20 def value_for_choice(choice) choice.is_a?(Hash) ? choice[:value] : choice end