Skip to content

Instantly share code, notes, and snippets.

@JackDanger
Created March 29, 2014 23: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 JackDanger/9864648 to your computer and use it in GitHub Desktop.
Save JackDanger/9864648 to your computer and use it in GitHub Desktop.
require 'csv'
def import(filename, out = $stdout, &block)
# Yes, there are gems that do progress bars.
# No, I'm not about to add another dependency for something this simple.
width = `tput cols`.to_i - 4
processed = 0
total = File.read(filename).lines.length.to_f - 1
label = File.basename(filename, '.csv')
out.puts "%11s:" % label
CSV.foreach(filename, headers: true) do |row|
yield row
processed += 1
percent = processed / total
wanted = (percent * width).ceil
out.print "-" * wanted
out.print " " * (width - wanted)
out.print (percent * 100).to_i.to_s + "%"
out.print "\r"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment