diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/download.rb | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/lib/download.rb b/lib/download.rb index 6d1b7fb..308e8ee 100644 --- a/lib/download.rb +++ b/lib/download.rb @@ -8,6 +8,8 @@ require_relative '../config/config' require_relative 'package' require 'net/http' require 'pathname' +require 'progressbar' +require 'term/ansicolor' module SlackBuilder extend Rake::FileUtilsExt @@ -28,16 +30,22 @@ module SlackBuilder end end +def write_chunk(response, checksum, progressbar, io) + response.read_body do |chunk| + progressbar.progress += chunk.length + io << chunk + checksum << chunk + end +end + def write_download(target, response) checksum = Digest::MD5.new + progressbar = ProgressBar.create title: target, total: response.header.content_length File.open target, 'w' do |io| - response.read_body do |chunk| - print '.' - io << chunk - checksum << chunk - end + write_chunk response, checksum, progressbar, io end + progressbar.finish checksum end @@ -63,7 +71,7 @@ def start_download(uri, target, http) end def download(uri, target) - print "Downloading #{uri} " + print Term::ANSIColor.green "Downloading #{uri} " checksum = nil Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| |
