Skip to content

Instantly share code, notes, and snippets.

@b1nary
Created January 24, 2012 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b1nary/1673371 to your computer and use it in GitHub Desktop.
Save b1nary/1673371 to your computer and use it in GitHub Desktop.
Colored Table from Array in Ruby

Colored Table from Array in Ruby

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment