progress 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -26,3 +26,10 @@ begin
26
26
  rescue LoadError
27
27
  puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
28
28
  end
29
+
30
+ require 'spec/rake/spectask'
31
+ Spec::Rake::SpecTask.new(:spec) do |spec|
32
+ spec.libs << 'lib' << 'spec'
33
+ spec.spec_files = FileList['spec/**/*_spec.rb']
34
+ end
35
+ task :default => :spec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.0
data/lib/progress.rb CHANGED
@@ -64,7 +64,7 @@ class Progress
64
64
  # Progress.highlight = true
65
65
  def start(title, total = 1)
66
66
  levels << new(title, total)
67
- print_message
67
+ print_message(true)
68
68
  if block_given?
69
69
  begin
70
70
  yield
@@ -97,13 +97,13 @@ class Progress
97
97
 
98
98
  def stop
99
99
  if levels.last
100
- print_message if levels.last.step_if_blank
100
+ print_message(true) if levels.last.step_if_blank
101
101
  levels.pop
102
102
  io.puts if levels.empty?
103
103
  end
104
104
  end
105
105
 
106
- attr_writer :io, :lines, :highlight # :nodoc:
106
+ attr_writer :lines, :highlight # :nodoc:
107
107
 
108
108
  private
109
109
 
@@ -112,8 +112,10 @@ class Progress
112
112
  end
113
113
 
114
114
  def io
115
- @io ||= $stderr
116
- @io.sync = true
115
+ unless @io
116
+ @io = $stderr
117
+ @io.sync = true
118
+ end
117
119
  @io
118
120
  end
119
121
 
@@ -129,29 +131,45 @@ class Progress
129
131
  @highlight.nil? ? io_tty? : @highlight
130
132
  end
131
133
 
132
- def print_message
133
- messages = []
134
- inner = 0
135
- levels.reverse.each do |l|
136
- current = l.to_f(inner)
137
- value = current == 0 ? '......' : '%5.1f%%' % (current * 100.0)
138
- messages << "#{l.title}: #{!highlight? || value == '100.0%' ? value : "\e[1m#{value}\e[0m"}"
139
- inner = current
140
- end
141
- message = messages.reverse * ' > '
142
-
143
- unless lines?
144
- previous_length = @previous_length || 0
145
- message_cl = if highlight?
146
- message.gsub(/\033\[(0|1)m/, '')
134
+ def time_to_print?
135
+ if @previous
136
+ if @previous < Time.now - 0.3
137
+ @previous = Time.now
138
+ true
147
139
  else
148
- message
140
+ false
149
141
  end
150
- @previous_length = message_cl.length
151
- message = "#{message}#{' ' * [previous_length - message_cl.length, 0].max}\r"
142
+ else
143
+ @previous = Time.now
144
+ true
152
145
  end
146
+ end
153
147
 
154
- lines? ? io.puts(message) : io.print(message)
148
+ def print_message(force = false)
149
+ if force || time_to_print?
150
+ messages = []
151
+ inner = 0
152
+ levels.reverse.each do |l|
153
+ current = l.to_f(inner)
154
+ value = current == 0 ? '......' : '%5.1f%%' % (current * 100.0)
155
+ messages << "#{l.title}: #{!highlight? || value == '100.0%' ? value : "\e[1m#{value}\e[0m"}"
156
+ inner = current
157
+ end
158
+ message = messages.reverse * ' > '
159
+
160
+ unless lines?
161
+ previous_length = @previous_length || 0
162
+ message_cl = if highlight?
163
+ message.gsub(/\033\[(0|1)m/, '')
164
+ else
165
+ message
166
+ end
167
+ @previous_length = message_cl.length
168
+ message = "#{message}#{' ' * [previous_length - message_cl.length, 0].max}\r"
169
+ end
170
+
171
+ lines? ? io.puts(message) : io.print(message)
172
+ end
155
173
  end
156
174
  end
157
175
  end
data/progress.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{progress}
8
- s.version = "0.1.2"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Boba Fat"]
@@ -3,7 +3,8 @@ require File.dirname(__FILE__) + '/spec_helper.rb'
3
3
  describe Progress do
4
4
  before :each do
5
5
  @io = StringIO.new
6
- Progress.io = @io
6
+ Progress.stub!(:io).and_return(@io)
7
+ Progress.stub!(:time_to_print?).and_return(true)
7
8
  end
8
9
 
9
10
  def io_pop
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: progress
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Boba Fat