Module: Optplus::IO
- Defined in:
- lib/optplus/IO.rb
Overview
bunch of convenience methods to output messages and get inputs for command-line scripts
Constant Summary collapse
- Answers =
lookup to map between symbols and responses
Hash.new(false).merge({:no=>'n', :yes=>'y', :skip=>'s', :diff=>'d', :list=>'l'})
Instance Method Summary collapse
- #alert(txt) ⇒ Object
- #ask(question, default = :no, answers = nil) ⇒ Object
- #continue?(question, default = false) ⇒ Boolean
- #say(txt) ⇒ Object
- #say_ok(txt) ⇒ Object
- #warn(txt) ⇒ Object
Instance Method Details
#alert(txt) ⇒ Object
35 36 37 |
# File 'lib/optplus/IO.rb', line 35 def alert(txt) $stderr.puts txt.red.bold end |
#ask(question, default = :no, answers = nil) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# 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] = answers.values.collect {|k| k == def_key ? k.upcase : k}.join('') loop do $stderr.print "#{question}(#{})? " 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) ⇒ Boolean
62 63 64 65 66 |
# 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) ⇒ Object
23 24 25 |
# File 'lib/optplus/IO.rb', line 23 def say(txt) $stderr.puts txt end |
#say_ok(txt) ⇒ Object
27 28 29 |
# File 'lib/optplus/IO.rb', line 27 def say_ok(txt) $stderr.puts txt.green end |
#warn(txt) ⇒ Object
31 32 33 |
# File 'lib/optplus/IO.rb', line 31 def warn(txt) $stderr.puts txt.yellow end |