class DownloadProgress
An object of this class will inform about the download-progress and give estimates.
Constants
- Show_Seconds
Minimum of remaining seconds to have their number displayed. See below. With a value of 120, the first possible indication of seconds will be: 1 minute, 59 seconds
Public Class Methods
new(file, size = 0, do_start = true)
click to toggle source
Sets some reasonable defaults and starts counting, if do_start is true
# File lib/download_progress.rb, line 41 def initialize(file, size = 0, do_start = true) @bi = nil @start_time = nil @log = @@log @file = file @size = size start if do_start end
Public Instance Methods
progress()
click to toggle source
retrives or calculates values
# File lib/download_progress.rb, line 67 def progress() if(@start_time) @current_size = File.size(@file) @percent = @current_size.to_f / (@size.to_f / 100) @remaining_time = nil @dsize = nil if(@current_size > 1000) now = Time.now time = now.to_i - @start_time.to_i @dsize = @current_size - @initial_size if @dsize > 0 btime = time.to_f / @dsize total_time = (@size * btime).to_i @remaining_time = (@start_time + total_time) - now.to_i end end end end
progress_bar()
click to toggle source
returns a progress bar, showing the amount of data already downloaded.
# File lib/download_progress.rb, line 87 def progress_bar progress "[%-50s] (%i/%i)" %[('#' * ((50.0/100) * @percent)), time_units(@remaining_time, with_secs ), byte_units(@dsize.to_f/time)] end
start()
click to toggle source
Starts counting bytes and seconds
# File lib/download_progress.rb, line 51 def start if(File.exist?(@file) ) @start_time = Time.now @initial_size = File.size(@file) # @log.debug('initial size is ' << @initial_size.to_s) @bi = BusyIndicator.new(true, 100) {sleep 0.4; status} else @log.error('The file ' << @file << 'does not exist! Aborting.') exit false end end
status()
click to toggle source
gives a summary of the download status and an estimate TODO: define fixed-with fields for all data.
# File lib/download_progress.rb, line 94 def status() progress # puts "progress() calculates : @current_size " << @current_size << ", @remaining_time " << @remaining_time if(@start_time) p = "%s of %s (%.2f%%). " %[byte_units(@current_size, 0), byte_units(@size, 0), @percent] if(@remaining_time) # show seconds if less than Show_Seconds seconds remain with_secs = (@remaining_time < Show_Seconds) p << "Time remaining: %s (%s/sec)" %[time_units(@remaining_time, with_secs ), byte_units(@dsize.to_f/time) ] end end end
stop()
click to toggle source
# File lib/download_progress.rb, line 63 def stop @bi.stop if @bi end
Private Instance Methods
time_units(time, with_secs = false)
click to toggle source
Creates a string-representation of the time period 'time'. Includes the seconds only if with_secs is true.
# File lib/download_progress.rb, line 111 def time_units(time, with_secs = false) # seconds in a full day days = 24*60*60 # in a full hour hours = 60*60 # in a full minute minutes = 60 @log.debug('time is ' << time.to_s) tu = '' if time > days tu << "#{(time / days)} days" end # remaining time, partial day pday = time % days if pday > 0 tu << ", " if !tu.empty? tu << "#{(pday) / hours} hours" if (pday) > hours tu << "#{(pday) / hours} hour" if (pday) == hours end # remaining time, partial hour phour = time % hours if phour > 0 tu << ", " if !tu.empty? tu << "#{(phour) / minutes} minutes" if (phour) >= minutes tu << "#{(phour) / minutes} minute" if (phour) == minutes end # remaining time, partial minute pminute = time % minutes if with_secs && (pminute > 0) tu << ", " if !tu.empty? tu << "#{(pminuts)} seconds" if (pminute) > 0 tu << "#{(pminuts)} second" if (pminute) == 1 end # I feel like adding stuff before returning the result tu end