[Rubygems-developers] progressbar for remote installs

Henrik Horneber ryco at gmx.net
Sun Oct 10 09:23:47 EDT 2004


Hi!

Again, another shot at the progressbar feature, this time a little bit 
more seriously.

   For some reason, probably because we're still in initialization 
phase, require 'progressbar' fails if you have installed it via gems. 
Manual installation works. Latest progressbar on its website, 
http://namazu.org/~satoru/ruby-progressbar/, is 0.8. This crashes on my 
windows machine, so I went back to version 0.3. Haven't noticed any 
problems so far.

regards,
Henrik
-------------- next part --------------
diff -u -b rubygems_org/remote_installer.rb rubygems/remote_installer.rb
--- rubygems_org/remote_installer.rb	2004-10-07 18:35:03.250000000 +0200
+++ rubygems/remote_installer.rb	2004-10-10 15:12:25.265625000 +0200
@@ -219,9 +219,21 @@
     
     def fetch( uri_str )
       require 'rubygems/open-uri'
-      open(uri_str, :proxy => @http_proxy) do |input|
-        input.read
+      opts = {
+        :content_length_proc => lambda {|total_size|
+          report_size_to_download total_size
+        },
+        :progress_proc => lambda {|fetched_size|
+          report_download_progress fetched_size
+        },
+        :proxy => @http_proxy
+      }
+      data = nil
+      open(uri_str, opts ) do |input|
+        data = input.read
       end
+      report_download_finished
+      data
     end
 
     def new_installer(gem)
diff -u -b rubygems_org/user_interaction.rb rubygems/user_interaction.rb
--- rubygems_org/user_interaction.rb	2004-10-07 18:35:03.250000000 +0200
+++ rubygems/user_interaction.rb	2004-10-10 15:14:44.296875000 +0200
@@ -1,3 +1,6 @@
+
+
+
 module Gem
 
   ##
@@ -59,7 +62,8 @@
     include DefaultUserInteraction
     [
       :choose_from_list, :ask, :ask_yes_no, :say, :alert, :alert_warning,
-      :alert_error, :terminate_interaction!, :terminate_interaction
+      :alert_error, :terminate_interaction!, :terminate_interaction, 
+      :report_size_to_download, :report_download_progress, :report_download_finished
     ].each do |methname|
       class_eval %{
         def #{methname}(*args)
@@ -168,8 +172,37 @@
   # Subclass of StreamUI that instantiates the user interaction using
   # standard in, out and error.
   class ConsoleUI < StreamUI
-    def initialize
+  # having progressbar as a gem somehow does not work here
+  # get it from  http://namazu.org/~satoru/ruby-progressbar/ruby-progressbar-0.3.tar.gz
+  # there are newer versions, but they crashed on me.
+  # even though it did work in remote_installer.rb
+  # probably we're not done setting up here
+  require 'progressbar'      
+     
+    def initialize( use_progressbar = true )
       super(STDIN, STDOUT, STDERR)
+      @progressbar=nil
+      @use_progressbar = use_progressbar
+    end
+     
+    #set up progress bar with total download size as 100%     
+    def report_size_to_download(total_size)
+        if @use_progressbar && total_size && 0 < total_size 
+            @progressbar= ProgressBar.new("transfer", total_size)
+	    # next line does not work with progressbar v0.3
+	    # progress bar 0.8 crashes on win2k	    
+            # @progressbar.file_transfer_mode	    
+	end
+    end
+    
+    # tell progressbar to update itself
+    def report_download_progress(fetched_size)
+       @progressbar.set fetched_size if @progressbar
+    end
+    
+    def report_download_finished()
+       @progressbar.finish if @progressbar
+       @progressbar = nil
     end
   end
 end


More information about the Rubygems-developers mailing list