Created
October 9, 2019 04:40
-
-
Save yalab/3ce0e2ea5d85d69c2e7f901704b0cb57 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
require 'csv' | |
body = if ARGV.length > 0 | |
ARGF.read | |
else | |
STDIN.read | |
end | |
csv = CSV.new(body) | |
def wrap(cols, gap=nil) | |
if gap | |
cols = gap.map.with_index{|length, i| sprintf("%-#{length}s", cols[i]) } | |
end | |
"|#{cols.join('|')}|" | |
end | |
gap = nil | |
while !csv.eof? | |
row = csv.readline | |
unless gap | |
cols = row.to_a | |
puts wrap(cols) | |
gap = cols.map{|col| col.length } | |
puts wrap(cols.map{|col| col.gsub(/./, '-') if col }) | |
next | |
end | |
puts wrap(row.to_a, gap) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment