You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildr.apache.org by as...@apache.org on 2008/03/26 08:45:56 UTC

svn commit: r641190 - in /incubator/buildr/trunk: NOTICE Rakefile buildfile lib/core/progressbar.rb lib/core/transports.rb

Author: assaf
Date: Wed Mar 26 00:45:55 2008
New Revision: 641190

URL: http://svn.apache.org/viewvc?rev=641190&view=rev
Log:
Latest version of progressbar, replacing the one from Facets

Added:
    incubator/buildr/trunk/lib/core/progressbar.rb
Modified:
    incubator/buildr/trunk/NOTICE
    incubator/buildr/trunk/Rakefile
    incubator/buildr/trunk/buildfile
    incubator/buildr/trunk/lib/core/transports.rb

Modified: incubator/buildr/trunk/NOTICE
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/NOTICE?rev=641190&r1=641189&r2=641190&view=diff
==============================================================================
--- incubator/buildr/trunk/NOTICE (original)
+++ incubator/buildr/trunk/NOTICE Wed Mar 26 00:45:55 2008
@@ -15,6 +15,11 @@
 http://www.intalio.com
 
 
+* progressbar - http://0xcc.net/ruby-progressbar/
+Licensed under the Ruby License
+Copyright (C) 2001-2005 Satoru Takabayashi <sa...@namazu.org>
+
+
 THIRD-PARTY DEPENDENCIES
 ------------------------
 Even though those dependencies aren't shipped with Buildr, Buildr needs 
@@ -59,10 +64,6 @@
 * ruwiki (archive-tar-minitar) - http://rubyforge.org/projects/ruwiki/
 Licensed under the Ruby License
 Copyright 2004 Mauricio Julio Fernández Pradier and Austin Ziegler
-
-* progressbar - http://0xcc.net/ruby-progressbar/
-Licensed under the Ruby License
-Copyright (C) 2001 Satoru Takabayashi <sa...@namazu.org>
 
 
 MATERIALS IN DOCUMENTATION

Modified: incubator/buildr/trunk/Rakefile
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/Rakefile?rev=641190&r1=641189&r2=641190&view=diff
==============================================================================
--- incubator/buildr/trunk/Rakefile (original)
+++ incubator/buildr/trunk/Rakefile Wed Mar 26 00:45:55 2008
@@ -38,7 +38,6 @@
     spec.add_dependency 'xml-simple',           '~> 1.0'
     spec.add_dependency 'archive-tar-minitar',  '~> 0.5'
     spec.add_dependency 'rubyforge',            '~> 0.4'
-    spec.add_dependency 'progressbar',          '~> 0.0.3'
     
     spec.platform = platform
     spec.add_dependency 'rjb', '~> 1.1' unless platform == 'java'

Modified: incubator/buildr/trunk/buildfile
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/buildfile?rev=641190&r1=641189&r2=641190&view=diff
==============================================================================
--- incubator/buildr/trunk/buildfile (original)
+++ incubator/buildr/trunk/buildfile Wed Mar 26 00:45:55 2008
@@ -1,6 +1,8 @@
 require 'buildr/jetty'
 $LOADED_FEATURES << 'jruby' unless RUBY_PLATFORM =~ /java/ # Pretend to have JRuby, keeps Nailgun happy.
 require 'java/nailgun'
+repositories.remote << 'http://repo1.maven.org/maven2'
+
 
 define 'buildr' do
   compile.using :source=>'1.4', :target=>'1.4', :debug=>false

