time_left 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/time_left.rb +61 -16
- data/time_left.gemspec +3 -3
- metadata +3 -3
data/Rakefile
CHANGED
data/lib/time_left.rb
CHANGED
@@ -9,27 +9,39 @@ class TimeLeft
|
|
9
9
|
# use_format "%S.%U"
|
10
10
|
# end
|
11
11
|
# end
|
12
|
+
# t.finish
|
12
13
|
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
# t.tick do
|
17
|
-
# data=collect_data_from_remote unless data
|
18
|
-
# self.total=data.size if data
|
19
|
-
# show_when Time.now-self.start_time>0.5
|
14
|
+
# TimeLeft.timer(:total=>total_records) do |t|
|
15
|
+
# 1.upto(100) do |i|
|
16
|
+
# t.tick(i)
|
20
17
|
# end
|
21
18
|
# end
|
22
|
-
|
19
|
+
#
|
20
|
+
attr_accessor :start_time,:format,:total,:value
|
23
21
|
|
24
|
-
def initialize(options={})
|
22
|
+
def initialize(options={},out=STDERR)
|
23
|
+
@out=out
|
24
|
+
@finished=false
|
25
|
+
@terminal_width=80
|
25
26
|
self.reset(options)
|
26
27
|
end
|
27
28
|
|
29
|
+
def self.timer(options={},out=STDERR)
|
30
|
+
timer=self.new(options,out)
|
31
|
+
yield timer
|
32
|
+
timer.finish
|
33
|
+
end
|
34
|
+
|
28
35
|
def reset(options={})
|
29
36
|
options.each{|k,v| self.send(:"#{k}=",v)}
|
30
37
|
self.format="%H:%M:%S" unless self.format
|
31
38
|
end
|
32
39
|
|
40
|
+
def finish
|
41
|
+
@finished=true
|
42
|
+
self.calculate_time
|
43
|
+
show
|
44
|
+
end
|
33
45
|
# Start to show time
|
34
46
|
# ===Example
|
35
47
|
# t=TimeLeft.new(:total=>1000)
|
@@ -45,11 +57,12 @@ class TimeLeft
|
|
45
57
|
if block_given?
|
46
58
|
self.instance_eval(&block)
|
47
59
|
else
|
48
|
-
|
60
|
+
@visible=true
|
49
61
|
end
|
50
|
-
if
|
51
|
-
self.
|
52
|
-
self.
|
62
|
+
if @visible
|
63
|
+
self.calculate_time
|
64
|
+
self.show if @current_time
|
65
|
+
@visible=false
|
53
66
|
end
|
54
67
|
end
|
55
68
|
|
@@ -58,7 +71,7 @@ class TimeLeft
|
|
58
71
|
# show_when a%5==0
|
59
72
|
# show_when Time.now-self.start_time>0.5 # every half second
|
60
73
|
def show_when conditions
|
61
|
-
|
74
|
+
@visible=true if conditions
|
62
75
|
end
|
63
76
|
|
64
77
|
# Available format
|
@@ -70,7 +83,8 @@ class TimeLeft
|
|
70
83
|
self.format=format
|
71
84
|
end
|
72
85
|
|
73
|
-
def
|
86
|
+
def calculate_time
|
87
|
+
@current_time=nil
|
74
88
|
if self.total.to_f>0
|
75
89
|
part_done=self.value.to_f/self.total.to_f
|
76
90
|
end
|
@@ -82,8 +96,39 @@ class TimeLeft
|
|
82
96
|
time_left=Time.at(difference)
|
83
97
|
h,m,s,ms=(time_left.hour-2),time_left.min,time_left.sec,time_left.usec
|
84
98
|
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]
|
85
|
-
|
99
|
+
@current_time=self.format.gsub(/%H/,h).gsub(/%M/,m).gsub(/%S/,s).gsub(/%U/,ms)
|
86
100
|
end
|
87
101
|
end
|
88
102
|
end
|
103
|
+
|
104
|
+
def get_width #from ruby progress_bar
|
105
|
+
default_width = 80
|
106
|
+
begin
|
107
|
+
tiocgwinsz = 0x5413
|
108
|
+
data = [0, 0, 0, 0].pack("SSSS")
|
109
|
+
if @out.ioctl(tiocgwinsz, data) >= 0 then
|
110
|
+
rows, cols, xpixels, ypixels = data.unpack("SSSS")
|
111
|
+
if cols >= 0 then cols else default_width end
|
112
|
+
else
|
113
|
+
default_width
|
114
|
+
end
|
115
|
+
rescue Exception
|
116
|
+
default_width
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def eol
|
121
|
+
@finished ? "\n" : "\r"
|
122
|
+
end
|
123
|
+
|
124
|
+
def show
|
125
|
+
line =@current_time
|
126
|
+
width = get_width
|
127
|
+
if line.to_s.length < width - 1
|
128
|
+
@out.print("#{line}#{eol}")
|
129
|
+
elsif line.length >= width
|
130
|
+
@out.print("#{line}\n")
|
131
|
+
end
|
132
|
+
@out.flush
|
133
|
+
end
|
89
134
|
end
|
data/time_left.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{time_left}
|
5
|
-
s.version = "0.1.
|
5
|
+
s.version = "0.1.2"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Artur Meisters"]
|
9
|
-
s.date = %q{2010-04-
|
9
|
+
s.date = %q{2010-04-29}
|
10
10
|
s.description = %q{Print out time that left doing some process}
|
11
11
|
s.email = %q{arturs@ithouse.lv}
|
12
12
|
s.extra_rdoc_files = ["README.rdoc", "lib/time_left.rb"]
|
@@ -15,7 +15,7 @@ Gem::Specification.new do |s|
|
|
15
15
|
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Time_left", "--main", "README.rdoc"]
|
16
16
|
s.require_paths = ["lib"]
|
17
17
|
s.rubyforge_project = %q{time_left}
|
18
|
-
s.rubygems_version = %q{1.3.
|
18
|
+
s.rubygems_version = %q{1.3.6}
|
19
19
|
s.summary = %q{Print out time that left doing some process}
|
20
20
|
|
21
21
|
if s.respond_to? :specification_version then
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 2
|
9
|
+
version: 0.1.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Artur Meisters
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-04-
|
17
|
+
date: 2010-04-29 00:00:00 +03:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|