Skip to content

Instantly share code, notes, and snippets.

@junegunn
Last active August 29, 2015 14:13
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 junegunn/b3386e912ee9229824ef to your computer and use it in GitHub Desktop.
Save junegunn/b3386e912ee9229824ef to your computer and use it in GitHub Desktop.
Ruby filter for aligning tab-separated input
#!/usr/bin/env ruby
lines = []
maxes = []
while raw = gets
lines << line = raw.chomp.split(/\t+/)
line.each_with_index do |token, idx|
maxes[idx] = [maxes.fetch(idx, 0), token.length].max
end
end
lines.each do |line|
line.zip(maxes).each do |(token, width)|
print token.ljust(width + 1)
end
puts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment