Class: Terminal::ProgressBar
- Inherits:
-
Object
- Object
- Terminal::ProgressBar
show all
- Defined in:
- lib/terminal/progressbar.rb,
lib/terminal/progressbar/version.rb,
lib/terminal/progressbar/singleton_class.rb
Defined Under Namespace
Classes: Error, InvalidPointingError
Constant Summary
collapse
- CR =
"\r"
- EOL =
"\n"
- STOP =
'|'
- SPACE =
' '
- DECORATION_LENGTH =
"100% #{STOP}#{STOP}".length
- OptArg =
OptionalArgument.define {
opt(:body_char, must: true,
condition: ->v { v.length == 1 },
adjuster: ->v { v.to_str.dup.freeze },
aliases: [:mark])
opt(:max_count, default: 100,
condition: AND(Integer, ->v { v >= 1 }),
adjuster: ->v { v.to_int })
opt(:max_width, default: $stderr.winsize.last,
condition: AND(Integer, ->v { v >= 1 }),
adjuster: ->v { v.to_int })
opt(:output, default: $stderr,
condition: AND(CAN(:print), CAN(:flush)))
}
- VERSION =
'0.2.0'
Instance Attribute Summary collapse
Useful wrapper for constructors
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(**options) ⇒ ProgressBar
Returns a new instance of ProgressBar.
46
47
48
49
50
51
52
53
54
|
# File 'lib/terminal/progressbar.rb', line 46
def initialize(**options)
opts = OptArg.parse(options)
@body_char = opts.body_char
@max_count = opts.max_count
@max_width = opts.max_width
@output = opts.output
@pointer = 0
end
|
Instance Attribute Details
#max_count ⇒ Object
Returns the value of attribute max_count.
22
23
24
|
# File 'lib/terminal/progressbar.rb', line 22
def max_count
@max_count
end
|
#max_width ⇒ Object
Returns the value of attribute max_width.
22
23
24
|
# File 'lib/terminal/progressbar.rb', line 22
def max_width
@max_width
end
|
#output ⇒ Object
Returns the value of attribute output.
22
23
24
|
# File 'lib/terminal/progressbar.rb', line 22
def output
@output
end
|
#pointer ⇒ Object
Also known as:
current_count
Returns the value of attribute pointer.
22
23
24
|
# File 'lib/terminal/progressbar.rb', line 22
def pointer
@pointer
end
|
Class Method Details
.auto(interval_sec, **options) {|instance| ... } ⇒ void
This method returns an undefined value.
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/terminal/progressbar/singleton_class.rb', line 29
def auto(interval_sec, **options)
interval_sec = Float(interval_sec)
printing_thread = nil
run(options) do |instance|
printing_thread = Thread.new do
loop do
if instance.finished?
break
else
instance.flush
sleep(interval_sec)
end
end
end
yield instance
end
ensure
printing_thread.join if printing_thread
nil
end
|
.run(**options) {|instance| ... } ⇒ void
This method returns an undefined value.
14
15
16
17
18
19
20
21
|
# File 'lib/terminal/progressbar/singleton_class.rb', line 14
def run(**options)
instance = new(options)
instance.flush
yield instance
ensure
instance.finish!
nil
end
|
Instance Method Details
#bar ⇒ String
77
78
79
|
# File 'lib/terminal/progressbar.rb', line 77
def bar
"#{@body_char * current_bar_width}#{bar_padding}"
end
|
#bar_padding ⇒ String
159
160
161
|
# File 'lib/terminal/progressbar.rb', line 159
def bar_padding
SPACE * (max_bar_width - current_bar_width)
end
|
#body_char ⇒ String
57
58
59
|
# File 'lib/terminal/progressbar.rb', line 57
def body_char
@body_char.dup
end
|
#current_bar_width ⇒ Integer
67
68
69
|
# File 'lib/terminal/progressbar.rb', line 67
def current_bar_width
(percentage == 0) ? 0 : (max_bar_width * rational).to_int
end
|
#decrement(step = 1) ⇒ step
128
129
130
131
|
# File 'lib/terminal/progressbar.rb', line 128
def decrement(step=1)
increment(-step)
step
end
|
#fast_forward ⇒ void
Also known as:
finish
This method returns an undefined value.
140
141
142
143
|
# File 'lib/terminal/progressbar.rb', line 140
def fast_forward
@pointer = @max_count
nil
end
|
#finished? ⇒ Boolean
Also known as:
end?
99
100
101
|
# File 'lib/terminal/progressbar.rb', line 99
def finished?
@pointer == @max_count
end
|
#flush ⇒ void
This method returns an undefined value.
87
88
89
90
91
|
# File 'lib/terminal/progressbar.rb', line 87
def flush
@output.print(line)
@output.print(finished? ? EOL : CR)
@output.flush
end
|
#increment(step = 1) ⇒ step
118
119
120
121
122
123
124
|
# File 'lib/terminal/progressbar.rb', line 118
def increment(step=1)
new_pointer = @pointer + step.to_int
raise InvalidPointingError unless pointable?(new_pointer)
@pointer = new_pointer
step
end
|
#line ⇒ String
82
83
84
|
# File 'lib/terminal/progressbar.rb', line 82
def line
"#{percentage.to_s.rjust(3)}% #{STOP}#{bar}#{STOP}"
end
|
#max_bar_width ⇒ Integer
62
63
64
|
# File 'lib/terminal/progressbar.rb', line 62
def max_bar_width
max_width - DECORATION_LENGTH
end
|
#percentage ⇒ Fixnum
72
73
74
|
# File 'lib/terminal/progressbar.rb', line 72
def percentage
((rational * (100 / @max_count)) * 100).to_int
end
|
#pointable?(point) ⇒ Boolean
94
95
96
97
|
# File 'lib/terminal/progressbar.rb', line 94
def pointable?(point)
int = point.to_int
(int >= 0) && (int <= @max_count)
end
|
#rational ⇒ Rational
Returns pointer / max_count.
164
165
166
|
# File 'lib/terminal/progressbar.rb', line 164
def rational
Rational(@pointer, @max_count)
end
|
#rewind ⇒ void
This method returns an undefined value.
134
135
136
137
|
# File 'lib/terminal/progressbar.rb', line 134
def rewind
@pointer = 0
nil
end
|