Module: CLI::UI::Prompt

Extended by:
T::Sig
Defined in:
lib/cli/ui/prompt.rb,
lib/cli/ui/prompt/options_handler.rb,
lib/cli/ui/prompt/interactive_options.rb

Defined Under Namespace

Classes: InteractiveOptions, OptionsHandler

Class Method Summary collapse

Methods included from T::Sig

sig

Class Method Details

.any_key(prompt = 'Press any key to continue...') ⇒ Object



226
227
228
229
230
231
# File 'lib/cli/ui/prompt.rb', line 226

def any_key(prompt = 'Press any key to continue...')
  CLI::UI::StdoutRouter::Capture.in_alternate_screen do
    puts_question(prompt)
    read_char
  end
end

.ask(question, options: nil, default: nil, is_file: false, allow_empty: true, multiple: false, filter_ui: true, select_ui: true, &options_proc) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/cli/ui/prompt.rb', line 128

def ask(
  question,
  options: nil,
  default: nil,
  is_file: false,
  allow_empty: true,
  multiple: false,
  filter_ui: true,
  select_ui: true,
  &options_proc
)
  has_options = !!(options || block_given?)
  if has_options && default && !multiple
    raise(ArgumentError, 'conflicting arguments: default may not be provided with options when not multiple')
  end

  if has_options && is_file
    raise(ArgumentError, 'conflicting arguments: is_file is only useful when options are not provided')
  end

  if options && multiple && default && !(Array(default) - options).empty?
    raise(ArgumentError, 'conflicting arguments: default should only include elements present in options')
  end

  if multiple && !has_options
    raise(ArgumentError, 'conflicting arguments: options must be provided when multiple is true')
  end

  if !multiple && default.is_a?(Array)
    raise(ArgumentError, 'conflicting arguments: multiple defaults may only be provided when multiple is true')
  end

  if has_options
    ask_interactive(
      question,
      options,
      multiple: multiple,
      default: default,
      filter_ui: filter_ui,
      select_ui: select_ui,
      &options_proc
    )
  else
    ask_free_form(question, T.cast(default, T.nilable(String)), is_file, allow_empty)
  end
end

.ask_password(question) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/cli/ui/prompt.rb', line 183

def ask_password(question)
  require 'io/console'

  CLI::UI::StdoutRouter::Capture.in_alternate_screen do
    $stdout.print(CLI::UI.fmt('{{?}} ' + question)) # Do not use puts_question to avoid the new line.

    # noecho interacts poorly with Readline under system Ruby, so do a manual `gets` here.
    # No fancy Readline integration (like echoing back) is required for a password prompt anyway.
    password = $stdin.noecho do
      # Chomp will remove the one new line character added by `gets`, without touching potential extra spaces:
      # " 123 \n".chomp => " 123 "
      $stdin.gets.to_s.chomp
    end

    $stdout.puts # Complete the line

    password
  end
end

.confirm(question, default: true) ⇒ Object



214
215
216
# File 'lib/cli/ui/prompt.rb', line 214

def confirm(question, default: true)
  ask_interactive(question, default ? ['yes', 'no'] : ['no', 'yes'], filter_ui: false) == 'yes'
end

.instructions_colorObject



33
34
35
# File 'lib/cli/ui/prompt.rb', line 33

def instructions_color
  @instructions_color ||= Color::YELLOW
end

.instructions_color=(color) ⇒ Object



44
45
46
# File 'lib/cli/ui/prompt.rb', line 44

def instructions_color=(color)
  @instructions_color = CLI::UI.resolve_color(color)
end

.read_charObject



235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/cli/ui/prompt.rb', line 235

def read_char
  CLI::UI::StdoutRouter::Capture.in_alternate_screen do
    if $stdin.tty? && !ENV['TEST']
      require 'io/console'
      $stdin.getch # raw mode for tty
    else
      $stdin.getc # returns nil at end of input
    end
  end
rescue Errno::EIO, Errno::EPIPE, IOError
  "\e"
end