Class: TimeLeft
- Inherits:
-
Object
- Object
- TimeLeft
- Defined in:
- lib/time_left.rb
Instance Attribute Summary collapse
-
#format ⇒ Object
Monitor time that is left to do some stuff.
-
#start_time ⇒ Object
Monitor time that is left to do some stuff.
-
#total ⇒ Object
Monitor time that is left to do some stuff.
-
#value ⇒ Object
Monitor time that is left to do some stuff.
Class Method Summary collapse
Instance Method Summary collapse
- #calculate_time ⇒ Object
- #eol ⇒ Object
- #finish ⇒ Object
-
#get_width ⇒ Object
from ruby progress_bar.
-
#initialize(options = {}, out = STDERR) ⇒ TimeLeft
constructor
A new instance of TimeLeft.
- #reset(options = {}) ⇒ Object
- #show ⇒ Object
-
#show_when(conditions) ⇒ Object
Use conditions to output time.
-
#tick(value = nil, &block) ⇒ Object
Start to show time ===Example t=TimeLeft.new(:total=>1000) 1.upto(10) do |i| t.tick(i) do show_when i>1 end end.
-
#use_format(format) ⇒ Object
Available format * %H - Hours of day (0..23) * %M - Minutes of hour (0..59) * %S - Seconds of minute (0..59) * %U - Miliseconds of second (0..999).
Constructor Details
#initialize(options = {}, out = STDERR) ⇒ TimeLeft
Returns a new instance of TimeLeft.
22 23 24 25 26 27 |
# File 'lib/time_left.rb', line 22 def initialize(={},out=STDERR) @out=out @finished=false @terminal_width=80 self.reset() end |
Instance Attribute Details
#format ⇒ Object
20 21 22 |
# File 'lib/time_left.rb', line 20 def format @format end |
#start_time ⇒ Object
20 21 22 |
# File 'lib/time_left.rb', line 20 def start_time @start_time end |
#total ⇒ Object
20 21 22 |
# File 'lib/time_left.rb', line 20 def total @total end |
#value ⇒ Object
20 21 22 |
# File 'lib/time_left.rb', line 20 def value @value end |
Class Method Details
.timer(options = {}, out = STDERR) {|timer| ... } ⇒ Object
29 30 31 32 33 |
# File 'lib/time_left.rb', line 29 def self.timer(={},out=STDERR) timer=self.new(,out) yield timer timer.finish end |
Instance Method Details
#calculate_time ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/time_left.rb', line 86 def calculate_time @current_time=nil if self.total.to_f>0 part_done=self.value.to_f/self.total.to_f end if part_done>0 spent_time=(Time.now-self.start_time) total_time=spent_time/part_done difference=total_time-spent_time if difference>0 time_left=Time.at(difference) h,m,s,ms=(time_left.hour-2),time_left.min,time_left.sec,time_left.usec h,m,s,ms=(h>9 ? h.to_s : "0#{h}"),(m>9 ? m.to_s : "0#{m}"),(s>9 ? s.to_s : "0#{s}"),(ms>99 ? ms.to_s : (ms>9 ? "0#{ms}" : "00#{ms}"))[0..2] @current_time=self.format.gsub(/%H/,h).gsub(/%M/,m).gsub(/%S/,s).gsub(/%U/,ms) end end end |
#eol ⇒ Object
120 121 122 |
# File 'lib/time_left.rb', line 120 def eol @finished ? "\n" : "\r" end |
#finish ⇒ Object
40 41 42 43 44 |
# File 'lib/time_left.rb', line 40 def finish @finished=true self.calculate_time show end |
#get_width ⇒ Object
from ruby progress_bar
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/time_left.rb', line 104 def get_width #from ruby progress_bar 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 |
#reset(options = {}) ⇒ Object
35 36 37 38 |
# File 'lib/time_left.rb', line 35 def reset(={}) .each{|k,v| self.send(:"#{k}=",v)} self.format="%H:%M:%S" unless self.format end |
#show ⇒ Object
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/time_left.rb', line 124 def show line =@current_time width = get_width if line.to_s.length < width - 1 @out.print("#{line}#{eol}") elsif line.length >= width @out.print("#{line}\n") end @out.flush end |
#show_when(conditions) ⇒ Object
Use conditions to output time.
Exammple
show_when a%5==0
show_when Time.now-self.start_time>0.5 # every half second
73 74 75 |
# File 'lib/time_left.rb', line 73 def show_when conditions @visible=true if conditions end |
#tick(value = nil, &block) ⇒ Object
Start to show time
Example
t=TimeLeft.new(:total=>1000)
1.upto(10) do |i|
t.tick(i) do
show_when i>1
end
end
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/time_left.rb', line 53 def tick(value=nil,&block) self.start_time=Time.now if !self.start_time self.value=value || Time.now.to_i if block_given? self.instance_eval(&block) else @visible=true end if @visible self.calculate_time self.show if @current_time @visible=false end end |
#use_format(format) ⇒ Object
Available format
-
%H - Hours of day (0..23)
-
%M - Minutes of hour (0..59)
-
%S - Seconds of minute (0..59)
-
%U - Miliseconds of second (0..999)
82 83 84 |
# File 'lib/time_left.rb', line 82 def use_format format self.format=format end |