Class: Speedometer

Inherits:
Object
  • Object
show all
Defined in:
lib/speedometer.rb

Overview

speedometer - class to track, calculate and display upload speed from an application Copyright © 2013, Tadeus Dobrovolskij This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

Methods

  • new - accepts hash with: units in KB/MB(default)/GB; progressbar - bool

  • start - start displaying upload speed

  • stop - stops displaying upload rate

  • done(bytesize) - increments uploaded byte counter for progressbar

  • log(message) - you need to use this instead of puts

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Speedometer

Returns a new instance of Speedometer.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/speedometer.rb', line 29

def initialize(**options)
  units = options[:units]
  units = 'MB' if units.nil?
  if options[:progressbar]
    @progressbar = true
  else
    @progressbar = false
  end
  @to_upload = 0
  @done = 0
  @cols = `tput cols`.split.last.to_i
  @active = true
  @work_to_do = false
  @refresh_time = 1000
  @msg_lock = Mutex.new
  if ["KB","MB","GB"].include?(units)
    @units = units
  else
    @units = "MB"
  end
end

Instance Attribute Details

#activeObject

Returns the value of attribute active.



27
28
29
# File 'lib/speedometer.rb', line 27

def active
  @active
end

#refresh_timeObject

Returns the value of attribute refresh_time.



27
28
29
# File 'lib/speedometer.rb', line 27

def refresh_time
  @refresh_time
end

#to_uploadObject

Returns the value of attribute to_upload.



27
28
29
# File 'lib/speedometer.rb', line 27

def to_upload
  @to_upload
end

#uploadedObject

Returns the value of attribute uploaded.



27
28
29
# File 'lib/speedometer.rb', line 27

def uploaded
  @uploaded
end

Instance Method Details

#clearObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/speedometer.rb', line 51

def clear
  @msg_lock.synchronize do
    print "\r"
    STDOUT.flush
    print "#{' ' * @cols}"
    STDOUT.flush
    print "\r"
    STDOUT.flush
  end
end

#done(size) ⇒ Object



95
96
97
98
# File 'lib/speedometer.rb', line 95

def done(size)
  abort "Upload size needs to be positive!" if size < 0
  @done += size
end

#log(msg, **opts) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/speedometer.rb', line 62

def log(msg, **opts)
  clear
  @msg_lock.synchronize do
    if opts[:stderr]
	STDERR.puts msg
    else
      puts msg
    end
  end
  if @started
    display
  end
end

#startObject



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/speedometer.rb', line 76

def start
  @start_time = Time.now if @start_time.nil?
  if !@started
    @t = Thread.new {
      while @active || @work_to_do
        display
        sleep @refresh_time.to_f / 1000
      end
    }
    @started = true
  end
end

#stopObject



89
90
91
92
93
# File 'lib/speedometer.rb', line 89

def stop
  @active = false
  @started = false
  @t.join
end