-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathprinter.rb
74 lines (56 loc) · 1.35 KB
/
printer.rb
1
2
3
4
5
6
7
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require "paint"
module Util
module Printer
PROMPT = "---> "
INDENT = " "
SEPARATOR = "-" * 80
TITLE_PROMPT = "#### "
extend self
def error!(message)
Printer.say(message, color: :red)
exit(1)
end
def Printer.get(words, choices = nil)
question = "#{words.strip}"
if !choices.nil?
question += " (" + choices.join("/") + ")"
end
Printer.say(question)
print INDENT
input = gets().chomp
if choices && !choices.include?(input)
Printer.say("You must enter one of #{choices.join(", ")}", color: :red)
Printer.get(words, choices)
else
input
end
end
def invalid(words)
Printer.say(words, color: :yellow)
end
def say(words, color: nil, new: true, prompt: PROMPT)
prefix = new ? prompt : INDENT
if color
words = Paint[prefix + words, color]
else
words = prefix + words
end
puts words.gsub("\n", "\n#{INDENT}")
end
def separate(color: nil)
string = SEPARATOR
if color
string = Paint[string, color]
end
puts ""
puts string
end
def success(words)
Printer.say(words, color: :green)
end
def title(words)
separate(color: :cyan)
Printer.say(words, color: :cyan, prompt: TITLE_PROMPT)
end
end
end