Author: assaf
Date: Mon Mar 31 15:32:27 2008
New Revision: 643191

URL: http://svn.apache.org/viewvc?rev=643191&view=rev
Log:
Switched to our very own, new and improved, progress bar for download/upload.

Modified:
    incubator/buildr/trunk/lib/buildr.rb
    incubator/buildr/trunk/lib/core/common.rb
    incubator/buildr/trunk/lib/core/generate.rb
    incubator/buildr/trunk/lib/core/progressbar.rb
    incubator/buildr/trunk/lib/core/transports.rb
    incubator/buildr/trunk/spec/transport_spec.rb

Modified: incubator/buildr/trunk/lib/buildr.rb
URL: 
http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr.rb?rev=643191&r1=643190&r2=643191&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/buildr.rb (original)
+++ incubator/buildr/trunk/lib/buildr.rb Mon Mar 31 15:32:27 2008
@@ -21,7 +21,6 @@
 require 'needle'
 require 'net/ssh'
 
-require 'highline'
 require 'highline/import'
 require 'builder' # A different kind of buildr, one we use to create XML.
 

Modified: incubator/buildr/trunk/lib/core/common.rb
URL: 
http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/core/common.rb?rev=643191&r1=643190&r2=643191&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/core/common.rb (original)
+++ incubator/buildr/trunk/lib/core/common.rb Mon Mar 31 15:32:27 2008
@@ -310,7 +310,7 @@
 
 
 # Add a touch of colors (red) to warnings.
-HighLine.use_color = PLATFORM !~ /win32/
+HighLine.use_color = !Gem.win_platform?
 module Kernel #:nodoc:
 
   alias :warn_without_color :warn

Modified: incubator/buildr/trunk/lib/core/generate.rb
URL: 
http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/core/generate.rb?rev=643191&r1=643190&r2=643191&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/core/generate.rb (original)
+++ incubator/buildr/trunk/lib/core/generate.rb Mon Mar 31 15:32:27 2008
@@ -21,7 +21,7 @@
 
     task "generate" do
       script = nil 
-      HighLine.new.choose do |menu|
+      choose do |menu|
         menu.header = "To use Buildr you need a buildfile. Do you want me to 
create one?"
 
         menu.choice("From maven2 pom file") { script = 
Generate.from_maven2_pom(true).join("\n") } if File.exists?("pom.xml")

Modified: incubator/buildr/trunk/lib/core/progressbar.rb
URL: 
http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/core/progressbar.rb?rev=643191&r1=643190&r2=643191&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/core/progressbar.rb (original)
+++ incubator/buildr/trunk/lib/core/progressbar.rb Mon Mar 31 15:32:27 2008
@@ -1,241 +1,149 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with this
+# work for additional information regarding copyright ownership.  The ASF
+# licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
 #
-# Ruby/ProgressBar - a text progress bar library
+#    http://www.apache.org/licenses/LICENSE-2.0
 #
-# Copyright (C) 2001-2005 Satoru Takabayashi <[EMAIL PROTECTED]>
-#     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"
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations under
+# the License.
 
-  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 = "[EMAIL PROTECTED] %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
+class ProgressBar
 
-  def fmt_title
-    @title[0,(@title_width - 1)] + ":"
-  end
+  class << self
 
-  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)
+    def start(args, &block)
+      new(args).start &block
     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);
