Skip to content

Instantly share code, notes, and snippets.

@yalab
Created October 9, 2019 04:40
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 yalab/3ce0e2ea5d85d69c2e7f901704b0cb57 to your computer and use it in GitHub Desktop.
Save yalab/3ce0e2ea5d85d69c2e7f901704b0cb57 to your computer and use it in GitHub Desktop.
#!/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