Added: incubator/buildr/trunk/lib/core/progressbar.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/core/progressbar.rb?rev=641190&view=auto
==============================================================================
--- incubator/buildr/trunk/lib/core/progressbar.rb (added)
+++ incubator/buildr/trunk/lib/core/progressbar.rb Wed Mar 26 00:45:55 2008
@@ -0,0 +1,236 @@
+#
+# Ruby/ProgressBar - a text progress bar library
+#
+# Copyright (C) 2001-2005 Satoru Takabayashi <sa...@namazu.org>
+#     All rights reserved.
+#     This is free software with ABSOLUTELY NO WARRANTY.
+#
+# You can redistribute it and/or modify it under the terms
+# of Ruby's license.
+#
+
+class ProgressBar
+  VERSION = "0.9"
+
+  def initialize (title, total, out = STDERR)
+    @title = title
+    @total = total
+    @out = out
+    @terminal_width = 80
+    @bar_mark = "o"
+    @current = 0
+    @previous = 0
+    @finished_p = false
+    @start_time = Time.now
+    @previous_time = @start_time
+    @title_width = 14
+    @format = "%-#{@title_width}s %3d%% %s %s"
+    @format_arguments = [:title, :percentage, :bar, :stat]
+    clear
+    show
+  end
+  attr_reader   :title
+  attr_reader   :current
+  attr_accessor :start_time
+  attr_accessor :bar_mark
+
+  private
+  def fmt_bar
+    bar_width = do_percentage * @terminal_width / 100
+    sprintf("|%s%s|", 
+            @bar_mark * bar_width, 
+            " " *  (@terminal_width - bar_width))
+  end
+
+  def fmt_percentage
+    do_percentage
+  end
+
+  def fmt_stat
+    if @finished_p then elapsed else eta end
+  end
+
+  def fmt_stat_for_file_transfer
+    if @finished_p then 
+      sprintf("%s %s %s", bytes, transfer_rate, elapsed)
+    else 
+      sprintf("%s %s %s", bytes, transfer_rate, eta)
+    end
+  end
+
+  def fmt_title
+    @title[0,(@title_width - 1)] + ":"
+  end
+
+  def convert_bytes (bytes)
+    if bytes < 1024
+      sprintf("%6dB", bytes)
+    elsif bytes < 1024 * 1000 # 1000kb
+      sprintf("%5.1fKB", bytes.to_f / 1024)
+    elsif bytes < 1024 * 1024 * 1000  # 1000mb
+      sprintf("%5.1fMB", bytes.to_f / 1024 / 1024)
+    else
+      sprintf("%5.1fGB", bytes.to_f / 1024 / 1024 / 1024)
+    end
+  end
+
+  def transfer_rate
+    bytes_per_second = @current.to_f / (Time.now - @start_time)
+    sprintf("%s/s", convert_bytes(bytes_per_second))
+  end
+
+  def bytes
+    convert_bytes(@current)
+  end
+
+  def format_time (t)
+    t = t.to_i
+    sec = t % 60
+    min  = (t / 60) % 60
+    hour = t / 3600
+    sprintf("%02d:%02d:%02d", hour, min, sec);
+  end
+
+  # ETA stands for Estimated Time of Arrival.
+  def eta
+    if @current == 0
+      "ETA:  --:--:--"
+    else
+      elapsed = Time.now - @start_time
+      eta = elapsed * @total / @current - elapsed;
+      sprintf("ETA:  %s", format_time(eta))
+    end
+  end
+
+  def elapsed
+    elapsed = Time.now - @start_time
+    sprintf("Time: %s", format_time(elapsed))
+  end
+  
+  def eol
+    if @finished_p then "\n" else "\r" end
+  end
+
+  def do_percentage
+    if @total.zero?
+      100
+    else
+      @current  * 100 / @total
+    end
+  end
+
+  def get_width
+    # FIXME: I don't know how portable it is.
+    default_width = 80
+    begin
+      tiocgwinsz = 0x5413
+      data = [0, 0, 0, 0].pack("SSSS")
+      if @out.ioctl(tiocgwinsz, data) >= 0 then
+        rows, cols, xpixels, ypixels = data.unpack("SSSS")
+        if cols >= 0 then cols else default_width end
+      else
+        default_width
+      end
+    rescue Exception
+      default_width
+    end
+  end
+
+  def show
+    arguments = @format_arguments.map {|method| 
+      method = sprintf("fmt_%s", method)
+      send(method)
+    }
+    line = sprintf(@format, *arguments)
+
+    width = get_width
+    if line.length == width - 1 
+      @out.print(line + eol)
+      @out.flush
+    elsif line.length >= width
+      @terminal_width = [@terminal_width - (line.length - width + 1), 0].max
+      if @terminal_width == 0 then @out.print(line + eol) else show end
+    else # line.length < width - 1
+      @terminal_width += width - line.length + 1
+      show
+    end
+    @previous_time = Time.now
+  end
+
+  def show_if_needed
+    if @total.zero?
+      cur_percentage = 100
+      prev_percentage = 0
+    else
+      cur_percentage  = (@current  * 100 / @total).to_i
+      prev_percentage = (@previous * 100 / @total).to_i
+    end
+
+    # Use "!=" instead of ">" to support negative changes
+    if cur_percentage != prev_percentage || 
+        Time.now - @previous_time >= 1 || @finished_p
+      show
+    end
+  end
+
+  public
+  def clear
+    @out.print "\r"
+    @out.print(" " * (get_width - 1))
+    @out.print "\r"
+  end
+
+  def finish
+    @current = @total
+    @finished_p = true
+    show
+  end
+
+  def finished?
+    @finished_p
+  end
+
+  def file_transfer_mode
+    @format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]
+  end
+
+  def format= (format)
+    @format = format
+  end
+
+  def format_arguments= (arguments)
+    @format_arguments = arguments
+  end
+
+  def halt
+    @finished_p = true
+    show
+  end
+
+  def inc (step = 1)
+    @current += step
+    @current = @total if @current > @total
+    show_if_needed
+    @previous = @current
+  end
+
+  def set (count)
+    if count < 0 || count > @total
+      raise "invalid count: #{count} (total: #{@total})"
+    end
+    @current = count
+    show_if_needed
+    @previous = @current
+  end
+
+  def inspect
+    "#<ProgressBar:#{@current}/#{@total}>"
+  end
+end
+
+class ReversedProgressBar < ProgressBar
+  def do_percentage
+    100 - super
+  end
+end
+