+  def initialize(args = {})
+    @title = args[:title] || ''
+    @total = args[:total] || 0
+    @mark = args[:mark] || '.'
+    @format = args[:format] ||
+      @total == 0 ? ['%s %8s %s', :title, :count, :elapsed] : ['%s: %s |--| 
%8s/%s %s', :title, :percentage, :count, :total, :time]
+    @width = $terminal.output_cols - 1
+    @output = args[:output] || $stderr unless args[:hidden] || !$stdout.isatty
+    clear
   end
 
-  # ETA stands for Estimated Time of Arrival.
-  def eta
-    if @current == 0
-      "ETA:  --:--:--"
+  def start
+    @start = @last_time = Time.now
+    @count = 0
+    render
+    if block_given?
+      result = yield(self) if block_given?
+      finish
+      result
     else
-      elapsed = Time.now - @start_time
-      eta = elapsed * @total / @current - elapsed;
-      sprintf("ETA:  %s", format_time(eta))
+      self
     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
+  def inc(count)
+    set @count + count
   end
 
-  def do_percentage
-    if @total.zero?
-      100
-    else
-      @current  * 100 / @total
-    end
+  def <<(bytes)
+    inc bytes.size
   end
 
-  def get_width
-    @screen_width ||= HighLine::SystemExtensions.terminal_size.first rescue 80
-    return @screen_width
-    # Original code below, above code adds dependency on HighLine.
-=begin
-    # 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 set(count)
+    @count = [count, 0].max
+    @count = [count, @total].min unless @total == 0
+    render if changed?
   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 = [EMAIL PROTECTED] - (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
+  def title
+    @title.size > @width / 5 ? (@title[0, @width / 5 - 2] + '..') : @title 
   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
+  def count
+    human(@count)
+  end
 
-    # Use "!=" instead of ">" to support negative changes
-    if cur_percentage != prev_percentage || 
-        Time.now - @previous_time >= 1 || @finished_p
-      show
-    end
+  def total
+    human(@total)
   end
 
-  public
-  def clear
-    @out.print "\r"
-    @out.print(" " * (get_width - 1))
-    @out.print "\r"
+  def percentage
+    '%3d%%' % (@total == 0 ? 100 : (@count * 100 / @total))
   end
 
-  def finish
-    @current = @total
-    @finished_p = true
-    show
+  def time
+    @finished ? elapsed : eta
   end
 
-  def finished?
-    @finished_p
+  def eta
+    return 'ETA:  --:--:--' if @count == 0
+    elapsed = Time.now - @start
+    eta = elapsed * @total / @count - elapsed
+    'ETA:  %s' % duration(eta.ceil)
   end
 
-  def file_transfer_mode
-    @format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]
+  def elapsed
+    'Time: %s' % duration(Time.now - @start) 
   end
 
-  def format= (format)
-    @format = format
+  def rate
+    '%s/s' % human(@count / (Time.now - @start))
   end
 
-  def format_arguments= (arguments)
-    @format_arguments = arguments
+  def progress(width)
+    width -= 2
+    marks = @total == 0 ? width : (@count * width / @total)
+    "|%-#{width}s|" % (@mark * marks)
   end
 
-  def halt
-    @finished_p = true
-    show
+  def human(bytes)
+    magnitude = (0..3).find { |i| bytes < (1024 << i * 10) } || 3
+    return '%dB' % bytes if magnitude == 0
+    return '%.1f%s' % [ bytes.to_f / (1 << magnitude * 10), [nil, 'KB', 'MB', 
'GB'][magnitude] ]
   end
 
-  def inc (step = 1)
-    @current += step
-    @current = @total if @current > @total
-    show_if_needed
-    @previous = @current
+  def duration(seconds)
+    '%02d:%02d:%02d' % [seconds / 3600, (seconds / 60) % 60, seconds % 60]
   end
 
-  def set (count)
-    if count < 0 || count > @total
-      raise "invalid count: #{count} (total: [EMAIL PROTECTED])"
+  def finish
+    unless @finished
+      @finished = true
+      render
     end
-    @current = count
-    show_if_needed
-    @previous = @current
   end
 
-  def inspect
-    "#<ProgressBar:[EMAIL PROTECTED]/[EMAIL PROTECTED]>"
-  end
-end
+protected
 
-class ReversedProgressBar < ProgressBar
-  def do_percentage
-    100 - super
+  def clear
+    return unless @output
+    @output.print "\r", " " * @width, "\r"
+    @output.flush
+  end
+    
+  def render
+    return unless @output
+    format, *args = @format
+    line = format % args.map { |arg| send(arg) }
+    @output.print line.sub('|--|') { progress(@width - line.size + 4) }
+    @output.print @finished ? "\n" : "\r"
+    @output.flush
+    @previous = @count
+    @last_time = Time.now
+  end
+
+  def changed?
+    return false unless @output
+    return human(@count) != human(@previous) if @total == 0
+    return true if (@count - @previous) >= @total / 100
+    return Time.now - @last_time > 1
   end
-end
 
+end

Modified: incubator/buildr/trunk/lib/core/transports.rb
URL: 
http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/core/transports.rb?rev=643191&r1=643190&r2=643191&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/core/transports.rb (original)
+++ incubator/buildr/trunk/lib/core/transports.rb Mon Mar 31 15:32:27 2008
@@ -248,7 +248,7 @@
   protected
 
     # :call-seq:
-    #   with_progress_bar(enable, file_name, size) { |progress| ... }
+    #   with_progress_bar(show, file_name, size) { |progress| ... }
     #
     # Displays a progress bar while executing the block. The first argument 
must be true for the
     # progress bar to show (TTY output also required), as a convenient for 
selectively using the
@@ -258,39 +258,10 @@
     #
     # The block is yielded with a progress object that implements a single 
method.
     # Call << for each block of bytes down/uploaded.
-    def with_progress_bar(enable, file_name, size) #:nodoc:
-      if enable && $stdout.isatty && size
-        progress_bar = ProgressBar.new(file_name, size, $stdout)
-        # Squeeze the filename into 30 characters.
-        if file_name.size > 30
-          base, ext = file_name.split('.')
-          truncated = "#{base[0..26-ext.to_s.size]}...#{ext}"
-        else
-          truncated = file_name
-        end
-        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
-          class << progress_bar
-            def <<(bytes)
-              inc bytes.respond_to?(:size) ? bytes.size : bytes
-            end
-          end
-          yield progress_bar
-        ensure
-          progress_bar.finish
-        end
-      else
-        progress_bar = Object.new
-        class << progress_bar
-          def <<(bytes)
-          end
-        end
-        yield progress_bar
-      end
+    def with_progress_bar(show, file_name, size, &block) #:nodoc:
+      options = { :total=>size, :title=>file_name }
+      options[:hidden] = true unless show
+      ProgressBar.start options, &block
     end
 
     # :call-seq:
@@ -393,7 +364,7 @@
       rescue Net::SSH::AuthenticationFailed=>ex
         # Only if running with console, prompt for password.
         if !ssh_options[:password] && $stdout.isatty
-          password = HighLine.new.ask("Password for #{host}:") { |q| q.echo = 
'*' }
+          password = ask("Password for #{host}:") { |q| q.echo = '*' }
           ssh_options[:password] = password
           retry
         end

Modified: incubator/buildr/trunk/spec/transport_spec.rb
URL: 
http://svn.apache.org/viewvc/incubator/buildr/trunk/spec/transport_spec.rb?rev=643191&r1=643190&r2=643191&view=diff
==============================================================================
--- incubator/buildr/trunk/spec/transport_spec.rb (original)
+++ incubator/buildr/trunk/spec/transport_spec.rb Mon Mar 31 15:32:27 2008
@@ -286,6 +286,7 @@
     ok.stub!(:read_body)
     @http.stub!(:request).and_yield(ok)
     Net::HTTP.should_receive(:new).and_return(@http)
+    $stdout.should_receive(:isatty).and_return(false)
     @uri.read :progress=>true
   end
 


Reply via email to