ruby-progressbar 1.6.1 → 1.7.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.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +1 -1
  3. data/lib/ruby-progressbar.rb +8 -3
  4. data/lib/ruby-progressbar/base.rb +121 -177
  5. data/lib/ruby-progressbar/calculators/length.rb +75 -0
  6. data/lib/ruby-progressbar/calculators/length_spec.rb +9 -0
  7. data/lib/ruby-progressbar/calculators/running_average.rb +9 -0
  8. data/lib/ruby-progressbar/components.rb +3 -5
  9. data/lib/ruby-progressbar/components/bar.rb +79 -43
  10. data/lib/ruby-progressbar/components/percentage.rb +29 -0
  11. data/lib/ruby-progressbar/components/rate.rb +34 -62
  12. data/lib/ruby-progressbar/components/time.rb +103 -0
  13. data/lib/ruby-progressbar/components/title.rb +13 -0
  14. data/lib/ruby-progressbar/format.rb +2 -1
  15. data/lib/ruby-progressbar/format/formatter.rb +27 -0
  16. data/lib/ruby-progressbar/format/molecule.rb +55 -37
  17. data/lib/ruby-progressbar/format/string.rb +36 -0
  18. data/lib/ruby-progressbar/output.rb +61 -0
  19. data/lib/ruby-progressbar/outputs/non_tty.rb +47 -0
  20. data/lib/ruby-progressbar/outputs/tty.rb +32 -0
  21. data/lib/ruby-progressbar/progress.rb +110 -0
  22. data/lib/ruby-progressbar/throttle.rb +25 -0
  23. data/lib/ruby-progressbar/time.rb +20 -17
  24. data/lib/ruby-progressbar/timer.rb +72 -0
  25. data/lib/ruby-progressbar/version.rb +2 -2
  26. data/spec/fixtures/benchmark.rb +21 -4
  27. data/spec/{lib/ruby-progressbar → ruby-progressbar}/base_spec.rb +55 -62
  28. data/spec/ruby-progressbar/calculators/running_average_spec.rb +19 -0
  29. data/spec/ruby-progressbar/components/bar_spec.rb +234 -0
  30. data/spec/ruby-progressbar/components/percentage_spec.rb +9 -0
  31. data/spec/ruby-progressbar/components/rate_spec.rb +9 -0
  32. data/spec/ruby-progressbar/components/throttle_spec.rb +157 -0
  33. data/spec/ruby-progressbar/components/time_spec.rb +308 -0
  34. data/spec/ruby-progressbar/components/title_spec.rb +12 -0
  35. data/spec/ruby-progressbar/format/formatter_spec.rb +9 -0
  36. data/spec/ruby-progressbar/format/molecule_spec.rb +30 -0
  37. data/spec/ruby-progressbar/format/string_spec.rb +9 -0
  38. data/spec/ruby-progressbar/output_spec.rb +7 -0
  39. data/spec/ruby-progressbar/outputs/non_tty_spec.rb +9 -0
  40. data/spec/ruby-progressbar/outputs/tty_spec.rb +9 -0
  41. data/spec/ruby-progressbar/progress_spec.rb +150 -0
  42. data/spec/ruby-progressbar/time_spec.rb +37 -0
  43. data/spec/ruby-progressbar/timer_spec.rb +7 -0
  44. data/spec/spec_helper.rb +2 -2
  45. data/spec/support/time.rb +3 -1
  46. metadata +55 -35
  47. data/lib/ruby-progressbar/components/elapsed_timer.rb +0 -25
  48. data/lib/ruby-progressbar/components/estimated_timer.rb +0 -90
  49. data/lib/ruby-progressbar/components/progressable.rb +0 -112
  50. data/lib/ruby-progressbar/components/throttle.rb +0 -21
  51. data/lib/ruby-progressbar/components/timer.rb +0 -69
  52. data/lib/ruby-progressbar/format/base.rb +0 -55
  53. data/lib/ruby-progressbar/formatter.rb +0 -112
  54. data/lib/ruby-progressbar/length_calculator.rb +0 -64
  55. data/lib/ruby-progressbar/running_average_calculator.rb +0 -7
  56. data/spec/lib/ruby-progressbar/components/bar_spec.rb +0 -210
  57. data/spec/lib/ruby-progressbar/components/elapsed_timer_spec.rb +0 -91
  58. data/spec/lib/ruby-progressbar/components/estimated_timer_spec.rb +0 -241
  59. data/spec/lib/ruby-progressbar/components/progressable_spec.rb +0 -47
  60. data/spec/lib/ruby-progressbar/components/throttle_spec.rb +0 -100
  61. data/spec/lib/ruby-progressbar/format/molecule_spec.rb +0 -22
  62. data/spec/lib/ruby-progressbar/running_average_calculator_spec.rb +0 -11
  63. data/spec/lib/ruby-progressbar/time_spec.rb +0 -49
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8ab0a31d7bb98cd80041a9c7a30d02d06f5ff2c0
4
- data.tar.gz: cc94aa0ec99f5638339360ac366cf11e9bbc6b77
3
+ metadata.gz: 65d4119d92f6886b3336cb4a1f4aae7c0f41db32
4
+ data.tar.gz: 61cbb19a13d99c622042ae32cfb5e132658d25ff
5
5
  SHA512:
