Class: Question::CheckboxList

Inherits:
Object
  • Object
show all
Defined in:
lib/question/checkbox_list.rb

Instance Method Summary collapse

Constructor Details

#initialize(question, choices, default: []) ⇒ CheckboxList

Returns a new instance of CheckboxList.



3
4
5
6
7
8
9
10
11
12
13
14
# 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

Instance Method Details

#askObject



24
25
26
27
28
29
30
31
32
33
34
# 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_inputObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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

#instructionsObject



60
61
62
# File 'lib/question/checkbox_list.rb', line 60

def instructions
  "(Use <space>, <up>, <down> – Press <enter> when finished)"
end

#label_for_choice(choice) ⇒ Object



16
17
18
# File 'lib/question/checkbox_list.rb', line 16

def label_for_choice(choice)
  choice.is_a?(Hash) ? choice[:label] : choice
end

#renderObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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) ⇒ Object



20
21
22
# File 'lib/question/checkbox_list.rb', line 20

def value_for_choice(choice)
  choice.is_a?(Hash) ? choice[:value] : choice
end