Class: CLI::UI::Prompt::InteractiveOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/ui/prompt/interactive_options.rb

Constant Summary collapse

DONE =
'Done'
CHECKBOX_ICON =
{ false => '', true => '' }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, multiple: false, default: nil) ⇒ InteractiveOptions

Initializes a new InteractiveOptions Usually called from self.call

Example Usage:

CLI::UI::Prompt::InteractiveOptions.new(%w(rails go python))

: (Array options, ?multiple: bool, ?default: (String | Array)?) -> void



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
# File 'lib/cli/ui/prompt/interactive_options.rb', line 52

def initialize(options, multiple: false, default: nil)
  @options = options
  @active = if default && (i = options.index(default))
    i + 1
  else
    1
  end
  @marker = '>'
  @answer = nil
  @state = :root
  @multiple = multiple
  # Indicate that an extra line (the "metadata" line) is present and
  # the terminal output should be drawn over when processing user input
  @displaying_metadata = false
  @filter = ''
  # 0-indexed array representing if selected
  # @options[0] is selected if @chosen[0]
  if multiple
    @chosen = if default
      @options.map { |option| default.include?(option) }
    else
      Array.new(@options.size) { false }
    end
  end
  @redraw = true
  @presented_options = [] #: Array[[String, Integer?]]
end

Class Method Details

.call(options, multiple: false, default: nil) ⇒ Object

Prompts the user with options Uses an interactive session to allow the user to pick an answer Can use arrows, y/n, numbers (1/2), and vim bindings to control For more than 9 options, hitting ‘e’, ‘:’, or ‘G’ will enter select mode allowing the user to type in longer numbers Pressing ‘f’ or ‘/’ will allow the user to filter the results

Example Usage:

Ask an interactive question

CLI::UI::Prompt::InteractiveOptions.call(%w(rails go python))

: (Array options, ?multiple: bool, ?default: (String | Array)?) -> (String | Array)



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cli/ui/prompt/interactive_options.rb', line 30

def call(options, multiple: false, default: nil)
  list = new(options, multiple: multiple, default: default)
  selected = list.call
  case selected
  when Array
    selected.map do |s|
      options[s - 1] #: as !nil
    end
  else
    options[selected - 1] #: as !nil
  end
end

Instance Method Details

#callObject

Calls the InteractiveOptions and asks the question Usually used from self.call

: -> (Integer | Array)



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cli/ui/prompt/interactive_options.rb', line 84

def call
  calculate_option_line_lengths
  CLI::UI.raw { print(ANSI.hide_cursor) }
  while @answer.nil?
    render_options
    process_input_until_redraw_required
    reset_position
  end
  clear_output

  @answer
ensure
  CLI::UI.raw do
    print(ANSI.show_cursor)
  end
end