ruby-progressbar 1.6.1 → 1.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +1 -1
- data/lib/ruby-progressbar.rb +8 -3
- data/lib/ruby-progressbar/base.rb +121 -177
- data/lib/ruby-progressbar/calculators/length.rb +75 -0
- data/lib/ruby-progressbar/calculators/length_spec.rb +9 -0
- data/lib/ruby-progressbar/calculators/running_average.rb +9 -0
- data/lib/ruby-progressbar/components.rb +3 -5
- data/lib/ruby-progressbar/components/bar.rb +79 -43
- data/lib/ruby-progressbar/components/percentage.rb +29 -0
- data/lib/ruby-progressbar/components/rate.rb +34 -62
- data/lib/ruby-progressbar/components/time.rb +103 -0
- data/lib/ruby-progressbar/components/title.rb +13 -0
- data/lib/ruby-progressbar/format.rb +2 -1
- data/lib/ruby-progressbar/format/formatter.rb +27 -0
- data/lib/ruby-progressbar/format/molecule.rb +55 -37
- data/lib/ruby-progressbar/format/string.rb +36 -0
- data/lib/ruby-progressbar/output.rb +61 -0
- data/lib/ruby-progressbar/outputs/non_tty.rb +47 -0
- data/lib/ruby-progressbar/outputs/tty.rb +32 -0
- data/lib/ruby-progressbar/progress.rb +110 -0
- data/lib/ruby-progressbar/throttle.rb +25 -0
- data/lib/ruby-progressbar/time.rb +20 -17
- data/lib/ruby-progressbar/timer.rb +72 -0
- data/lib/ruby-progressbar/version.rb +2 -2
- data/spec/fixtures/benchmark.rb +21 -4
- data/spec/{lib/ruby-progressbar → ruby-progressbar}/base_spec.rb +55 -62
- data/spec/ruby-progressbar/calculators/running_average_spec.rb +19 -0
- data/spec/ruby-progressbar/components/bar_spec.rb +234 -0
- data/spec/ruby-progressbar/components/percentage_spec.rb +9 -0
- data/spec/ruby-progressbar/components/rate_spec.rb +9 -0
- data/spec/ruby-progressbar/components/throttle_spec.rb +157 -0
- data/spec/ruby-progressbar/components/time_spec.rb +308 -0
- data/spec/ruby-progressbar/components/title_spec.rb +12 -0
- data/spec/ruby-progressbar/format/formatter_spec.rb +9 -0
- data/spec/ruby-progressbar/format/molecule_spec.rb +30 -0
- data/spec/ruby-progressbar/format/string_spec.rb +9 -0
- data/spec/ruby-progressbar/output_spec.rb +7 -0
- data/spec/ruby-progressbar/outputs/non_tty_spec.rb +9 -0
- data/spec/ruby-progressbar/outputs/tty_spec.rb +9 -0
- data/spec/ruby-progressbar/progress_spec.rb +150 -0
- data/spec/ruby-progressbar/time_spec.rb +37 -0
- data/spec/ruby-progressbar/timer_spec.rb +7 -0
- data/spec/spec_helper.rb +2 -2
- data/spec/support/time.rb +3 -1
- metadata +55 -35
- data/lib/ruby-progressbar/components/elapsed_timer.rb +0 -25
- data/lib/ruby-progressbar/components/estimated_timer.rb +0 -90
- data/lib/ruby-progressbar/components/progressable.rb +0 -112
- data/lib/ruby-progressbar/components/throttle.rb +0 -21
- data/lib/ruby-progressbar/components/timer.rb +0 -69
- data/lib/ruby-progressbar/format/base.rb +0 -55
- data/lib/ruby-progressbar/formatter.rb +0 -112
- data/lib/ruby-progressbar/length_calculator.rb +0 -64
- data/lib/ruby-progressbar/running_average_calculator.rb +0 -7
- data/spec/lib/ruby-progressbar/components/bar_spec.rb +0 -210
- data/spec/lib/ruby-progressbar/components/elapsed_timer_spec.rb +0 -91
- data/spec/lib/ruby-progressbar/components/estimated_timer_spec.rb +0 -241
- data/spec/lib/ruby-progressbar/components/progressable_spec.rb +0 -47
- data/spec/lib/ruby-progressbar/components/throttle_spec.rb +0 -100
- data/spec/lib/ruby-progressbar/format/molecule_spec.rb +0 -22
- data/spec/lib/ruby-progressbar/running_average_calculator_spec.rb +0 -11
- 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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65d4119d92f6886b3336cb4a1f4aae7c0f41db32
|
4
|
+
data.tar.gz: 61cbb19a13d99c622042ae32cfb5e132658d25ff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8080a8cc6c2565958dc51ebbead22286f2ba38ebd3c2ab83d61f658989dceea0bea077d3894333790251450b197c9b86494a5801a6bcfd2d4f05d1344fa0d29f
|
7
|
+
data.tar.gz: e48ece20505cc7406467d4c1e3aa1d4761003eb27886b5a6459f4769c67869a8e24cf77163ba20b7a11256c18fdd97bedd6551ae86a2fb2b411c62c3f2b27d99
|
data/Rakefile
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
#!/usr/bin/env rake
|
2
|
-
require
|
2
|
+
require 'bundler/gem_tasks'
|
data/lib/ruby-progressbar.rb
CHANGED
@@ -1,6 +1,11 @@
|
|
1
|
-
require 'ruby-progressbar/
|
2
|
-
require 'ruby-progressbar/
|
3
|
-
require 'ruby-progressbar/
|
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
|
-
|
2
|
-
class Base
|
3
|
-
include ProgressBar::LengthCalculator
|
4
|
-
include ProgressBar::Formatter
|
1
|
+
require 'forwardable'
|
5
2
|
|
6
|
-
|
3
|
+
class ProgressBar
|
4
|
+
class Base
|
5
|
+
extend Forwardable
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
def_delegators :output,
|
8
|
+
:clear,
|
9
|
+
:log,
|
10
|
+
:refresh
|
11
11
|
|
12
|
-
|
12
|
+
def_delegators :progressable,
|
13
|
+
:progress,
|
14
|
+
:total
|
13
15
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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
|
-
|
21
|
-
|
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
|
-
|
47
|
-
|
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
|
-
|
62
|
-
|
63
|
-
|
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
|
-
|
66
|
-
|
67
|
-
end
|
34
|
+
self.output = Output.detect(options.merge(:bar => self))
|
35
|
+
@format = Format::String.new(output.resolve_format(options[:format]))
|
68
36
|
|
69
|
-
|
70
|
-
|
71
|
-
|
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
|
-
|
81
|
-
|
82
|
-
end
|
41
|
+
def start(options = {})
|
42
|
+
clear
|
83
43
|
|
84
|
-
|
44
|
+
timer.start
|
45
|
+
update_progress(:start, options)
|
46
|
+
end
|
85
47
|
|
86
|
-
|
87
|
-
|
88
|
-
|
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
|
-
|
91
|
-
|
92
|
-
|
56
|
+
def pause
|
57
|
+
output.with_refresh { timer.pause } unless paused?
|
58
|
+
end
|
93
59
|
|
94
|
-
|
95
|
-
|
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
|
-
|
102
|
-
|
103
|
-
|
64
|
+
def resume
|
65
|
+
output.with_refresh { timer.resume } if stopped?
|
66
|
+
end
|
104
67
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
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
|
-
|
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
|
-
|
126
|
-
update
|
127
|
-
end
|
80
|
+
alias_method :paused?, :stopped?
|
128
81
|
|
129
|
-
|
130
|
-
|
131
|
-
|
82
|
+
def finished?
|
83
|
+
finished || (autofinish && progressable.finished?)
|
84
|
+
end
|
132
85
|
|
133
|
-
|
134
|
-
|
86
|
+
def started?
|
87
|
+
timer.started?
|
88
|
+
end
|
135
89
|
|
136
|
-
|
137
|
-
|
90
|
+
def decrement
|
91
|
+
update_progress(:decrement)
|
92
|
+
end
|
138
93
|
|
139
|
-
|
140
|
-
|
94
|
+
def increment
|
95
|
+
update_progress(:increment)
|
96
|
+
end
|
141
97
|
|
142
|
-
|
143
|
-
|
144
|
-
|
98
|
+
def progress=(new_progress)
|
99
|
+
update_progress(:progress=, new_progress)
|
100
|
+
end
|
145
101
|
|
146
|
-
|
102
|
+
def total=(new_total)
|
103
|
+
update_progress(:total=, new_total)
|
104
|
+
end
|
147
105
|
|
148
|
-
|
149
|
-
|
106
|
+
def progress_mark=(mark)
|
107
|
+
output.refresh_with_format_change { bar.progress_mark = mark }
|
108
|
+
end
|
150
109
|
|
151
|
-
|
110
|
+
def remainder_mark=(mark)
|
111
|
+
output.refresh_with_format_change { bar.remainder_mark = mark }
|
112
|
+
end
|
152
113
|
|
153
|
-
|
154
|
-
|
155
|
-
|
114
|
+
def title
|
115
|
+
title_comp.title
|
116
|
+
end
|
156
117
|
|
157
|
-
|
158
|
-
|
159
|
-
|
118
|
+
def title=(title)
|
119
|
+
output.refresh_with_format_change { title_comp.title = title }
|
120
|
+
end
|
160
121
|
|
161
|
-
|
162
|
-
|
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
|
-
|
168
|
-
|
169
|
-
@elapsed_time.send(*args)
|
170
|
-
@rate.send(*args)
|
171
|
-
end
|
125
|
+
Format::Formatter.process(@format, output.length, self)
|
126
|
+
end
|
172
127
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
with_timers(:stop) if finished?
|
177
|
-
end
|
178
|
-
end
|
128
|
+
def inspect
|
129
|
+
"#<ProgressBar:#{progress}/#{total || 'unknown'}>"
|
130
|
+
end
|
179
131
|
|
180
|
-
|
181
|
-
|
182
|
-
|
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
|
-
|
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
|
-
|
205
|
-
output.flush
|
206
|
-
end
|
207
|
-
end
|
140
|
+
protected
|
208
141
|
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
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
|