-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.rb
87 lines (67 loc) · 1.54 KB
/
utils.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
module Klank
def self.randomize(array)
temp = []
array.each do |a|
temp << {value: a, sort: rand}
end
temp.sort_by { |hash| hash[:sort] }.map { |t| t[:value] }
end
def self.table(array)
columns = {}
array.each do |hash|
columns.merge!(hash)
end
columns.keys.each do |key|
columns[key] = key.length
end
array.each do |hash|
columns.keys.each do |key|
if hash.key?(key)
columns[key] = [columns[key], hash[key].to_s.length].max
end
end
end
table = []
bar = []
string = []
columns.each do |k, v|
bar << Array.new(v, "-").join
string << sprintf("%-#{v}s", k.to_s)
end
bar = "+-#{bar.join("-+-")}-+"
table << bar
table << "| #{string.join(" | ")} |"
table << bar
array.each do |hash|
row = []
columns.each do |k, v|
row << sprintf("%-#{v}s", hash.key?(k) ? hash[k].to_s : "")
end
table << "| #{row.join(" | ")} |"
end
table << bar
table.join("\n")
end
def self.red(text)
colorize(text, "31")
end
def self.green(text)
colorize(text, "32")
end
def self.yellow(text)
colorize(text, "33")
end
def self.blue(text)
colorize(text, "34")
end
def self.magenta(text)
colorize(text, "35")
end
def self.cyan(text)
colorize(text, "36")
end
private
def self.colorize(text, color_code)
"\e[#{color_code}m#{text}\e[0m"
end
end