module DTK::Client::Console

Public Class Methods

prompt_yes_no(message, opts = {}) click to toggle source

Display confirmation prompt and repeat message until expected answer is given

opts can have keys

:disable_ctrl_c - Boolean (default: true)
:add_options - Boolean (default: false)
# File lib/client/util/console.rb, line 27
def self.prompt_yes_no(message, opts = {})
  # used to disable skip with ctrl+c
  prompt_context(opts) do
    message += ' (yes|no)' if opts[:add_options]
    HighLine.agree(message)
  end
end
wait_animation(message, time_seconds) click to toggle source
# File lib/client/util/console.rb, line 35
def self.wait_animation(message, time_seconds)
  print message
  print " [     ]"
  STDOUT.flush
  time_seconds.downto(1) do
    1.upto(4) do |i|
      next_output = "\b\b\b\b\b\b\b"
      case
       when i % 4 == 0
        next_output  += "[ =   ]"
       when i % 3 == 0
         next_output += "[  =  ]"
       when i % 2 == 0
        next_output  += "[   = ]"
       else
        next_output  += "[  =  ]"
      end

      print next_output
      STDOUT.flush
      sleep(0.25)
    end
  end
  # remove loading animation
  print "\b\b\b\b\b\b\bRefreshing..."
  STDOUT.flush
  puts
end

Private Class Methods

confirmation_prompt_additional_options(message, options = []) click to toggle source

Display confirmation prompt and repeat message until expected answer is given options should be sent as array ['all', 'none']

# File lib/client/util/console.rb, line 68
def self.confirmation_prompt_additional_options(message, options = [])
  raise DTK::Client::DtkValidationError, "Options should be sent as array: ['all', 'none']" unless options.is_a?(Array)

  # used to disable skip with ctrl+c
  trap("INT", "SIG_IGN")
  message += " (yes/no#{options.empty? ? '' : ('/' + options.join('/'))})"

  while line = Readline.readline("#{message}: ", true)
    if line.eql?("yes") || line.eql?("y")
      trap("INT",false)
      return true
    elsif line.eql?("no") || line.eql?("n")
      trap("INT",false)
      return false
    elsif options.include?(line)
      trap("INT",false)
      return line
    end
  end
end
disable_ctrl_c(&body) click to toggle source
# File lib/client/util/console.rb, line 117
def self.disable_ctrl_c(&body)
  trap('INT', 'SIG_IGN')
  begin
    body.call
  ensure
    trap('INT', false)
  end
end
password_prompt(message, options = []) click to toggle source
# File lib/client/util/console.rb, line 89
def self.password_prompt(message, options = [])
  begin
    while line = (HighLine.ask("#{message}") { |q| q.echo = false})
      raise Interrupt if line.empty?
        return line
      end
    rescue Interrupt
      return nil
    ensure
      puts "\n" if line.nil?
    end
  end
prompt_context(opts = {}, &body) click to toggle source

opts can have keys

:disable_ctrl_c - Boolean (default: true)
# File lib/client/util/console.rb, line 113
def self.prompt_context(opts = {}, &body)
  "#{opts[:disable_ctrl_c]}" == 'false' ? body.call : disable_ctrl_c(&body)
end
version_prompt(versions, message, opts = {}) click to toggle source
# File lib/client/util/console.rb, line 102
def self.version_prompt(versions, message, opts = {})
  prompt_context(opts) do
    HighLine.choose do |menu|
      menu.prompt = message
      menu.choices(*versions)
      menu.choice('all') if opts[:add_all]
    end
  end
end