needs: colorize
or hack the colors out :3
also needs some optimations. the code is redundant. Just posted it here if i may not work on it in next time
puts Table.generate([ | |
["ID","Title","Description"], | |
["1","Heyho","Heyhooo"], | |
["2","Example","Another Example line"] | |
]) |
module Table | |
def Table.generate(content) | |
l = Array.new | |
content.each do |c| | |
c.each_with_index do |d,i| | |
l[i] = 0 if l[i].nil? | |
l[i] = d.size if l[i] < d.size | |
end | |
end | |
length = 0 | |
l.each { |x| length += x } | |
length += 4 + 3*(l.size-1)+1 | |
out = " +".cyan | |
length.times do | |
out += "-".cyan | |
end | |
out += "+\n".cyan | |
out += " | ".cyan | |
content[0].each_with_index do |h,i| | |
out += h.light_magenta | |
((l[i]-h.size)+1).times { out += " " } | |
out += " | ".cyan | |
end | |
out += "\n" | |
content.delete_at(0) | |
out += " +".cyan | |
length.times do | |
out += "-".cyan | |
end | |
out += "+\n".cyan | |
content.each do |row| | |
out += " | ".cyan | |
row.each_with_index do |h,i| | |
out += h.yellow | |
((l[i]-h.size)+1).times { out += " " } | |
out += " | ".cyan | |
end | |
out += "\n" | |
end | |
out += " +".cyan | |
length.times do | |
out += "-".cyan | |
end | |
out += "+\n".cyan | |
return out | |
end | |
end |