Module: Planter::Prompt

Included in:
Planter
Defined in:
lib/planter/prompt.rb

Overview

Individual question

Defined Under Namespace

Classes: Question

Class Method Summary collapse

Class Method Details

.choice(choices, prompt = 'Make a selection', default_response: nil) ⇒ String

Choose from an array of multiple choices. Letter surrounded in parenthesis becomes character for response. Only one letter should be specified and must be unique.

Parameters:

  • choices (Array)

    The choices

  • prompt (String) (defaults to: 'Make a selection')

    The prompt

  • default_response (String) (defaults to: nil)

    The character of the default response

Returns:

  • (String)

    character of selected response, lowercased



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/planter/prompt.rb', line 243

def self.choice(choices, prompt = 'Make a selection', default_response: nil)
  $stdin.reopen('/dev/tty')

  default = default_response.is_a?(String) ? default_response.downcase : nil

  # if this isn't an interactive shell, answer default
  return default unless $stdout.isatty

  # If --defaults is set, return default
  return default if Planter.accept_defaults || ENV['PLANTER_DEBUG']

  # clear the buffer
  if ARGV&.length
    ARGV.length.times do
      ARGV.shift
    end
  end
  system 'stty cbreak'

  vertical = choices.join(' ').length + 4 > TTY::Screen.cols
  desc = choices.map { |c| c.highlight_character(default: default) }
  abbr = choices.abbr_choices(default: default)

  options = if vertical
              "{x}#{desc.join("\n")}\n{by}#{prompt}{x} #{abbr}{bw}? "
            else
              "{by}#{prompt}{bw}?\n#{desc.join(', ')}{x} #{abbr}:{x} "
            end

  $stdout.syswrite options.x
  res = $stdin.sysread 1
  puts
  system 'stty cooked'

  res.chomp!
  res.downcase!

  res.empty? ? default : res
end

.file_what?(entry) ⇒ Boolean

Returns:

  • (Boolean)


283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/planter/prompt.rb', line 283

def self.file_what?(entry)
  options = %w[(o)vewrite (m)erge]
  options << '(c)opy' unless File.exist?(entry.target)
  options << '(i)gnore'
  opt = Prompt.choice(options, "What do you want to do with #{File.basename(entry.target)}", default_response: 'i')
  case opt
  when /^m/
    :merge
  when /^o/
    :overwrite
  when /^c/
    :copy
  else
    :ignore
  end
end

.yn(question, default_response: false) ⇒ Boolean

Ask a yes or no question in the terminal

Parameters:

  • question (String)

    The question to ask

  • default_response (Boolean) (defaults to: false)

    default response if no input

Returns:

  • (Boolean)

    yes or no



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/planter/prompt.rb', line 310

def self.yn(question, default_response: false)
  $stdin.reopen('/dev/tty')

  default = if default_response.is_a?(String)
              default_response =~ /y/i ? true : false
            else
              default_response
            end

  # if PLANTER_DEBUG is set, answer default
  return true if ENV['PLANTER_DEBUG']

  # if this isn't an interactive shell, answer default
  return default unless $stdout.isatty

  # If --defaults is set, return default
  return default if Planter.accept_defaults

  # clear the buffer
  if ARGV&.length
    ARGV.length.times do
      ARGV.shift
    end
  end
  system 'stty cbreak'

  options = if default.nil?
              '{w}[{bw}y{w}/{bw}n{w}]'
            else
              "{w}[#{default ? '{bg}Y{w}/{bw}n' : '{bw}y{w}/{bg}N'}{w}]"
            end

  $stdout.syswrite "{bw}#{question.sub(/\?$/, '')} #{options}{bw}? {x}".x
  res = $stdin.sysread 1
  puts
  system 'stty cooked'

  res.chomp!
  res.downcase!

  return default if res.empty?

  res =~ /y/i ? true : false
end