Module: List

Extended by:
List
Included in:
List
Defined in:
lib/inquirer/prompts/list.rb

Instance Method Summary collapse

Instance Method Details

#prompt(opts = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/inquirer/prompts/list.rb', line 10

def prompt opts = {}
  @question = opts[:message]
  default   = opts[:default] || 0

  @position  = 0
  @paginator = Paginator.new

  @choices = []
  opts[:choices].each { |choice|

    if choice[:when].is_a?(Proc)

      when_parameter = opts.merge(
        choice: choice,
      )

      ask_choice = choice[:when].call( when_parameter )

      next if !ask_choice
    elsif [true, false].include? choice[:when]
      next if !choice[:when]
    end

    choice[:value] ||= choice[:name]

    @choices.push(choice)
  }

  return nil if Array(@choices).empty?

  return @choices[0][:value] if @choices.size == 1

  if default.is_a?(String) || default.is_a?(Symbol)
    @position   = @choices.find_index { |choice| choice[:value] == default }
    @position ||= 0
  elsif default.is_a?(Integer) && default < @choices.size
    @position = default
  else
    @position = 0
  end

  IOHelper.without_cursor do

    question_backup = @question
    @question      += ' '
    @question      += Inquirer::Style::List.selection_help

    render_prompt

    @question = question_backup

    IOHelper.read_char do |char|

      key = IOChar.char_to_key(char)

      case key
      when 'up'
        @position = (@position - 1) % @choices.length
      when 'down'
        @position = (@position + 1) % @choices.length
      end

      IOHelper.clear

      render_prompt

      key != 'return'
    end
  end

  IOHelper.clear

  render_result

  @choices[@position][:value]
end

#render_promptObject



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
# File 'lib/inquirer/prompts/list.rb', line 87

def render_prompt
  # start with the question prefix
  prompt = Inquirer::Style.question_prefix

  prompt += Inquirer::Style::List.question % @question

  prompt += IOChar.newline

  prompt += @choices.map.with_index(0) do |choice, position|

    choice_prompt = ''

    if position == @position
      choice_prompt += Inquirer::Style::List.selector
      choice_prompt += ' '
      choice_prompt += Inquirer::Style::List.selected_item % choice[:name]
    else
      choice_prompt += '  '
      choice_prompt += Inquirer::Style::List.item % choice[:name]
    end

    choice_prompt
  end.join('')

  paginated_promt = @paginator.paginate(prompt, @position)

  IOHelper.render( paginated_promt )
end

#render_resultObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/inquirer/prompts/list.rb', line 116

def render_result

  # start with the question prefix
  result = Inquirer::Style.question_prefix

  result += Inquirer::Style::List.question % @question

  display_value = @choices[@position][:short] || @choices[@position][:name]

  result += Inquirer::Style::List.response % display_value

  result += IOChar.newline

  IOHelper.render( result )
end