# = consoleutils.rb

module ConsoleUtils

  module_function

  # Convenient method to get simple console reply.

  def ask( question, answers=nil )
    print "#{question}"
    print " [#{answers}] " if answers
    until inp = $stdin.gets[0,1] ; sleep 1 ; end ; puts
    inp
  end

  # Convenience method for puts. Use this instead of
  # puts when the output should be supressed if the
  # global $QUIET option is set.

  def say( statement )
    puts statement unless $QUIET
  end
  alias :tell :say  # TODO deprecate

  #

  def password(forwhat=nil)
    if forwhat
      ask("password for #{forwhat}: ")
    else
      ask("password: ")
    end
  end

  #

  def password_cached(forwhat=nil)
    if forwhat
      @password ||= ask("password for #{forwhat}: ")
    else
      @password ||= ask("password: ")
    end
  end

end