6
- metadata.gz: 4e2e0bbc3a033fcab712a3dc2b636018c3206e6bbd0ac5998d53a1719e195c3d8ee895dfb98c075b4f5fdd939cd862a26269cb0f01b3aa6b8f59cd1825a8eebc
7
- data.tar.gz: cffef174c3686916351cf8482f61602574a0eaf566fcf5f8d4045ba1fad127181766006bd9c8fdf884a83a832f835257ba462e57670f2ecb3595e707801546f9
6
+ metadata.gz: 8080a8cc6c2565958dc51ebbead22286f2ba38ebd3c2ab83d61f658989dceea0bea077d3894333790251450b197c9b86494a5801a6bcfd2d4f05d1344fa0d29f
7
+ data.tar.gz: e48ece20505cc7406467d4c1e3aa1d4761003eb27886b5a6459f4769c67869a8e24cf77163ba20b7a11256c18fdd97bedd6551ae86a2fb2b411c62c3f2b27d99
data/Rakefile CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env rake
2
- require "bundler/gem_tasks"
2
+ require 'bundler/gem_tasks'
@@ -1,6 +1,11 @@
1
- require 'ruby-progressbar/length_calculator'
2
- require 'ruby-progressbar/running_average_calculator'
3
- require 'ruby-progressbar/formatter'
1
+ require 'ruby-progressbar/output'
2
+ require 'ruby-progressbar/outputs/tty'
3
+ require 'ruby-progressbar/outputs/non_tty'
4
+ require 'ruby-progressbar/timer'
5
+ require 'ruby-progressbar/progress'
6
+ require 'ruby-progressbar/throttle'
7
+ require 'ruby-progressbar/calculators/length'
8
+ require 'ruby-progressbar/calculators/running_average'
4
9
  require 'ruby-progressbar/components'
5
10
  require 'ruby-progressbar/format'
6
11
  require 'ruby-progressbar/base'
@@ -1,217 +1,161 @@
1
- class ProgressBar
2
- class Base
3
- include ProgressBar::LengthCalculator
4
- include ProgressBar::Formatter
1
+ require 'forwardable'
5
2
 
6
- DEFAULT_OUTPUT_STREAM = $stdout
3
+ class ProgressBar
4
+ class Base
5
+ extend Forwardable
7
6
 
8
- def initialize(options = {})
9
- self.output = options[:output] || DEFAULT_OUTPUT_STREAM
10
- autostart = options.fetch(:autostart, true)
7
+ def_delegators :output,
8
+ :clear,
9
+ :log,
10
+ :refresh
11
11
 
12
- super(options)
12
+ def_delegators :progressable,
13
+ :progress,
14
+ :total
13
15
 
14
- @bar = Components::Bar.new(options)
15
- @rate = Components::Rate.new(options)
16
- @estimated_time = Components::EstimatedTimer.new(options)
17
- @elapsed_time = Components::ElapsedTimer.new
18
- @throttle = Components::Throttle.new(options)
16
+ # rubocop:disable Metrics/AbcSize
17
+ def initialize(options = {})
18
+ self.autostart = options.fetch(:autostart, true)
19
+ self.autofinish = options.fetch(:autofinish, true)
20
+ self.finished = false
19
21
 
20
- start :at => options[:starting_at] if autostart
21
- end
22
-
23
- ###
24
- # Starting The Bar
25
- #
26
- def start(options = {})
27
- clear
28
-
29
- with_update do
30
- with_progressables(:start, options)
31
- with_timers(:start)
32
- end
33
- end
34
-
35
- ###
36
- # Updating The Bar's Progress
37
- #
38
- def decrement
39
- update_progress(:decrement)
40
- end
41
-
42
- def increment
43
- update_progress(:increment)
44
- end
22
+ self.timer = Timer.new(options)
23
+ self.progressable = Progress.new(options)
45
24
 
46
- def progress=(new_progress)
47
- update_progress(:progress=, new_progress)
48
- end
49
-
50
- def total=(new_total)
51
- update_progress(:total=, new_total)
52
- end
53
-
54
- ###
55
- # Stopping The Bar
56
- #
57
- def finish
58
- with_update { with_progressables(:finish); with_timers(:stop) } unless finished?
59
- end
25
+ options = options.merge(:timer => timer,
26
+ :progress => progressable)
60
27
 
