[Rubygems-developers] progressbar for remote installs

Henrik Horneber ryco at gmx.net
Thu Oct 7 15:42:45 EDT 2004


Hi!

Remote installations take some time, when you have p2p using flatmates 
like I have. In order to see the progress of the download, I hacked 
remote_installer.rb to include a progressbar, from the 'progressbar' gem 
(d'uh).  It's a more or less straight adoption from the open-uri docs, 
beware tho, it's nuby code nevertheless.

I thought I share it, just in case anyone is interested. :)

gems are a great way to handle libraries, thanks for the effort!

regards,
Henrik


-------------- next part --------------
--- remote_installer_old.rb	2004-10-07 18:35:03.250000000 +0200
+++ remote_installer.rb	2004-10-07 21:24:54.375000000 +0200
@@ -219,9 +219,25 @@
     
     def fetch( uri_str )
       require 'rubygems/open-uri'
-      open(uri_str, :proxy => @http_proxy) do |input|
-        input.read
+      require 'progressbar'
+      pbar = nil
+      opts = {
+        :content_length_proc => lambda {|total_length|
+          if total_length && 0 < total_length
+            pbar = ProgressBar.new("transfer", total_length)          
+          end
+        },
+        :progress_proc => lambda {|fetched_size|
+          pbar.set fetched_size if pbar
+        },
+	:proxy => @http_proxy
+      }
+      data = nil
+      open(uri_str, opts ) do |input|
+        data = input.read
       end
+      pbar.finish if pbar
+      data
     end
 
     def new_installer(gem)


More information about the Rubygems-developers mailing list