Module: EpubForge::Action::Chatterbox

Included in:
Action2
Defined in:
lib/epubforge/action/chatterbox.rb

Instance Method Summary collapse

Instance Method Details

#ask(question, opts = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# 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) ⇒ Object



79
80
81
# File 'lib/epubforge/action/chatterbox.rb', line 79

def ask_prettily( question, *args )
  ask( question.paint(:blue), *args  )
end

#no?(message, color = nil) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
# 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) ⇒ Object



4
5
6
# File 'lib/epubforge/action/chatterbox.rb', line 4

def say( message, color = nil )
  puts message.paint(color)
end

#say_all_is_well(statement) ⇒ Object



83
84
85
# File 'lib/epubforge/action/chatterbox.rb', line 83

def say_all_is_well( statement )
  say( statement, :green )
end

#say_error(statement) ⇒ Object



87
88
89
# File 'lib/epubforge/action/chatterbox.rb', line 87

def say_error( statement )
  say( statement.paint(:bg_red, :bold) )
end

#say_instruction(instruction) ⇒ Object



60
61
62
# File 'lib/epubforge/action/chatterbox.rb', line 60

def say_instruction( instruction )
  puts instruction.paint(:yellow)
end

#yes?(message, opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
# 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) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/epubforge/action/chatterbox.rb', line 47

def yes_prettily?( statement )
  yes?( statement, :color => :pink )
end