module Optplus::IO

bunch of convenience methods to output messages and get inputs for command-line scripts

Constants

Answers

lookup to map between symbols and responses

Public Instance Methods

alert(txt) click to toggle source
# File lib/optplus/IO.rb, line 35
def alert(txt)
  $stderr.puts txt.red.bold
end
ask(question, default=:no, answers=nil) click to toggle source
# File lib/optplus/IO.rb, line 39
def ask(question, default=:no, answers=nil)
  answers ||= Answers
  default = answers.keys.first unless answers.has_key?(default)
  def_key = answers[default]
  answer_options = answers.values.collect {|k| k == def_key ? k.upcase : k}.join('')
  loop do
    $stderr.print "#{question}(#{answer_options})? "
    response = $stdin.gets[0,1].downcase
    if response == '?' then
      answers.each_pair do |key, val|
        $stderr.puts "#{key}"
      end
      next
    end
    if answers.has_value?(response) then
      return answers.key(response)
    else
      return default
    end
  end
  
end
continue?(question, default=false) click to toggle source
# File lib/optplus/IO.rb, line 62
def continue?(question, default=false)
  def_response = default ? :yes : :no
  response = ask(question, def_response, {:yes=>'y', :no=>'n'})
  return response == :yes
end
say(txt) click to toggle source
# File lib/optplus/IO.rb, line 23
def say(txt)
  $stderr.puts txt
end
say_ok(txt) click to toggle source
# File lib/optplus/IO.rb, line 27
def say_ok(txt)
  $stderr.puts txt.green
end
warn(txt) click to toggle source
# File lib/optplus/IO.rb, line 31
def warn(txt)
  $stderr.puts txt.yellow
end