Last active
August 29, 2015 14:13
-
-
Save junegunn/b3386e912ee9229824ef to your computer and use it in GitHub Desktop.
Ruby filter for aligning tab-separated input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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