61
- def pause
62
- with_update { with_timers(:pause) } unless paused?
63
- end
28
+ self.title_comp = Components::Title.new(options)
29
+ self.bar = Components::Bar.new(options)
30
+ self.percentage = Components::Percentage.new(options)
31
+ self.rate = Components::Rate.new(options)
32
+ self.time = Components::Time.new(options)
64
33
 
65
- def stop
66
- with_update { with_timers(:stop) } unless stopped?
67
- end
34
+ self.output = Output.detect(options.merge(:bar => self))
35
+ @format = Format::String.new(output.resolve_format(options[:format]))
68
36
 
69
- def resume
70
- with_update { with_timers(:resume) } if stopped?
71
- end
72
-
73
- def reset
74
- with_update do
75
- @bar.reset
76
- with_timers(:reset)
77
- end
78
- end
37
+ start :at => options[:starting_at] if autostart
38
+ end
39
+ # rubocop:enable Metrics/AbcSize
79
40
 
80
- def stopped?
81
- (@estimated_time.stopped? && @elapsed_time.stopped?) || finished?
82
- end
41
+ def start(options = {})
42
+ clear
83
43
 
84
- alias :paused? :stopped?
44
+ timer.start
45
+ update_progress(:start, options)
46
+ end
85
47
 
86
- def finished?
87
- @estimated_time.finished? && @bar.finished?
88
- end
48
+ def finish
49
+ output.with_refresh do
50
+ self.finished = true
51
+ progressable.finish
52
+ timer.stop
53
+ end unless finished?
54
+ end
89
55
 
90
- def started?
91
- @estimated_time.started? && @elapsed_time.started? && @bar.started?
92
- end
56
+ def pause
57
+ output.with_refresh { timer.pause } unless paused?
58
+ end
93
59
 
94
- ###
95
- # UI Updates
96
- #
97
- def progress_mark=(mark)
98
- with_update { @bar.progress_mark = mark }
99
- end
60
+ def stop
61
+ output.with_refresh { timer.stop } unless stopped?
62
+ end
100
63
 
101
- def remainder_mark=(mark)
102
- with_update { @bar.remainder_mark = mark }
103
- end
64
+ def resume
65
+ output.with_refresh { timer.resume } if stopped?
66
+ end
104
67
 
105
- def title=(title)
106
- if output.tty?
107
- with_update { super }
108
- end
68
+ def reset
69
+ output.with_refresh do
70
+ self.finished = false
71
+ progressable.reset
72
+ timer.reset
109
73
  end
74
+ end
110
75
 
111
- ###
112
- # Output
113
- #
114
- def clear
115
- self.last_update_length = 0
116
-
117
- if output.tty?
118
- output.print clear_string
119
- output.print "\r"
120
- else
121
- output.print "\n"
122
- end
123
- end
76
+ def stopped?
77
+ timer.stopped? || finished?
78
+ end
124
79
 
125
- def refresh
126
- update
127
- end
80
+ alias_method :paused?, :stopped?
128
81
 
129
- def log(string)
130
- clear
131
- output.puts string
82
+ def finished?
83
+ finished || (autofinish && progressable.finished?)
84
+ end
132
85
 
133
- update(:force => true) unless stopped?
134
- end
86
+ def started?
87
+ timer.started?
88
+ end
135
89
 
136
- def to_s(format_string = nil)
137
- format_string ||= @format_string
90
+ def decrement
91
+ update_progress(:decrement)
92
+ end
138
93
 
139
- format(format_string)
140
- end
94
+ def increment
95
+ update_progress(:increment)
96
+ end
141
97
 
142
- def inspect
143
- "#<ProgressBar:#{progress}/#{total || 'unknown'}>"
144
- end
98
+ def progress=(new_progress)
99
+ update_progress(:progress=, new_progress)
100
+ end
145
101
 
146
- protected
102
+ def total=(new_total)
103
+ update_progress(:total=, new_total)
104
+ end
147
105
 
148
- attr_accessor :output,
149
- :last_update_length
106
+ def progress_mark=(mark)
107
+ output.refresh_with_format_change { bar.progress_mark = mark }
108
+ end
150
109
 
151
- private
110
+ def remainder_mark=(mark)
111
+ output.refresh_with_format_change { bar.remainder_mark = mark }
112
+ end
152
113
 
153
- def clear_string
154
- "#{" " * length}"
155
- end
114
+ def title
115
+ title_comp.title
116
+ end
156
117
 
157
- def last_update_length
158
- @last_update_length ||= 0
159
- end
118
+ def title=(title)
119
+ output.refresh_with_format_change { title_comp.title = title }
120
+ end
160
121
 
