tty-progressbar 0.3.0 → 0.4.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +28 -7
- data/lib/tty/progressbar.rb +25 -1
- data/lib/tty/progressbar/formatter.rb +1 -0
- data/lib/tty/progressbar/formatter/current.rb +1 -1
- data/lib/tty/progressbar/formatter/{byte.rb → current_byte.rb} +1 -1
- data/lib/tty/progressbar/formatter/total.rb +1 -1
- data/lib/tty/progressbar/formatter/total_byte.rb +33 -0
- data/lib/tty/progressbar/version.rb +1 -1
- data/spec/unit/formatter/{byte_spec.rb → current_byte_spec.rb} +2 -2
- data/spec/unit/formatter/estimated_spec.rb +1 -1
- data/spec/unit/formatter/total_byte_spec.rb +20 -0
- data/spec/unit/formatter/total_spec.rb +20 -0
- data/spec/unit/ratio_spec.rb +23 -0
- data/spec/unit/set_current_spec.rb +47 -0
- metadata +14 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff767a06ae19986026a9fc6bf8542246a66aa114
|
4
|
+
data.tar.gz: ee1b4fdfcf5e032a0f57b45edfc6076b7e44478f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e30d01509fe132b2af4ae89138723a6e8f3fd55a9cd212b83d7a4bcac299d1ca46bb28051d039c75eaf57f30217bd518e4e2727629a5c954896811c4f199a97
|
7
|
+
data.tar.gz: 023f9de9dc08949e1754ffc1caab86d5ad2d06c9f538577049a658e243a5680ae9fbd04a31a07c64e56d9d7cff8cad1d687a07d18ee2576d75342725901e4963
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
0.4.0 (December 25, 2014)
|
2
|
+
|
3
|
+
* Add :total_byte, :current_byte formatters by @vincentjames501
|
4
|
+
* Add current= method for updating progress to a given value by @vincentjames501
|
5
|
+
* Add ratio= method for updating progress ratio
|
6
|
+
|
1
7
|
0.3.0 (December 21, 2014)
|
2
8
|
|
3
9
|
* Catch INT signal and cleanly end progress
|
data/README.md
CHANGED
@@ -40,9 +40,11 @@ Or install it yourself as:
|
|
40
40
|
|
41
41
|
* [1. Usage](#1-usage)
|
42
42
|
* [1.1 advance](#11-advance)
|
43
|
-
* [1.2
|
44
|
-
* [1.3
|
45
|
-
* [1.4
|
43
|
+
* [1.2 current=](#12-current)
|
44
|
+
* [1.3 ratio=](#13-ratio)
|
45
|
+
* [1.4 finish](#14-finish)
|
46
|
+
* [1.5 complete?](#15-complete)
|
47
|
+
* [1.6 resize](#16-resize)
|
46
48
|
* [2. Configuration](#2-configuration)
|
47
49
|
* [2.1 Frequency](#21-frequency)
|
48
50
|
* [3. Formatting](#3-formatting)
|
@@ -85,7 +87,25 @@ bar.advance(-1)
|
|
85
87
|
|
86
88
|
Note: If a progress bar has already finished then negative steps will not set it back to desired value.
|
87
89
|
|
88
|
-
### 1.2
|
90
|
+
### 1.2 current=
|
91
|
+
|
92
|
+
**TTY::ProgressBar** allows you to set progress to a given value by calling `current=` method.
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
bar.current = 50
|
96
|
+
```
|
97
|
+
|
98
|
+
Note: If a progress bar has already finished then negative steps will not set it back to desired value.
|
99
|
+
|
100
|
+
### 1.3 ratio=
|
101
|
+
|
102
|
+
In order to update overall completion of a progress bar as an exact percentage use the `ratio=` method. The method accepts values between `0` and `1` inclusive. For example, a ratio of 0.5 will attempt to set the progress bar halfway:
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
bar.ratio = 0.5
|
106
|
+
```
|
107
|
+
|
108
|
+
### 1.4 finish
|
89
109
|
|
90
110
|
In order to immediately stop and finish the progress call `finish`. This will finish drawing the progress and return to new line.
|
91
111
|
|
@@ -93,7 +113,7 @@ In order to immediately stop and finish the progress call `finish`. This will fi
|
|
93
113
|
bar.finish
|
94
114
|
```
|
95
115
|
|
96
|
-
### 1.
|
116
|
+
### 1.5 complete?
|
97
117
|
|
98
118
|
During progresion you can check if bar is finished or not by calling `complete?`.
|
99
119
|
|
@@ -101,7 +121,7 @@ During progresion you can check if bar is finished or not by calling `complete?`
|
|
101
121
|
bar.complete? # => false
|
102
122
|
```
|
103
123
|
|
104
|
-
### 1.
|
124
|
+
### 1.6 resize
|
105
125
|
|
106
126
|
If you wish for a progress bar to change it's current width, you can use `resize` by passing in a new desired length:
|
107
127
|
|
@@ -155,9 +175,10 @@ Every **TTY::ProgressBar** instance requires a format string, which apart from r
|
|
155
175
|
These are the tokens that are currently supported:
|
156
176
|
|
157
177
|
* `:bar` the progress bar
|
158
|
-
* `:byte` the current progress in bytes
|
159
178
|
* `:current` the current progress number
|
179
|
+
* `:current_byte` the current progress in bytes
|
160
180
|
* `:total` the total progress number
|
181
|
+
* `:total_byte` the total progress in bytes
|
161
182
|
* `:percent` the completion percentage
|
162
183
|
* `:elapsed` the elapsed time in seconds
|
163
184
|
* `:eta` the esitmated time to completion in seconds
|
data/lib/tty/progressbar.rb
CHANGED
@@ -11,12 +11,13 @@ require 'tty/progressbar/pipeline'
|
|
11
11
|
require 'tty/progressbar/formatter'
|
12
12
|
|
13
13
|
require 'tty/progressbar/formatter/bar'
|
14
|
-
require 'tty/progressbar/formatter/byte'
|
15
14
|
require 'tty/progressbar/formatter/current'
|
15
|
+
require 'tty/progressbar/formatter/current_byte'
|
16
16
|
require 'tty/progressbar/formatter/elapsed'
|
17
17
|
require 'tty/progressbar/formatter/estimated'
|
18
18
|
require 'tty/progressbar/formatter/percent'
|
19
19
|
require 'tty/progressbar/formatter/total'
|
20
|
+
require 'tty/progressbar/formatter/total_byte'
|
20
21
|
|
21
22
|
module TTY
|
22
23
|
# Used for creating terminal progress bar
|
@@ -109,6 +110,29 @@ module TTY
|
|
109
110
|
render
|
110
111
|
end
|
111
112
|
|
113
|
+
# Advance the progress bar to the updated value
|
114
|
+
#
|
115
|
+
# @param [Number] value
|
116
|
+
# the desired value to updated to
|
117
|
+
#
|
118
|
+
# @api public
|
119
|
+
def current=(value)
|
120
|
+
value = [0, [value, total].min].max
|
121
|
+
advance(value - @current)
|
122
|
+
end
|
123
|
+
|
124
|
+
# Advance the progress bar to an exact ratio.
|
125
|
+
# The target value is set to the closest available value.
|
126
|
+
#
|
127
|
+
# @param [Float] value
|
128
|
+
# the ratio between 0 and 1 inclusive
|
129
|
+
#
|
130
|
+
# @api public
|
131
|
+
def ratio=(value)
|
132
|
+
target = (value * total).floor
|
133
|
+
advance(target - @current)
|
134
|
+
end
|
135
|
+
|
112
136
|
# Ratio of completed over total steps
|
113
137
|
#
|
114
138
|
# @return [Float]
|
@@ -18,6 +18,7 @@ module TTY
|
|
18
18
|
def load
|
19
19
|
@pipeline.use TTY::ProgressBar::CurrentFormatter
|
20
20
|
@pipeline.use TTY::ProgressBar::TotalFormatter
|
21
|
+
@pipeline.use TTY::ProgressBar::TotalByteFormatter
|
21
22
|
@pipeline.use TTY::ProgressBar::ElapsedFormatter
|
22
23
|
@pipeline.use TTY::ProgressBar::EstimatedFormatter
|
23
24
|
@pipeline.use TTY::ProgressBar::PercentFormatter
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
module TTY
|
4
|
+
class ProgressBar
|
5
|
+
# Used by {Pipeline} to format :total_byte token
|
6
|
+
#
|
7
|
+
# @api private
|
8
|
+
class TotalByteFormatter
|
9
|
+
MATCHER = /:total_byte/.freeze
|
10
|
+
|
11
|
+
def initialize(progress, *args, &block)
|
12
|
+
@progress = progress
|
13
|
+
@converter = Converter.new
|
14
|
+
end
|
15
|
+
|
16
|
+
# Determines whether this formatter is applied or not.
|
17
|
+
#
|
18
|
+
# @param [Object] value
|
19
|
+
#
|
20
|
+
# @return [Boolean]
|
21
|
+
#
|
22
|
+
# @api private
|
23
|
+
def matches?(value)
|
24
|
+
!!(value.to_s =~ MATCHER)
|
25
|
+
end
|
26
|
+
|
27
|
+
def format(value)
|
28
|
+
bytes = @converter.to_bytes(@progress.total)
|
29
|
+
value.gsub(MATCHER, bytes)
|
30
|
+
end
|
31
|
+
end # TotalByteFormatter
|
32
|
+
end # ProgressBar
|
33
|
+
end # TTY
|
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
RSpec.describe TTY::ProgressBar, '
|
5
|
+
RSpec.describe TTY::ProgressBar, 'current_byte' do
|
6
6
|
let(:output) { StringIO.new('', 'w+') }
|
7
7
|
|
8
8
|
it "displays bytes processed" do
|
9
|
-
progress = described_class.new(":
|
9
|
+
progress = described_class.new(":current_byte", output: output, total: 102_400)
|
10
10
|
5.times { progress.advance(20_480) }
|
11
11
|
output.rewind
|
12
12
|
expect(output.read).to eq([
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe TTY::ProgressBar, 'total_byte' do
|
6
|
+
let(:output) { StringIO.new('', 'w+') }
|
7
|
+
|
8
|
+
it "displays bytes total" do
|
9
|
+
progress = described_class.new(":total_byte", output: output, total: 102_400)
|
10
|
+
5.times { progress.advance(20_480) }
|
11
|
+
output.rewind
|
12
|
+
expect(output.read).to eq([
|
13
|
+
"\e[1G100.00KB",
|
14
|
+
"\e[1G100.00KB",
|
15
|
+
"\e[1G100.00KB",
|
16
|
+
"\e[1G100.00KB",
|
17
|
+
"\e[1G100.00KB\n"
|
18
|
+
].join)
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe TTY::ProgressBar, 'total' do
|
6
|
+
let(:output) { StringIO.new('', 'w+') }
|
7
|
+
|
8
|
+
it "displays bytes total" do
|
9
|
+
progress = described_class.new(":total", output: output, total: 102_400)
|
10
|
+
5.times { progress.advance(20_480) }
|
11
|
+
output.rewind
|
12
|
+
expect(output.read).to eq([
|
13
|
+
"\e[1G102400",
|
14
|
+
"\e[1G102400",
|
15
|
+
"\e[1G102400",
|
16
|
+
"\e[1G102400",
|
17
|
+
"\e[1G102400\n"
|
18
|
+
].join)
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe TTY::ProgressBar, 'ratio=' do
|
6
|
+
let(:output) { StringIO.new('', 'w+') }
|
7
|
+
|
8
|
+
it "allows to set ratio" do
|
9
|
+
progress = TTY::ProgressBar.new("[:bar]", output: output, total: 10)
|
10
|
+
progress.ratio = 0.7
|
11
|
+
expect(progress.current).to eq(7)
|
12
|
+
output.rewind
|
13
|
+
expect(output.read).to eq([
|
14
|
+
"\e[1G[======= ]"
|
15
|
+
].join)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "finds closest available step from the ratio" do
|
19
|
+
progress = TTY::ProgressBar.new("[:bar]", output: output, total: 3)
|
20
|
+
progress.ratio = 0.5
|
21
|
+
expect(progress.current).to eq(1)
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe TTY::ProgressBar, 'current=' do
|
6
|
+
let(:output) { StringIO.new('', 'w+') }
|
7
|
+
|
8
|
+
it "allows to go back" do
|
9
|
+
progress = TTY::ProgressBar.new("[:bar]", output: output, total: 10)
|
10
|
+
3.times { progress.advance }
|
11
|
+
progress.current = 5
|
12
|
+
expect(progress.current).to eq(5)
|
13
|
+
output.rewind
|
14
|
+
expect(output.read).to eq([
|
15
|
+
"\e[1G[= ]",
|
16
|
+
"\e[1G[== ]",
|
17
|
+
"\e[1G[=== ]",
|
18
|
+
"\e[1G[===== ]"
|
19
|
+
].join)
|
20
|
+
progress.current = 0
|
21
|
+
expect(progress.current).to eq(0)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "doesn't allow to go over total" do
|
25
|
+
progress = TTY::ProgressBar.new("[:bar]", output: output, total: 10)
|
26
|
+
progress.current = 12
|
27
|
+
expect(progress.current).to eq(10)
|
28
|
+
output.rewind
|
29
|
+
expect(output.read).to eq("\e[1G[==========]\n")
|
30
|
+
end
|
31
|
+
|
32
|
+
it "doesn't allow to go below 0" do
|
33
|
+
progress = TTY::ProgressBar.new("[:bar]", output: output, total: 10)
|
34
|
+
progress.current = -1
|
35
|
+
expect(progress.current).to eq(0)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "cannot backtrack on finished" do
|
39
|
+
progress = TTY::ProgressBar.new("[:bar]", output: output, total: 10)
|
40
|
+
progress.current = 10
|
41
|
+
expect(progress.current).to eq(10)
|
42
|
+
progress.current = 5
|
43
|
+
expect(progress.current).to eq(10)
|
44
|
+
output.rewind
|
45
|
+
expect(output.read).to eq("\e[1G[==========]\n")
|
46
|
+
end
|
47
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tty-progressbar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Murach
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: tty-screen
|
@@ -62,12 +62,13 @@ files:
|
|
62
62
|
- lib/tty/progressbar/converter.rb
|
63
63
|
- lib/tty/progressbar/formatter.rb
|
64
64
|
- lib/tty/progressbar/formatter/bar.rb
|
65
|
-
- lib/tty/progressbar/formatter/byte.rb
|
66
65
|
- lib/tty/progressbar/formatter/current.rb
|
66
|
+
- lib/tty/progressbar/formatter/current_byte.rb
|
67
67
|
- lib/tty/progressbar/formatter/elapsed.rb
|
68
68
|
- lib/tty/progressbar/formatter/estimated.rb
|
69
69
|
- lib/tty/progressbar/formatter/percent.rb
|
70
70
|
- lib/tty/progressbar/formatter/total.rb
|
71
|
+
- lib/tty/progressbar/formatter/total_byte.rb
|
71
72
|
- lib/tty/progressbar/pipeline.rb
|
72
73
|
- lib/tty/progressbar/version.rb
|
73
74
|
- spec/spec_helper.rb
|
@@ -78,17 +79,21 @@ files:
|
|
78
79
|
- spec/unit/converter/to_time_spec.rb
|
79
80
|
- spec/unit/custom_formatter_spec.rb
|
80
81
|
- spec/unit/formatter/bar_spec.rb
|
81
|
-
- spec/unit/formatter/
|
82
|
+
- spec/unit/formatter/current_byte_spec.rb
|
82
83
|
- spec/unit/formatter/current_spec.rb
|
83
84
|
- spec/unit/formatter/elapsed_spec.rb
|
84
85
|
- spec/unit/formatter/estimated_spec.rb
|
85
86
|
- spec/unit/formatter/percent_spec.rb
|
87
|
+
- spec/unit/formatter/total_byte_spec.rb
|
88
|
+
- spec/unit/formatter/total_spec.rb
|
86
89
|
- spec/unit/frequency_spec.rb
|
87
90
|
- spec/unit/hide_cursor_spec.rb
|
88
91
|
- spec/unit/log_spec.rb
|
89
92
|
- spec/unit/new_spec.rb
|
90
93
|
- spec/unit/pipeline_spec.rb
|
94
|
+
- spec/unit/ratio_spec.rb
|
91
95
|
- spec/unit/resize_spec.rb
|
96
|
+
- spec/unit/set_current_spec.rb
|
92
97
|
- spec/unit/width_spec.rb
|
93
98
|
- tasks/console.rake
|
94
99
|
- tasks/coverage.rake
|
@@ -127,16 +132,20 @@ test_files:
|
|
127
132
|
- spec/unit/converter/to_time_spec.rb
|
128
133
|
- spec/unit/custom_formatter_spec.rb
|
129
134
|
- spec/unit/formatter/bar_spec.rb
|
130
|
-
- spec/unit/formatter/
|
135
|
+
- spec/unit/formatter/current_byte_spec.rb
|
131
136
|
- spec/unit/formatter/current_spec.rb
|
132
137
|
- spec/unit/formatter/elapsed_spec.rb
|
133
138
|
- spec/unit/formatter/estimated_spec.rb
|
134
139
|
- spec/unit/formatter/percent_spec.rb
|
140
|
+
- spec/unit/formatter/total_byte_spec.rb
|
141
|
+
- spec/unit/formatter/total_spec.rb
|
135
142
|
- spec/unit/frequency_spec.rb
|
136
143
|
- spec/unit/hide_cursor_spec.rb
|
137
144
|
- spec/unit/log_spec.rb
|
138
145
|
- spec/unit/new_spec.rb
|
139
146
|
- spec/unit/pipeline_spec.rb
|
147
|
+
- spec/unit/ratio_spec.rb
|
140
148
|
- spec/unit/resize_spec.rb
|
149
|
+
- spec/unit/set_current_spec.rb
|
141
150
|
- spec/unit/width_spec.rb
|
142
151
|
has_rdoc:
|