Modified: incubator/buildr/trunk/lib/core/transports.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/core/transports.rb?rev=641190&r1=641189&r2=641190&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/core/transports.rb (original)
+++ incubator/buildr/trunk/lib/core/transports.rb Wed Mar 26 00:45:55 2008
@@ -23,7 +23,7 @@
 require 'uri/sftp'
 require 'digest/md5'
 require 'digest/sha1'
-require 'progressbar'
+require 'core/progressbar'
 require 'tempfile'
 
 
@@ -260,13 +260,7 @@
     # Call << for each block of bytes down/uploaded.
     def with_progress_bar(enable, file_name, size) #:nodoc:
       if enable && $stdout.isatty && size
-        progress_bar = Console::ProgressBar.new(file_name, size)
-        # Extend the progress bar so we can display count/total.
-        class << progress_bar
-          def total()
-            convert_bytes(@total)
-          end
-        end
+        progress_bar = ProgressBar.new(file_name, size)
         # Squeeze the filename into 30 characters.
         if file_name.size > 30
           base, ext = file_name.split('.')
@@ -274,9 +268,9 @@
         else
           truncated = file_name
         end
-        progress_bar.format = "#{truncated}: %3d%% %s %s/%s %s"
-        progress_bar.format = '%3d%% %s %s/%s %s'
-        progress_bar.format_arguments = [:percentage, :bar, :bytes, :total, :stat]
+        progress_bar.format = "#{truncated}: %3d%% %s %s" # %s/%s %s"
+        #progress_bar.format = '%3d%% %s %s/%s %s'
+        progress_bar.format_arguments = [:percentage, :bar, :stat_for_file_transfer] # :bytes, :total, :stat]
         progress_bar.bar_mark = '.'
 
         begin