161
- def with_progressables(*args)
162
- @bar.send(*args)
163
- @estimated_time.send(*args)
164
- @rate.send(*args)
165
- end
122
+ def to_s(new_format = nil)
123
+ self.format = new_format if new_format
166
124
 
167
- def with_timers(*args)
168
- @estimated_time.send(*args)
169
- @elapsed_time.send(*args)
170
- @rate.send(*args)
171
- end
125
+ Format::Formatter.process(@format, output.length, self)
126
+ end
172
127
 
173
- def update_progress(*args)
174
- with_update do
175
- with_progressables(*args)
176
- with_timers(:stop) if finished?
177
- end
178
- end
128
+ def inspect
129
+ "#<ProgressBar:#{progress}/#{total || 'unknown'}>"
130
+ end
179
131
 
180
- def with_update
181
- yield
182
- update
132
+ def format=(other)
133
+ output.refresh_with_format_change do
134
+ @format = Format::String.new(other || output.default_format)
183
135
  end
136
+ end
184
137
 
185
- def update(options = {})
186
- if length_changed?
187
- clear
188
- reset_length
189
- end
190
-
191
- @throttle.choke( stopped? || options[:force] ) do
192
- if output.tty?
193
- formatted_string = self.to_s
194
- output_string = formatted_string
195
- else
196
- formatted_string = self.to_s(DEFAULT_NON_TTY_FORMAT_STRING)
197
- formatted_string = formatted_string[0...-1] unless finished?
198
-
199
- output_string = formatted_string[last_update_length..-1]
200
- end
201
-
202
- self.last_update_length = formatted_string.length
138
+ alias_method :format, :format=
203
139
 
204
- output.print output_string + eol
205
- output.flush
206
- end
207
- end
140
+ protected
208
141
 
209
- def eol
210
- if output.tty?
211
- stopped? ? "\n" : "\r"
212
- else
213
- stopped? ? "\n" : ""
214
- end
142
+ attr_accessor :output,
143
+ :timer,
144
+ :progressable,
145
+ :title_comp,
146
+ :bar,
147
+ :percentage,
148
+ :rate,
149
+ :time,
150
+ :autostart,
151
+ :autofinish,
152
+ :finished
153
+
154
+ def update_progress(*args)
155
+ output.with_refresh do
156
+ progressable.send(*args)
157
+ timer.stop if finished?
215
158
  end
216
159
  end
217
160
  end
161
+ end
@@ -0,0 +1,75 @@
1
+ class ProgressBar
2
+ module Calculators
3
+ class Length
4
+ attr_accessor :length_override,
5
+ :current_length
6
+
7
+ def initialize(options)
8
+ self.length_override = options[:length]
9
+ self.current_length = nil
10
+ end
11
+
12
+ def length
13
+ current_length || reset_length
14
+ end
15
+
16
+ def length_changed?
17
+ previous_length = current_length
18
+ self.current_length = calculate_length
19
+
20
+ previous_length != current_length
21
+ end
22
+
23
+ def calculate_length
24
+ length_override || terminal_width || 80
25
+ end
26
+
27
+ def reset_length
28
+ self.current_length = calculate_length
29
+ end
30
+
31
+ def length_override=(other)
32
+ @length_override ||= ENV['RUBY_PROGRESS_BAR_LENGTH'] || other
33
+ @length_override = @length_override.to_i if @length_override
34
+ end
35
+
36
+ private
37
+
38
+ # This code was copied and modified from Rake, available under MIT-LICENSE
39
+ # Copyright (c) 2003, 2004 Jim Weirich
40
+ def terminal_width
41
+ return 80 unless unix?
42
+
43
+ result = dynamic_width
44
+ (result < 20) ? 80 : result
45
+ rescue
46
+ 80
47
+ end
48
+
49
+ begin
50
+ require 'io/console'
51
+
52
+ def dynamic_width
53
+ _rows, columns = IO.console.winsize
54
+ columns
55
+ end
56
+ rescue LoadError
57
+ def dynamic_width
58
+ dynamic_width_stty.nonzero? || dynamic_width_tput
59
+ end
60
+
61
+ def dynamic_width_stty
62
+ `stty size 2>/dev/null`.split[1].to_i
63
+ end
64
+
65
+ def dynamic_width_tput
66
+ `tput cols 2>/dev/null`.to_i
67
+ end
68
+ end
69
+
70
+ def unix?
71
+ RUBY_PLATFORM =~ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/i
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,9 @@
1
+ require 'rspectacular'
2
+ require 'ruby-progressbar/calculators/length'
3
+
4
+ class ProgressBar
5
+ module Calculators
6
+ describe Length do
7
+ end
8
+ end
9
+ end