Created
March 29, 2014 23:13
-
-
Save JackDanger/9864648 to your computer and use it in GitHub Desktop.
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
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