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

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
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

Methods included from T::Sig

sig

Constructor Details

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

Returns a new instance of InteractiveOptions.


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cli/ui/prompt/interactive_options.rb', line 55

def initialize(options, multiple: false, default: nil)
  @options = options
  @active = 1
  @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 = T.let([], T::Array[[String, T.nilable(Integer)]])
end

Class Method Details

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


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

def self.call(options, multiple: false, default: nil)
  list = new(options, multiple: multiple, default: default)
  selected = list.call
  if multiple
    selected.map { |s| options[s - 1] }
  else
    options[selected - 1]
  end
end

Instance Method Details

#callObject


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

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