-
Notifications
You must be signed in to change notification settings - Fork 2
/
printer.rb
94 lines (76 loc) · 1.94 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
module Polvo::Printer
class << self
# --------------------------------------------
# Simple methods for outputing semantic text
# --------------------------------------------
def h1(str)
printf "\n === #{str.upcase} ===\n\n"
end
def h2(str)
self.h1(str)
end
def h3(str)
self.h1(str)
end
def h4(str)
self.h1(str)
end
def h5(str)
self.h1(str)
end
def p(str)
print str,"\n\n"
end
def notice(str)
puts "*** #{str}".green
end
def warning(str)
puts "*** #{str}".yellow
end
def error(str)
puts "*** #{str}".red
end
# ---------------------------------------------
# Simple methods that expect user interaction
# ---------------------------------------------
def wait(str='Press ENTER to continue.')
puts
self.warning(str)
STDIN.gets
end
def confirm(str="Continue")
printf "\n#{str} yes/no? ".yellow
input = STDIN.gets.chomp
return( %w{y Y yes YES}.any? {|v| v == input} )
puts
end
def ask(str)
printf "\n#{str}"
return STDIN.gets.chomp
puts
end
def menu(items,options = {})
question = options['question'] || 'Choice: '
self.clear unless options['noclear']
self.h1(options['title']) if options['title']
self.p(options['description']) if options['title']
i = 0
items.each do |item|
opt = (sprintf "%5d",i+1).gsub!(/\s(\d)/,'[\1')
puts "#{opt}] #{item}"
i+=1
end
self.warn(options['warn']) if options['warn']
return self.ask(question)
end
# --------------------------------------
# More useful methods
# --------------------------------------
def clear
system('clear')
end
def debug(str)
puts str.magenta
end
end
end