module EpubForge::Action::Chatterbox

Public Instance Methods

ask( question, opts = {} ) click to toggle source
# File lib/epubforge/action/chatterbox.rb, line 8
def ask( question, opts = {} )
  while true
    opts[:colors] ||= opts[:color]
    
    answer = nil
    input = Readline.readline( question.paint(opts[:colors] ) + " " ).strip
    input.strip
    
    if input.fwf_blank?
      if opts[:default]
        answer = opts[:default]
      elsif opts[:blank_allowed] || (opts[:possible_answers] || []).include?("")
        answer = input
      end
    elsif opts[:possible_answers]
      answer = input if opts[:possible_answers].include?( input )
    else
      answer = input
    end
    
    if answer.nil?
      say( "'#{input}' is not a valid answer.", :red )
      say( "Possible answers: #{ opts[:possible_answers].inspect }") if opts[:possible_answers]
    end
    
    return answer unless answer.nil?
  end
end
ask_from_menu( statement, choices ) click to toggle source
# File lib/epubforge/action/chatterbox.rb, line 65
def ask_from_menu( statement, choices )
  choices.map! do |choice|
    choice.is_a?(String) ? [choice] : choice    # I'm being too clever by half here.  .first/.last still works.
  end
  
  choice_text = ""
  choices.each_with_index do |choice,i|
    choice_text << "\t\t#{i}) #{choice.first}\n" 
  end
  
  selection = ask( "#{statement}\n\tChoices:\n#{choice_text}>>> ".paint(:blue) )
  choices[selection.to_i].last
end
ask_prettily( question, *args ) click to toggle source
# File lib/epubforge/action/chatterbox.rb, line 79
def ask_prettily( question, *args )
  ask( question.paint(:blue), *args  )
end
no?( message, color = nil ) click to toggle source
# File lib/epubforge/action/chatterbox.rb, line 52
def no?( message, color = nil )
  opts[:allowed] = ["Y", "y", "N", "n"]
  opts[:default] = "n"
  answer = ask( "#{message} (y/N)", opts ).downcase
  
  answer == "n"
end
say( message, color = nil ) click to toggle source
# File lib/epubforge/action/chatterbox.rb, line 4
def say( message, color = nil )
  puts message.paint(color)
end
say_all_is_well( statement ) click to toggle source
# File lib/epubforge/action/chatterbox.rb, line 83
def say_all_is_well( statement )
  say( statement, :green )
end
say_error( statement ) click to toggle source
# File lib/epubforge/action/chatterbox.rb, line 87
def say_error( statement )
  say( statement.paint(:bg_red, :bold) )
end
say_instruction( instruction ) click to toggle source
# File lib/epubforge/action/chatterbox.rb, line 60
def say_instruction( instruction )
  puts instruction.paint(:yellow)
end
yes?( message, opts = {} ) click to toggle source
# File lib/epubforge/action/chatterbox.rb, line 37
def yes?( message, opts = {} )
  opts[:colors]   ||= :blue
  opts[:possible_answers] = ["Y", "y", "N", "n", "yes", "no"]
  opts[:default] ||= "y"
  
  answer = ask( "#{message} (Y/n)", opts ).downcase[0..1]
  
  answer == "y"
end
yes_prettily?( statement ) click to toggle source
# File lib/epubforge/action/chatterbox.rb, line 47
def yes_prettily?( statement )
  yes?( statement, :color => :pink )
end