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.



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

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 = T.let([], T::Array[[String, T.nilable(Integer)]])
end

Class Method Details

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



37
38
39
40
41
42
43
44
45
46
# File 'lib/cli/ui/prompt/interactive_options.rb', line 37

def call(options, multiple: false, default: nil)
  list = new(options, multiple: multiple, default: default)
  selected = list.call
  case selected
  when Array
    selected.map { |s| T.must(options[s - 1]) }
  else
    T.must(options[selected - 1])
  end
end

Instance Method Details

#callObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/cli/ui/prompt/interactive_options.rb', line 92

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