tty-progressbar 0.15.1 → 0.18.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +63 -0
- data/LICENSE.txt +1 -1
- data/README.md +495 -125
- data/lib/tty-progressbar.rb +2 -2
- data/lib/tty/progressbar.rb +169 -68
- data/lib/tty/progressbar/configuration.rb +121 -27
- data/lib/tty/progressbar/converter.rb +16 -19
- data/lib/tty/progressbar/formats.rb +120 -0
- data/lib/tty/progressbar/formatter.rb +33 -38
- data/lib/tty/progressbar/formatter/bar.rb +74 -27
- data/lib/tty/progressbar/formatter/byte_rate.rb +6 -20
- data/lib/tty/progressbar/formatter/current.rb +4 -19
- data/lib/tty/progressbar/formatter/current_byte.rb +9 -17
- data/lib/tty/progressbar/formatter/elapsed.rb +9 -18
- data/lib/tty/progressbar/formatter/estimated.rb +18 -20
- data/lib/tty/progressbar/formatter/estimated_time.rb +39 -0
- data/lib/tty/progressbar/formatter/mean_byte.rb +6 -20
- data/lib/tty/progressbar/formatter/mean_rate.rb +6 -20
- data/lib/tty/progressbar/formatter/percent.rb +10 -16
- data/lib/tty/progressbar/formatter/rate.rb +5 -19
- data/lib/tty/progressbar/formatter/total.rb +10 -16
- data/lib/tty/progressbar/formatter/total_byte.rb +14 -18
- data/lib/tty/progressbar/formatters.rb +53 -0
- data/lib/tty/progressbar/meter.rb +2 -2
- data/lib/tty/progressbar/multi.rb +61 -21
- data/lib/tty/progressbar/pipeline.rb +13 -6
- data/lib/tty/progressbar/timer.rb +89 -0
- data/lib/tty/progressbar/version.rb +3 -1
- metadata +56 -164
- data/.codeclimate.yml +0 -11
- data/.gitignore +0 -14
- data/.rspec +0 -3
- data/.travis.yml +0 -26
- data/CODE_OF_CONDUCT.md +0 -74
- data/Gemfile +0 -14
- data/Rakefile +0 -8
- data/appveyor.yml +0 -23
- data/examples/color.rb +0 -18
- data/examples/failure.rb +0 -12
- data/examples/iterator.rb +0 -5
- data/examples/lazy.rb +0 -6
- data/examples/multi/main_bar.rb +0 -13
- data/examples/multi/simple.rb +0 -13
- data/examples/multi/width.rb +0 -13
- data/examples/simple.rb +0 -7
- data/examples/slow_process.rb +0 -29
- data/examples/speed.rb +0 -11
- data/examples/threaded.rb +0 -14
- data/examples/tokens.rb +0 -12
- data/examples/unicode.rb +0 -7
- data/spec/spec_helper.rb +0 -51
- data/spec/unit/advance_spec.rb +0 -25
- data/spec/unit/clear_spec.rb +0 -17
- data/spec/unit/complete_spec.rb +0 -16
- data/spec/unit/converter/to_bytes_spec.rb +0 -47
- data/spec/unit/converter/to_seconds_spec.rb +0 -15
- data/spec/unit/converter/to_time_spec.rb +0 -19
- data/spec/unit/custom_formatter_spec.rb +0 -26
- data/spec/unit/custom_token_spec.rb +0 -14
- data/spec/unit/events_spec.rb +0 -33
- data/spec/unit/finish_spec.rb +0 -15
- data/spec/unit/formatter/bar_spec.rb +0 -16
- data/spec/unit/formatter/byte_rate_spec.rb +0 -32
- data/spec/unit/formatter/current_byte_spec.rb +0 -16
- data/spec/unit/formatter/current_spec.rb +0 -14
- data/spec/unit/formatter/elapsed_spec.rb +0 -58
- data/spec/unit/formatter/estimated_spec.rb +0 -27
- data/spec/unit/formatter/mean_byte_spec.rb +0 -32
- data/spec/unit/formatter/mean_rate_spec.rb +0 -31
- data/spec/unit/formatter/percent_spec.rb +0 -16
- data/spec/unit/formatter/rate_spec.rb +0 -31
- data/spec/unit/formatter/total_byte_spec.rb +0 -16
- data/spec/unit/formatter/total_spec.rb +0 -16
- data/spec/unit/frequency_spec.rb +0 -27
- data/spec/unit/head_spec.rb +0 -32
- data/spec/unit/hide_cursor_spec.rb +0 -27
- data/spec/unit/inspect_spec.rb +0 -11
- data/spec/unit/iterate_spec.rb +0 -79
- data/spec/unit/log_spec.rb +0 -29
- data/spec/unit/meter_spec.rb +0 -70
- data/spec/unit/multi/advance_spec.rb +0 -123
- data/spec/unit/multi/events_spec.rb +0 -115
- data/spec/unit/multi/finish_spec.rb +0 -41
- data/spec/unit/multi/line_inset_spec.rb +0 -65
- data/spec/unit/multi/register_spec.rb +0 -35
- data/spec/unit/multi/reset_spec.rb +0 -28
- data/spec/unit/multi/stop_spec.rb +0 -15
- data/spec/unit/multi/width_spec.rb +0 -118
- data/spec/unit/new_spec.rb +0 -76
- data/spec/unit/pipeline_spec.rb +0 -19
- data/spec/unit/ratio_spec.rb +0 -31
- data/spec/unit/render_spec.rb +0 -25
- data/spec/unit/reset_spec.rb +0 -31
- data/spec/unit/resize_spec.rb +0 -35
- data/spec/unit/set_current_spec.rb +0 -43
- data/spec/unit/start_spec.rb +0 -14
- data/spec/unit/stop_spec.rb +0 -19
- data/spec/unit/update_spec.rb +0 -22
- data/spec/unit/width_spec.rb +0 -86
- data/tasks/console.rake +0 -9
- data/tasks/coverage.rake +0 -9
- data/tasks/spec.rake +0 -27
- data/tty-progressbar.gemspec +0 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6e266e18a501b913609146035057b886fb5810fa9415b4ba217cf21901b4963e
|
4
|
+
data.tar.gz: 7ef378c17f1d033f4ef38258591ece40267cf51f4867786f42c6aaec4734edf7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b77549a6eafedcd5bbe077013f53a694cbcfe0b21fe3d098a8e790dbb8c95e37af1f49b2d3c928196002c26f3e0f1dbda0eb472a947f5c0f421e49cf9bfc3df1
|
7
|
+
data.tar.gz: 22b5ea40361f6a300211a34031f39436120be9fcac78a62fb51a334c0f279422ad45229c5379584a85368478e72a242f376abbec6682833b4c82015d5bb3f816
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,63 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
## [v0.18.2] - 2021-03-08
|
4
|
+
|
5
|
+
### Fixed
|
6
|
+
* Fix calculating total in MultiBar with indeterminate children by Tim Tilberg(@ttilberg)
|
7
|
+
|
8
|
+
## [v0.18.1] - 2021-01-25
|
9
|
+
|
10
|
+
### Fixed
|
11
|
+
* Fix :eta and :eta_time format tokens display when progress isn't started
|
12
|
+
|
13
|
+
## [v0.18.0] - 2021-01-20
|
14
|
+
|
15
|
+
### Added
|
16
|
+
* Add #resume to allow stopped or paused bar to continue progressing
|
17
|
+
* Add :clear_head option to remove head when progress is done
|
18
|
+
* Add #configure to allow runtime configuration
|
19
|
+
* Add Multi#done? to check if all bar are stopped or finished
|
20
|
+
* Add indeterminate progress support when no total is given
|
21
|
+
* Add :bar_format option to allow selecting preconfigured bar displays
|
22
|
+
* Add :eta_time format token to display the estimated time of day at completion
|
23
|
+
* Add measurement of the total elapsed time that ignores stopped time intervals
|
24
|
+
* Add #pause to prevent bar from continuing progression and suspend time measurements
|
25
|
+
* Add Multi#pause to allow suspending progression of all registered bars at once
|
26
|
+
* Add Multi#resume to start again all registered bars that are stopped or paused
|
27
|
+
* Add Timer class to handle the total elapsed time measurements
|
28
|
+
|
29
|
+
### Changed
|
30
|
+
* Change Multi#stopped? to check that all bars are stopped
|
31
|
+
* Change gemspec to load version directly and remove test artifacts
|
32
|
+
* Change to update strings-ansi and tty-screen dependencies
|
33
|
+
* Change Pipeline to inject progress bar instance only once
|
34
|
+
* Change :elapsed and :eta to show days after running for 24 hours
|
35
|
+
* Change to ensure complete, incomplete and unknown option cannot be an empty string
|
36
|
+
* Change to allow setting total to nil via accessor
|
37
|
+
* Change gemspec to allow version 2.0 of unicode-display_width dependency
|
38
|
+
* Change #stop to show hidden cursor after render similar to #finish
|
39
|
+
|
40
|
+
### Fixed
|
41
|
+
* Fix MultiBar top bar to allow resuming progress when stopped/done (@d4be4st)
|
42
|
+
* Fix MultiBar to only set width when top bar present
|
43
|
+
|
44
|
+
## [v0.17.0] - 2019-05-31
|
45
|
+
|
46
|
+
### Changed
|
47
|
+
* Change gemspec to load files directly without git
|
48
|
+
* Change to update tty-cursor and tty-screen dependencies
|
49
|
+
|
50
|
+
## [v0.16.0] - 2018-08-27
|
51
|
+
|
52
|
+
### Added
|
53
|
+
* Add strings-ansi dependency
|
54
|
+
|
55
|
+
### Changed
|
56
|
+
* Change tty-cursor dependency version
|
57
|
+
|
58
|
+
### Fixed
|
59
|
+
* Fix to handle ANSI codes in bar formatting to allow correct size calculation
|
60
|
+
|
3
61
|
## [v0.15.1] - 2018-07-19
|
4
62
|
|
5
63
|
### Fixed
|
@@ -184,6 +242,11 @@
|
|
184
242
|
|
185
243
|
* Initial implementation and release
|
186
244
|
|
245
|
+
[v0.18.2]: https://github.com/piotrmurach/tty-progressbar/compare/v0.18.1...v0.18.2
|
246
|
+
[v0.18.1]: https://github.com/piotrmurach/tty-progressbar/compare/v0.18.0...v0.18.1
|
247
|
+
[v0.18.0]: https://github.com/piotrmurach/tty-progressbar/compare/v0.17.0...v0.18.0
|
248
|
+
[v0.17.0]: https://github.com/piotrmurach/tty-progressbar/compare/v0.16.0...v0.17.0
|
249
|
+
[v0.16.0]: https://github.com/piotrmurach/tty-progressbar/compare/v0.15.1...v0.16.0
|
187
250
|
[v0.15.1]: https://github.com/piotrmurach/tty-progressbar/compare/v0.15.0...v0.15.1
|
188
251
|
[v0.15.0]: https://github.com/piotrmurach/tty-progressbar/compare/v0.14.0...v0.15.0
|
189
252
|
[v0.14.0]: https://github.com/piotrmurach/tty-progressbar/compare/v0.13.0...v0.14.0
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,11 @@
|
|
1
|
+
<div align="center">
|
2
|
+
<a href="https://ttytoolkit.org"><img width="130" src="https://github.com/piotrmurach/tty/raw/master/images/tty.png" alt="TTY Toolkit logo"/></a>
|
3
|
+
</div>
|
4
|
+
|
1
5
|
# TTY::ProgressBar [![Gitter](https://badges.gitter.im/Join%20Chat.svg)][gitter]
|
2
6
|
|
3
7
|
[![Gem Version](https://badge.fury.io/rb/tty-progressbar.svg)][gem]
|
4
|
-
[![
|
8
|
+
[![Actions CI](https://github.com/piotrmurach/tty-progressbar/workflows/CI/badge.svg?branch=master)][gh_actions_ci]
|
5
9
|
[![Build status](https://ci.appveyor.com/api/projects/status/w3jafjeatt1ulufa?svg=true)][appveyor]
|
6
10
|
[![Maintainability](https://api.codeclimate.com/v1/badges/e85416137d2057169575/maintainability)][codeclimate]
|
7
11
|
[![Coverage Status](https://coveralls.io/repos/github/piotrmurach/tty-progressbar/badge.svg)][coverage]
|
@@ -9,41 +13,46 @@
|
|
9
13
|
|
10
14
|
[gitter]: https://gitter.im/piotrmurach/tty
|
11
15
|
[gem]: http://badge.fury.io/rb/tty-progressbar
|
12
|
-
[
|
16
|
+
[gh_actions_ci]: https://github.com/piotrmurach/tty-progressbar/actions?query=workflow%3ACI
|
13
17
|
[appveyor]: https://ci.appveyor.com/project/piotrmurach/tty-progressbar
|
14
18
|
[codeclimate]: https://codeclimate.com/github/piotrmurach/tty-progressbar/maintainability
|
15
19
|
[coverage]: https://coveralls.io/github/piotrmurach/tty-progressbar
|
16
20
|
[inchpages]: http://inch-ci.org/github/piotrmurach/tty-progressbar
|
17
21
|
|
18
|
-
> A flexible progress
|
22
|
+
> A flexible and extensible progress bar for terminal applications.
|
19
23
|
|
20
|
-
**TTY::ProgressBar** provides independent progress
|
24
|
+
**TTY::ProgressBar** provides independent progress bar component for [TTY](https://github.com/piotrmurach/tty) toolkit.
|
21
25
|
|
22
26
|
## Features
|
23
27
|
|
24
|
-
*
|
25
|
-
*
|
26
|
-
*
|
27
|
-
*
|
28
|
-
*
|
29
|
-
*
|
30
|
-
*
|
28
|
+
* **Customisable.** Choose from many [configuration](#3-configuration) options to get the behaviour you want.
|
29
|
+
* **Flexible.** Describe bar [format](#4-formatting) and pick from many predefined [tokens](#41-tokens) and [bar styles](#37-bar_format).
|
30
|
+
* **Extensible.** Define [custom tokens](#42-custom-formatters) to fit your needs.
|
31
|
+
* **Powerful.** Display [multi](#6-ttyprogressbarmulti-api) progress bars in parallel.
|
32
|
+
* Show an unbounded operation with [indeterminate](#31-total) progress.
|
33
|
+
* [Pause](#210-pause) and [resume](#212-resume) progress at any time.
|
34
|
+
* Include [Unicode](#44-unicode) characters in progress bar.
|
35
|
+
* Works on all ECMA-48 compatible terminals.
|
31
36
|
|
32
37
|
## Installation
|
33
38
|
|
34
39
|
Add this line to your application's Gemfile:
|
35
40
|
|
36
41
|
```ruby
|
37
|
-
gem
|
42
|
+
gem "tty-progressbar"
|
38
43
|
```
|
39
44
|
|
40
45
|
And then execute:
|
41
46
|
|
42
|
-
|
47
|
+
```
|
48
|
+
$ bundle
|
49
|
+
```
|
43
50
|
|
44
51
|
Or install it yourself as:
|
45
52
|
|
46
|
-
|
53
|
+
```
|
54
|
+
$ gem install tty-progressbar
|
55
|
+
```
|
47
56
|
|
48
57
|
## Contents
|
49
58
|
|
@@ -58,15 +67,29 @@ Or install it yourself as:
|
|
58
67
|
* [2.7 update](#27-update)
|
59
68
|
* [2.8 finish](#28-finish)
|
60
69
|
* [2.9 stop](#29-stop)
|
61
|
-
* [2.10
|
62
|
-
* [2.11
|
63
|
-
* [2.12
|
64
|
-
* [2.13
|
70
|
+
* [2.10 pause](#210-pause)
|
71
|
+
* [2.11 reset](#211-reset)
|
72
|
+
* [2.12 resume](#212-resume)
|
73
|
+
* [2.13 complete?](#213-complete)
|
74
|
+
* [2.14 paused?](#214-paused)
|
75
|
+
* [2.15 stopped?](#215-stopped)
|
76
|
+
* [2.16 indeterminate?](#216-indeterminate)
|
77
|
+
* [2.17 resize](#217-resize)
|
78
|
+
* [2.18 on](#218-on)
|
65
79
|
* [3. Configuration](#3-configuration)
|
66
|
-
* [3.1 :
|
67
|
-
* [3.
|
68
|
-
* [3.3 :
|
69
|
-
* [3.4 :
|
80
|
+
* [3.1 :total](#31-total)
|
81
|
+
* [3.1 :width](#32-width)
|
82
|
+
* [3.3 :complete](#33-complete)
|
83
|
+
* [3.4 :incomplete](#34-incomplete)
|
84
|
+
* [3.5 :head](#35-head)
|
85
|
+
* [3.6 :unknown](#36-unknown)
|
86
|
+
* [3.7 :bar_format](#37-bar_format)
|
87
|
+
* [3.8 :output](#38-output)
|
88
|
+
* [3.9 :frequency](#39-frequency)
|
89
|
+
* [3.10 :interval](#310-interval)
|
90
|
+
* [3.11 :hide_cursor](#311-hide_cursor)
|
91
|
+
* [3.12 :clear](#312-clear)
|
92
|
+
* [3.13 :clear_head](#313-clear_head)
|
70
93
|
* [4. Formatting](#4-formatting)
|
71
94
|
* [4.1 Tokens](#41-tokens)
|
72
95
|
* [4.2 Custom Formatters](#42-custom-formatters)
|
@@ -80,40 +103,63 @@ Or install it yourself as:
|
|
80
103
|
* [6.4 start](#64-start)
|
81
104
|
* [6.5 finish](#65-finish)
|
82
105
|
* [6.6 stop](#66-stop)
|
83
|
-
* [6.7
|
84
|
-
* [6.8
|
85
|
-
* [6.9
|
106
|
+
* [6.7 pause](#67-pause)
|
107
|
+
* [6.8 resume](#68-resume)
|
108
|
+
* [6.9 complete?](#69-complete)
|
109
|
+
* [6.10 paused?](#610-paused)
|
110
|
+
* [6.11 stopped?](#611-stopped)
|
111
|
+
* [6.12 on](#612-on)
|
112
|
+
* [6.13 :style](#613-style)
|
86
113
|
* [7. Examples](#7-examples)
|
87
114
|
* [7.1 Color](#71-color)
|
88
115
|
* [7.2 Speed](#72-speed)
|
89
116
|
|
90
117
|
## 1. Usage
|
91
118
|
|
92
|
-
**TTY::ProgressBar** requires only format string and total number of steps to completion
|
119
|
+
**TTY::ProgressBar** requires only a format string with `:bar` [token](#41-tokens) and total number of steps to completion:
|
93
120
|
|
94
121
|
```ruby
|
95
122
|
bar = TTY::ProgressBar.new("downloading [:bar]", total: 30)
|
123
|
+
```
|
124
|
+
|
125
|
+
Once initialized, use [advance](#21-advance) method to indicated progress:
|
126
|
+
|
127
|
+
```ruby
|
96
128
|
30.times do
|
97
129
|
sleep(0.1)
|
98
|
-
bar.advance
|
130
|
+
bar.advance # by default increases by 1
|
99
131
|
end
|
100
132
|
```
|
101
133
|
|
102
|
-
This would produce animation in your terminal:
|
134
|
+
This would produce the following animation in your terminal:
|
103
135
|
|
104
136
|
```ruby
|
105
137
|
# downloading [======================= ]
|
106
138
|
```
|
107
139
|
|
108
|
-
|
140
|
+
You can further change a progress bar behaviour and display by changing [configuration](#3-configuration) options and using many predefined [tokens](#41-tokens) and [bar formats](#37-bar_format).
|
141
|
+
|
142
|
+
When you don't know the total yet, you can set it to `nil` to switch to [indeterminate](#31-total) progress:
|
143
|
+
|
144
|
+
```ruby
|
145
|
+
# downloading [ <=> ]
|
146
|
+
```
|
147
|
+
|
148
|
+
Use [TTY::ProgressBar::Multi](#6-ttyprogressbarmulti-api) to display multiple parallel progress bars.
|
149
|
+
|
150
|
+
Declare a top level bar and then register child bars:
|
109
151
|
|
110
152
|
```ruby
|
111
153
|
bars = TTY::ProgressBar::Multi.new("main [:bar] :percent")
|
112
154
|
|
113
155
|
bar1 = bars.register("one [:bar] :percent", total: 15)
|
114
156
|
bar2 = bars.register("two [:bar] :percent", total: 15)
|
157
|
+
```
|
115
158
|
|
116
|
-
bars
|
159
|
+
Then progress the child bars in parallel:
|
160
|
+
|
161
|
+
```ruby
|
162
|
+
bars.start # starts all registered bars timers
|
117
163
|
|
118
164
|
th1 = Thread.new { 15.times { sleep(0.1); bar1.advance } }
|
119
165
|
th2 = Thread.new { 15.times { sleep(0.1); bar2.advance } }
|
@@ -121,7 +167,7 @@ th2 = Thread.new { 15.times { sleep(0.1); bar2.advance } }
|
|
121
167
|
[th1, th2].each { |t| t.join }
|
122
168
|
```
|
123
169
|
|
124
|
-
|
170
|
+
A possible terminal output may look like this:
|
125
171
|
|
126
172
|
```ruby
|
127
173
|
# ┌ main [=============== ] 50%
|
@@ -133,10 +179,10 @@ which will produce:
|
|
133
179
|
|
134
180
|
### 2.1 advance
|
135
181
|
|
136
|
-
Once you have **TTY::ProgressBar** instance, you can progress the display by calling `advance` method. By default it will increase by `1` but you can pass any number of steps, for instance,
|
182
|
+
Once you have **TTY::ProgressBar** instance, you can progress the display by calling `advance` method. By default, it will increase by `1` but you can pass any number of steps, for instance, a number of bytes for a downloaded file:
|
137
183
|
|
138
184
|
```ruby
|
139
|
-
bar.advance(
|
185
|
+
bar.advance(1024)
|
140
186
|
```
|
141
187
|
|
142
188
|
You can also pass negative steps if you wish to backtrack the progress:
|
@@ -145,13 +191,13 @@ You can also pass negative steps if you wish to backtrack the progress:
|
|
145
191
|
bar.advance(-1)
|
146
192
|
```
|
147
193
|
|
148
|
-
Note
|
194
|
+
*Note:* If a progress bar has already finished then any negative steps will not set it back to desired value.
|
149
195
|
|
150
196
|
### 2.2 iterate
|
151
197
|
|
152
198
|
To simplify progressing over an enumerable you can use `iterate` which as a first argument accepts an `Enumerable` and as a second the amount to progress the bar with.
|
153
199
|
|
154
|
-
First, create a progress bar without a total which will be
|
200
|
+
First, create a progress bar without a total which will be automatically updated for you once iteration starts:
|
155
201
|
|
156
202
|
```ruby
|
157
203
|
bar = TTY::ProgressBar.new("[:bar]")
|
@@ -163,7 +209,7 @@ Then, either directly iterate over a collection by yielding values to a block:
|
|
163
209
|
bar.iterate(30.times) { |v| ... }
|
164
210
|
```
|
165
211
|
|
166
|
-
|
212
|
+
Or return an `Enumerator`:
|
167
213
|
|
168
214
|
```ruby
|
169
215
|
progress = bar.iterate(30.times)
|
@@ -191,7 +237,7 @@ downloader = Enumerator.new do |y|
|
|
191
237
|
end
|
192
238
|
```
|
193
239
|
|
194
|
-
|
240
|
+
Would be used with progress bar with the total size matching the content size like so:
|
195
241
|
|
196
242
|
```ruby
|
197
243
|
bar = TTY::ProgressBar.new("[:bar]", total: content_size)
|
@@ -205,17 +251,17 @@ Please run [slow_process example](examples/slow_process.rb) to see this in actio
|
|
205
251
|
|
206
252
|
### 2.3 current=
|
207
253
|
|
208
|
-
|
254
|
+
A progress doesn't have to start from zero. You can set it to a given value using `current=` method:
|
209
255
|
|
210
256
|
```ruby
|
211
257
|
bar.current = 50
|
212
258
|
```
|
213
259
|
|
214
|
-
Note
|
260
|
+
*Note:* If a progress bar has already finished then setting current value will not have any effect.
|
215
261
|
|
216
262
|
### 2.4 ratio=
|
217
263
|
|
218
|
-
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:
|
264
|
+
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:
|
219
265
|
|
220
266
|
```ruby
|
221
267
|
bar.ratio = 0.5
|
@@ -223,13 +269,15 @@ bar.ratio = 0.5
|
|
223
269
|
|
224
270
|
### 2.5 width=
|
225
271
|
|
226
|
-
You can set how many terminal columns will the `:bar` actually span excluding any other tokens and/or text.
|
272
|
+
You can set how many terminal columns will the `:bar` actually span excluding any other tokens and/or text.
|
273
|
+
|
274
|
+
For example, if you need the bar to be always 20 columns wide do:
|
227
275
|
|
228
276
|
```ruby
|
229
277
|
bar.width = 20
|
230
278
|
```
|
231
279
|
|
232
|
-
|
280
|
+
Or with configuration options:
|
233
281
|
|
234
282
|
```ruby
|
235
283
|
bar = TTY::ProgressBar.new("[:bar]", width: 20)
|
@@ -237,7 +285,7 @@ bar = TTY::ProgressBar.new("[:bar]", width: 20)
|
|
237
285
|
|
238
286
|
### 2.6 start
|
239
287
|
|
240
|
-
By default the timer for internal time
|
288
|
+
By default the timer for internal time estimation is started automatically when the `advance` method is called. However, if you require control on when the progression timer is started use `start` call:
|
241
289
|
|
242
290
|
```ruby
|
243
291
|
bar.start # => sets timer and draws initial progress bar
|
@@ -245,15 +293,15 @@ bar.start # => sets timer and draws initial progress bar
|
|
245
293
|
|
246
294
|
### 2.7 update
|
247
295
|
|
248
|
-
Once
|
296
|
+
Once a progress bar has been started, you can change its configuration option(s) by calling `update`:
|
249
297
|
|
250
298
|
```ruby
|
251
|
-
bar.update(complete:
|
299
|
+
bar.update(complete: "+", frequency: 10)
|
252
300
|
```
|
253
301
|
|
254
302
|
### 2.8 finish
|
255
303
|
|
256
|
-
In order to immediately stop and finish
|
304
|
+
In order to immediately stop and finish progress of a bar call `finish`. This will finish drawing the progress by advancing it to 100% and returning to a new line.
|
257
305
|
|
258
306
|
```ruby
|
259
307
|
bar.finish
|
@@ -261,13 +309,23 @@ bar.finish
|
|
261
309
|
|
262
310
|
### 2.9 stop
|
263
311
|
|
264
|
-
In order to immediately stop
|
312
|
+
In order to immediately stop a bar in the current position and thus prevent any further progress use `stop`:
|
265
313
|
|
266
314
|
```ruby
|
267
315
|
bar.stop
|
268
316
|
```
|
269
317
|
|
270
|
-
### 2.10
|
318
|
+
### 2.10 pause
|
319
|
+
|
320
|
+
A running progress bar can be paused at the current position using `pause` method:
|
321
|
+
|
322
|
+
```ruby
|
323
|
+
bar.pause
|
324
|
+
```
|
325
|
+
|
326
|
+
A paused progress bar will stop accumulating any time measurements like elapsed time. It also won't return to a new line, so a progress animation can be smoothly resumed.
|
327
|
+
|
328
|
+
### 2.11 reset
|
271
329
|
|
272
330
|
In order to reset currently running or finished progress bar to its original configuration and initial position use `reset` like so:
|
273
331
|
|
@@ -275,23 +333,57 @@ In order to reset currently running or finished progress bar to its original con
|
|
275
333
|
bar.reset
|
276
334
|
```
|
277
335
|
|
278
|
-
After resetting
|
336
|
+
After resetting a progress bar, if you wish to draw and start a bar and its timers use `start` call.
|
337
|
+
|
338
|
+
### 2.12 resume
|
339
|
+
|
340
|
+
When a bar is stopped or paused, you can continue its progression using the `resume` method.
|
341
|
+
|
342
|
+
```ruby
|
343
|
+
bar.resume
|
344
|
+
```
|
345
|
+
|
346
|
+
A resumed progression will continue accumulating the total elapsed time without including time intervals for pausing or stopping.
|
279
347
|
|
280
|
-
### 2.
|
348
|
+
### 2.13 complete?
|
281
349
|
|
282
|
-
During
|
350
|
+
During progression you can check whether a bar is finished or not by calling `complete?`. The bar will only return `true` if the progression finished successfully, otherwise `false` will be returned.
|
283
351
|
|
284
352
|
```ruby
|
285
353
|
bar.complete? # => false
|
286
354
|
```
|
287
355
|
|
288
|
-
### 2.
|
356
|
+
### 2.14 paused?
|
357
|
+
|
358
|
+
To check whether a progress bar is paused or not use `paused?`:
|
359
|
+
|
360
|
+
```ruby
|
361
|
+
bar.paused? # => true
|
362
|
+
```
|
363
|
+
|
364
|
+
### 2.15 stopped?
|
365
|
+
|
366
|
+
To check whether a progress bar is stopped or not use `stopped?`:
|
367
|
+
|
368
|
+
```ruby
|
369
|
+
bar.stopped? # => true
|
370
|
+
```
|
371
|
+
|
372
|
+
### 2.16 indeterminate?
|
373
|
+
|
374
|
+
You can make a progress bar indeterminate by setting `:total` to `nil`. In this state, a progress bar animation is displayed to show unbounded task. You can check whether the progress bar is indeterminate with the `indeterminate?` method:
|
375
|
+
|
376
|
+
```ruby
|
377
|
+
bar.indeterminate? # => false
|
378
|
+
```
|
379
|
+
|
380
|
+
### 2.17 resize
|
289
381
|
|
290
|
-
If you
|
382
|
+
If you want to change a progress bar's current width, use `resize` and pass in a new desired length. However, if you don't provide any width the `resize` will use terminal current width as its base for scaling.
|
291
383
|
|
292
384
|
```ruby
|
293
|
-
bar.resize #
|
294
|
-
bar.resize(50) #
|
385
|
+
bar.resize # determine terminal width and scale accordingly
|
386
|
+
bar.resize(50) # will resize bar proportionately from this point onwards
|
295
387
|
```
|
296
388
|
|
297
389
|
To handle automatic resizing you can trap `:WINCH` signal:
|
@@ -300,66 +392,229 @@ To handle automatic resizing you can trap `:WINCH` signal:
|
|
300
392
|
trap(:WINCH) { bar.resize }
|
301
393
|
```
|
302
394
|
|
303
|
-
### 2.
|
395
|
+
### 2.18 on
|
304
396
|
|
305
|
-
|
397
|
+
A progress bar fires events when it is progressing, paused, stopped or finished. You can register to listen for these events using the `on` message.
|
306
398
|
|
307
|
-
Every time an `advance` is called the `:progress` event gets fired which you can listen for inside a block
|
399
|
+
Every time an `advance` is called the `:progress` event gets fired which you can listen for inside a block. A first yielded argument is the actual amount of progress:
|
308
400
|
|
309
401
|
```ruby
|
310
402
|
bar.on(:progress) { |amount| ... }
|
311
403
|
```
|
312
404
|
|
313
|
-
When
|
405
|
+
When a progress bar finishes and completes then the `:done` event is fired. You can listen for this event:
|
314
406
|
|
315
407
|
```ruby
|
316
408
|
bar.on(:done) { ... }
|
317
409
|
```
|
318
410
|
|
319
|
-
Alternatively, when
|
411
|
+
Alternatively, when a progress bar gets stopped the `:stopped` event is fired. You can listen for this event:
|
320
412
|
|
321
413
|
```ruby
|
322
414
|
bar.on(:stopped) { ... }
|
323
415
|
```
|
324
416
|
|
417
|
+
Anytime a progress bar is paused the `:paused` event will be fired. To listen for this event do:
|
418
|
+
|
419
|
+
```ruby
|
420
|
+
bar.on(:paused) { ... }
|
421
|
+
```
|
422
|
+
|
325
423
|
## 3. Configuration
|
326
424
|
|
327
425
|
There are number of configuration options that can be provided:
|
328
426
|
|
329
|
-
*
|
330
|
-
*
|
331
|
-
*
|
332
|
-
*
|
333
|
-
* [:head](#
|
334
|
-
* [:
|
335
|
-
* [:
|
336
|
-
* [:
|
337
|
-
*
|
338
|
-
*
|
427
|
+
* [:total](#31-total) - the total number of steps to completion.
|
428
|
+
* [:width](#32-width) - the number of terminal columns for displaying a bar excluding other tokens. Defaults to total steps.
|
429
|
+
* [:complete](#33-complete) - the completion character, by default `=`.
|
430
|
+
* [:incomplete](#34-incomplete) - the incomplete character, by default single space.
|
431
|
+
* [:head](#35-head) - the head character, by default `=`.
|
432
|
+
* [:unknown](#36-unknown) - the character(s) used to show indeterminate progress, defaults to `<=>`.
|
433
|
+
* [:bar_format](#37-bar_format) - the predefined bar format, by default `:classic`.
|
434
|
+
* [:output](#38-output) - the output stream defaulting to `stderr`.
|
435
|
+
* [:frequency](#39-frequency) - used to throttle the output, by default `0`.
|
436
|
+
* [:interval](#310-interval) - the time interval used to measure rate, by default `1 sec`.
|
437
|
+
* [:hide_cursor](#311-hide_cursor) - whether to hide the console cursor or not, defaults to `false`.
|
438
|
+
* [:clear](#312-clear) - whether to clear the finished bar or not, defaults to `false`.
|
439
|
+
* [:clear_head](#313-clear_head) - whether to clear the head character when the progress is done or not, defaults to `false`.
|
339
440
|
|
340
441
|
All the above options can be passed in as hash options or block parameters:
|
341
442
|
|
342
443
|
```ruby
|
343
|
-
TTY::ProgressBar.new
|
444
|
+
bar = TTY::ProgressBar.new("[:bar]") do |config|
|
344
445
|
config.total = 30
|
345
446
|
config.frequency = 10
|
346
447
|
config.clear = true
|
347
448
|
end
|
348
449
|
```
|
349
450
|
|
350
|
-
|
451
|
+
The progress bar's configuration can also be changed at runtime with `configure`:
|
452
|
+
|
453
|
+
```ruby
|
454
|
+
bar.configure do |config|
|
455
|
+
config.total = 100 # takes precedence over the original value
|
456
|
+
config.frequencye = 20
|
457
|
+
end
|
458
|
+
```
|
459
|
+
|
460
|
+
Or with the [update](#27-update) method:
|
461
|
+
|
462
|
+
```ruby
|
463
|
+
bar.update(total: 100, frequency: 20)
|
464
|
+
```
|
465
|
+
|
466
|
+
### 3.1 :total
|
467
|
+
|
468
|
+
The `:total` option determines the final value at which the progress bar fills up and stops.
|
469
|
+
|
470
|
+
```ruby
|
471
|
+
TTY::ProgressBar.new("[:bar]", total: 30)
|
472
|
+
```
|
473
|
+
|
474
|
+
Setting `:total` to `nil` or leaving it out will cause the progress bar to switch to indeterminate mode. Instead of showing completeness for a task, it will render animation like `<=>` that moves left and right:
|
475
|
+
|
476
|
+
```ruby
|
477
|
+
# [ <=> ]
|
478
|
+
```
|
479
|
+
|
480
|
+
The indeterminate mode is useful to show time-consuming and unbounded task.
|
481
|
+
|
482
|
+
Run [examples/indeterminate](https://github.com/piotrmurach/tty-progressbar/blob/master/examples/indeterminate.rb) to see indeterminate progress animation in action.
|
483
|
+
|
484
|
+
### 3.2 :width
|
485
|
+
|
486
|
+
The progress bar width defaults to the total value and is capped at the maximum terminal width minus all the labels. If you want to enforce the bar to have a specific length use the `:width` option:
|
487
|
+
|
488
|
+
```ruby
|
489
|
+
TTY::ProgressBar.new("[:bar]", width: 30)
|
490
|
+
```
|
491
|
+
|
492
|
+
### 3.3 :complete
|
493
|
+
|
494
|
+
By default, the `=` character is used to mark progression but this can be changed with `:complete` option:
|
495
|
+
|
496
|
+
```ruby
|
497
|
+
TTY::ProgressBar.new("[:bar]", complete: "x")
|
498
|
+
```
|
499
|
+
|
500
|
+
Then the output could look like this:
|
501
|
+
|
502
|
+
```ruby
|
503
|
+
# [xxxxxxxx ]
|
504
|
+
```
|
505
|
+
|
506
|
+
### 3.4 :incomplete
|
507
|
+
|
508
|
+
By default no characters are shown to mark the remaining progress in the `:classic` bar format. Other [bar styles](#37-bar_format) often have incomplete character. You can change this with `:incomplete` option:
|
509
|
+
|
510
|
+
```ruby
|
511
|
+
TTY::ProgressBar.new("[:bar]", incomplete: "_")
|
512
|
+
```
|
513
|
+
|
514
|
+
A possible output may look like this:
|
515
|
+
|
516
|
+
```ruby
|
517
|
+
# [======_________]
|
518
|
+
```
|
519
|
+
|
520
|
+
### 3.5 :head
|
351
521
|
|
352
522
|
If you prefer for the animated bar to display a specific character for a head of progression then use `:head` option:
|
353
523
|
|
354
524
|
```ruby
|
355
|
-
|
356
|
-
|
525
|
+
TTY::ProgressBar.new("[:bar]", head: ">")
|
526
|
+
```
|
527
|
+
|
528
|
+
This could result in output like this:
|
529
|
+
|
530
|
+
```ruby
|
357
531
|
# [=======> ]
|
358
532
|
```
|
359
533
|
|
360
|
-
### 3.
|
534
|
+
### 3.6 :unknown
|
535
|
+
|
536
|
+
By default, a progress bar shows indeterminate progress using `<=>` characters:
|
537
|
+
|
538
|
+
```ruby
|
539
|
+
# [ <=> ]
|
540
|
+
```
|
541
|
+
|
542
|
+
Other [bar formats](#37-bar_format) use different characters.
|
543
|
+
|
544
|
+
You can change this with the `:unknown` option:
|
545
|
+
|
546
|
+
```ruby
|
547
|
+
TTY::ProgressBar.new("[:bar]", unknown: "<?>")
|
548
|
+
```
|
549
|
+
|
550
|
+
This may result in the following output:
|
551
|
+
|
552
|
+
```ruby
|
553
|
+
# [ <?> ]
|
554
|
+
````
|
555
|
+
|
556
|
+
### 3.7 :bar_format
|
557
|
+
|
558
|
+
There are number of preconfigured bar formats you can choose from.
|
559
|
+
|
560
|
+
| Name | Determinate | Indeterminate |
|
561
|
+
|:-----------|:-------------|:--------------|
|
562
|
+
| `:arrow` | `▸▸▸▸▸▹▹▹▹▹` | `◂▸` |
|
563
|
+
| `:asterisk`| `✱✱✱✱✱✳✳✳✳✳` | `✳✱✳` |
|
564
|
+
| `:blade` | `▰▰▰▰▰▱▱▱▱▱` | `▱▰▱` |
|
565
|
+
| `:block` | `█████░░░░░` | `█` |
|
566
|
+
| `:box` | `■■■■■□□□□□` | `□■□` |
|
567
|
+
| `:bracket` | `❭❭❭❭❭❭❭❭❭❭` | `❬=❭` |
|
568
|
+
| `:burger` | `≡≡≡≡≡≡≡≡≡≡` | `<≡>` |
|
569
|
+
| `:button` | `⦿⦿⦿⦿⦿⦾⦾⦾⦾⦾` | `⦾⦿⦾` |
|
570
|
+
| `:chevron` | `››››››››››` | `‹=›` |
|
571
|
+
| `:circle` | `●●●●●○○○○○` | `○●○` |
|
572
|
+
| `:classic` | `==========` | `<=>` |
|
573
|
+
| `:crate` | `▣▣▣▣▣⬚⬚⬚⬚⬚` | `⬚▣⬚` |
|
574
|
+
| `:diamond` | `♦♦♦♦♦♢♢♢♢♢` | `♢♦♢` |
|
575
|
+
| `:dot` | `・・・・・・・・・・` | `・・・` |
|
576
|
+
| `:heart` | `♥♥♥♥♥♡♡♡♡♡` | `♡♥♡` |
|
577
|
+
| `:rectangle` | `▮▮▮▮▮▯▯▯▯▯` | `▯▮▯` |
|
578
|
+
| `:square` | `▪▪▪▪▪▫▫▫▫▫` | `▫▪▫` |
|
579
|
+
| `:star` | `★★★★★☆☆☆☆☆` | `☆★☆` |
|
580
|
+
| `:track` | `▬▬▬▬▬═════` | `═▬═` |
|
581
|
+
| `:tread` | `❱❱❱❱❱❱❱❱❱❱` | `❰=❱` |
|
582
|
+
| `:triangle`| `▶▶▶▶▶▷▷▷▷▷` | `◀▶` |
|
583
|
+
| `:wave` | `~~~~~_____` | `<~>` |
|
584
|
+
|
585
|
+
For example, you can specify `:box` format with the `:bar_format` option:
|
586
|
+
|
587
|
+
```ruby
|
588
|
+
TTY::ProgressBar.new("[:bar]", bar_format: :box)
|
589
|
+
```
|
590
|
+
|
591
|
+
This will result in output like this:
|
592
|
+
|
593
|
+
```ruby
|
594
|
+
# [■■■■■□□□□□□□□□□]
|
595
|
+
```
|
596
|
+
|
597
|
+
You can overwrite `:complete`, `:incomplete`, `:head` and `:unknown` characters:
|
598
|
+
|
599
|
+
```ruby
|
600
|
+
TTY::ProgressBar.new("[:bar]", bar_format: :box, incomplete: " ", unknown: "?")
|
601
|
+
```
|
602
|
+
|
603
|
+
This will display the following when total is given:
|
604
|
+
|
605
|
+
```ruby
|
606
|
+
# [■■■■■ ]
|
607
|
+
```
|
608
|
+
|
609
|
+
And for the unknown progress the `?` character will move from left to right:
|
610
|
+
|
611
|
+
```ruby
|
612
|
+
# [ ? ]
|
613
|
+
```
|
614
|
+
|
615
|
+
### 3.8 :output
|
361
616
|
|
362
|
-
|
617
|
+
A progress bar only outputs to a console. When the output is, for example, redirected to a file or a pipe, the progress bar doesn't get printed. This is so, for example, your error logs do not overflow with progress bar output.
|
363
618
|
|
364
619
|
You can change where console output is streamed with `:output` option:
|
365
620
|
|
@@ -369,7 +624,7 @@ bar = TTY::ProgressBar.new(output: $stdout)
|
|
369
624
|
|
370
625
|
The output stream defaults to `stderr`.
|
371
626
|
|
372
|
-
### 3.
|
627
|
+
### 3.9 :frequency
|
373
628
|
|
374
629
|
Each time the `advance` is called it causes the progress bar to repaint. In cases when there is a huge number of updates per second, you may need to limit the rendering process by using the `frequency` option.
|
375
630
|
|
@@ -379,9 +634,9 @@ The `frequency` option accepts `integer` representing number of `Hz` units, for
|
|
379
634
|
TTY::ProgressBar.new("[:bar]", total: 30, frequency: 10) # 10 Hz
|
380
635
|
```
|
381
636
|
|
382
|
-
### 3.
|
637
|
+
### 3.10 :interval
|
383
638
|
|
384
|
-
|
639
|
+
Every time `advance` method is called, a time sample is taken for speed measurement. By default, all the samples are grouped in second intervals to provide a rate of speed. You can change this by passing the `interval` option.
|
385
640
|
|
386
641
|
The `interval` option is an `integer` that represents the number of seconds, for example, interval of `60` would mean that speed is measured per 1 minute.
|
387
642
|
|
@@ -391,6 +646,55 @@ TTY::ProgressBar.new(":rate/minute", total: 100, interval: 60) # 1 minute
|
|
391
646
|
TTY::ProgressBar.new(":rate/hour", total: 100, interval: 3600) # 1 hour
|
392
647
|
```
|
393
648
|
|
649
|
+
### 3.11 :hide_cursor
|
650
|
+
|
651
|
+
By default the cursor is visible during progress bar rendering. If you wish to hide it, you can do so with the `:hide_cursor` option.
|
652
|
+
|
653
|
+
Please note that hiding cursor changes user's terminal and you need to ensure that the cursor is made visible after your code finishes. This means also handling premature interrupt signals and other unpredictable events.
|
654
|
+
|
655
|
+
One solution is to wrap your progress rendering inside the `begin` and `ensure` like so:
|
656
|
+
|
657
|
+
```ruby
|
658
|
+
progress = TTY::ProgressBar.new("[:bar]", hide_cursor: true)
|
659
|
+
|
660
|
+
begin
|
661
|
+
# logic to advance progress bar
|
662
|
+
ensure
|
663
|
+
progress.stop # or progress.finish
|
664
|
+
# both methods will ensure that cursor is made visible again
|
665
|
+
end
|
666
|
+
```
|
667
|
+
|
668
|
+
### 3.12 :clear
|
669
|
+
|
670
|
+
By default, when a progress bar finishes it returns to a new line leaving the last progress output behind.
|
671
|
+
|
672
|
+
If you prefer to erase a progress bar when it is finished use `:clear` option:
|
673
|
+
|
674
|
+
```ruby
|
675
|
+
TTY::ProgressBar.new("[:bar]", clear: true)
|
676
|
+
```
|
677
|
+
|
678
|
+
### 3.13 :clear_head
|
679
|
+
|
680
|
+
When a progress bar finishes and its animation includes [:head](#35-head) character, the character will remain in the output:
|
681
|
+
|
682
|
+
```ruby
|
683
|
+
# [=============>]
|
684
|
+
```
|
685
|
+
|
686
|
+
To replace a head character when a progress bar is finished use `:clear_head` option:
|
687
|
+
|
688
|
+
```ruby
|
689
|
+
TTY::ProgressBar.new("[:bar]", clear_head: true)
|
690
|
+
```
|
691
|
+
|
692
|
+
This will result in the following output:
|
693
|
+
|
694
|
+
```ruby
|
695
|
+
# [==============]
|
696
|
+
```
|
697
|
+
|
394
698
|
## 4. Formatting
|
395
699
|
|
396
700
|
Every **TTY::ProgressBar** instance requires a format string, which apart from regular characters accepts special tokens to display dynamic information. For instance, a format to measure download progress could be:
|
@@ -410,38 +714,55 @@ These are the tokens that are currently supported:
|
|
410
714
|
* `:total_byte` the total progress in bytes
|
411
715
|
* `:percent` the completion percentage
|
412
716
|
* `:elapsed` the elapsed time in seconds
|
413
|
-
* `:eta` the
|
717
|
+
* `:eta` the estimated time to completion in seconds
|
718
|
+
* `:eta_time` the estimated time of day at completion
|
414
719
|
* `:rate` the current rate of progression per second
|
415
|
-
* `:byte_rate` the current rate of
|
720
|
+
* `:byte_rate` the current rate of progression in bytes per second
|
416
721
|
* `:mean_rate` the averaged rate of progression per second
|
417
722
|
* `:mean_byte` the averaged rate of progression in bytes per second
|
418
723
|
|
724
|
+
In the indeterminate mode, the progress bar displays `-` for tokens that cannot be calculated like `:total`, `:total_byte`, `:percent` and `:eta`. The following format:
|
725
|
+
|
726
|
+
```ruby
|
727
|
+
"[:bar] :current/:total :total_byte :percent ET::elapsed ETA::eta :rate/s"
|
728
|
+
```
|
729
|
+
|
730
|
+
Will result in:
|
731
|
+
|
732
|
+
```ruby
|
733
|
+
# [ <=> ] 23/- -B -% ET: 1s ETA:--s 18.01/s
|
734
|
+
```
|
735
|
+
|
419
736
|
### 4.2 Custom Formatters
|
420
737
|
|
421
738
|
If the provided tokens do not meet your needs, you can write your own formatter and instrument formatting pipeline to use a formatter you prefer. This option is preferred if you are going to rely on progress bar internal data such as `rate`, `current` etc. which will all be available on the passed in progress bar instance.
|
422
739
|
|
423
|
-
For example,
|
740
|
+
For example, let's say you want to add `:time` token. First, start by creating a custom formatter class called `TimeFormatter` that will dynamically update `:time` token in the formatted string. In order for the `TimeFormatter` to recognise the `:time` token, you'll need to include the `TTY::ProgressBar::Formatter` module with a regular expression matching the token like so:
|
424
741
|
|
425
742
|
```ruby
|
426
743
|
class TimeFormatter
|
427
|
-
|
428
|
-
|
429
|
-
|
744
|
+
include TTY::ProgressBar::Formatter[/:time/i]
|
745
|
+
...
|
746
|
+
end
|
747
|
+
```
|
430
748
|
|
431
|
-
|
432
|
-
|
433
|
-
|
749
|
+
Next, add `call` method that will substitute the matched token with an actual value. For example, to see the time elapsed since the start do:
|
750
|
+
|
751
|
+
```ruby
|
752
|
+
class TimeFormatter
|
753
|
+
include TTY::ProgressBar::Formatter[/:time/i]
|
434
754
|
|
435
|
-
def
|
436
|
-
|
437
|
-
|
755
|
+
def call(value) # specify how display string is formatted
|
756
|
+
# access current progress bar instance to read start time
|
757
|
+
elapsed = (Time.now - progress.start_time).to_s
|
758
|
+
value.gsub(matcher, elapsed) # replace :time token with a value
|
438
759
|
end
|
439
760
|
end
|
440
761
|
```
|
441
762
|
|
442
|
-
Notice that you have access to all the configuration options inside the formatter by simply invoking them on the
|
763
|
+
Notice that you have access to all the configuration options inside the formatter by simply invoking them on the `progress` instance.
|
443
764
|
|
444
|
-
Create **TTY::ProgressBar** instance
|
765
|
+
Create **TTY::ProgressBar** instance using the new token:
|
445
766
|
|
446
767
|
```ruby
|
447
768
|
bar = TTY::ProgressBar.new(":time", total: 30)
|
@@ -453,7 +774,7 @@ Then add `TimeFormatter` to the pipeline like so:
|
|
453
774
|
bar.use TimeFormatter
|
454
775
|
```
|
455
776
|
|
456
|
-
|
777
|
+
Then invoke progression:
|
457
778
|
|
458
779
|
```ruby
|
459
780
|
bar.advance
|
@@ -461,39 +782,47 @@ bar.advance
|
|
461
782
|
|
462
783
|
### 4.3 Custom Tokens
|
463
784
|
|
464
|
-
You can define custom tokens by passing pairs `name: value` to `advance` method in order to dynamically update formatted bar. This option is useful for lightweight content replacement such as titles that doesn't depend on the internal data of
|
785
|
+
You can define custom tokens by passing pairs `name: value` to `advance` method in order to dynamically update formatted bar. This option is useful for lightweight content replacement such as titles that doesn't depend on the internal data of a progress bar. For example:
|
465
786
|
|
466
787
|
```ruby
|
467
788
|
bar = TTY::ProgressBar.new("(:current) :title", total: 4)
|
468
|
-
bar.advance(title:
|
469
|
-
bar.advance(3, title:
|
789
|
+
bar.advance(title: "Hello Piotr!")
|
790
|
+
bar.advance(3, title: "Bye Piotr!")
|
470
791
|
```
|
471
792
|
|
472
|
-
|
793
|
+
This will output:
|
473
794
|
|
474
795
|
```ruby
|
475
|
-
(1) Hello Piotr!
|
476
|
-
(4) Bye Piotr!
|
796
|
+
# (1) Hello Piotr!
|
797
|
+
# (4) Bye Piotr!
|
477
798
|
```
|
478
799
|
|
479
800
|
### 4.4 Unicode
|
480
801
|
|
481
|
-
The format string as well as
|
802
|
+
The format string as well as [:complete](#33-complete), [:head](#35-head), [:incomplete](#34-incomplete) and [:unknown](#36-unknown) configuration options can contain Unicode characters that aren't monospaced.
|
482
803
|
|
483
804
|
For example, you can specify complete bar progression character to be Unicode non-monospaced:
|
484
805
|
|
485
806
|
```ruby
|
486
|
-
bar = TTY::ProgressBar.new("Unicode [:bar]", total: 30, complete:
|
487
|
-
|
488
|
-
|
807
|
+
bar = TTY::ProgressBar.new("Unicode [:bar]", total: 30, complete: "あ")
|
808
|
+
```
|
809
|
+
|
810
|
+
Advancing above progress bar to completion will fit `あ` characters in 30 terminal columns:
|
811
|
+
|
812
|
+
```ruby
|
813
|
+
# Unicode [あああああああああああああああ]
|
489
814
|
```
|
490
815
|
|
491
816
|
Similarly, the formatted string can include Unicode characters:
|
492
817
|
|
493
818
|
```ruby
|
494
819
|
bar = TTY::ProgressBar.new("あめかんむり[:bar]", total: 20)
|
495
|
-
|
496
|
-
|
820
|
+
```
|
821
|
+
|
822
|
+
A finished progress bar will also fit within allowed width:
|
823
|
+
|
824
|
+
```ruby
|
825
|
+
# あめかんむり[== ]
|
497
826
|
```
|
498
827
|
|
499
828
|
## 5. Logging
|
@@ -501,11 +830,11 @@ bar = TTY::ProgressBar.new("あめかんむり[:bar]", total: 20)
|
|
501
830
|
If you want to print messages out to terminal along with the progress bar use the `log` method. The messages will appear above the progress bar and will continue scrolling up as more are logged out.
|
502
831
|
|
503
832
|
```ruby
|
504
|
-
bar.log(
|
833
|
+
bar.log("Piotrrrrr")
|
505
834
|
bar.advance
|
506
835
|
```
|
507
836
|
|
508
|
-
|
837
|
+
This could result in the following output:
|
509
838
|
|
510
839
|
```ruby
|
511
840
|
# Piotrrrrr
|
@@ -516,14 +845,13 @@ will result in:
|
|
516
845
|
|
517
846
|
### 6.1 new
|
518
847
|
|
519
|
-
The multi progress bar can be created in two ways. If you simply want to group multiple progress bars you can create multi bar like so:
|
848
|
+
The multi progress bar can be created in two ways. If you simply want to group multiple progress bars together, you can create multi bar without a format string like so:
|
520
849
|
|
521
850
|
```ruby
|
522
851
|
TTY::ProgressBar::Multi.new
|
523
852
|
```
|
524
853
|
|
525
|
-
|
526
|
-
However, if you want a top level multibar that tracks all the registered progress bars then provide a formatted string:
|
854
|
+
However, if you want a top level multibar that tracks progress of all the registered progress bars then you need to provide a formatted string:
|
527
855
|
|
528
856
|
```ruby
|
529
857
|
TTY::ProgressBar::Multi.new("main [:bar] :percent")
|
@@ -538,15 +866,15 @@ multibar = TTY::ProgressBar::Multi.new
|
|
538
866
|
bar = multibar.register("[:bar]", total: 30)
|
539
867
|
```
|
540
868
|
|
541
|
-
The `register` call returns the newly created progress bar
|
869
|
+
The `register` call returns the newly created progress bar that can be changed using all the available [progress bar API](#2-ttyprogressbar-api) methods.
|
542
870
|
|
543
|
-
|
871
|
+
*Note:* Remember to specify total value for each registered progress bar, either when sending `register` message or when using `update` to dynamically assign the total value.
|
544
872
|
|
545
873
|
### 6.3 advance
|
546
874
|
|
547
875
|
Once multi progress bar has been created you can advance each registered progress bar individually, either by executing them one after the other synchronously or by placing them in separate threads thus progressing each bar asynchronously. The multi bar handles synchronization and display of all bars as they continue their respective rendering.
|
548
876
|
|
549
|
-
For example, to display two bars
|
877
|
+
For example, to display two bars asynchronously, first register them with the multi bar:
|
550
878
|
|
551
879
|
```ruby
|
552
880
|
bar1 = multibar.register("one [:bar]", total: 20)
|
@@ -568,7 +896,7 @@ Finally, wait for the threads to finish:
|
|
568
896
|
|
569
897
|
### 6.4 start
|
570
898
|
|
571
|
-
By default the top level multi bar will be rendered as the first bar and have its timer started when
|
899
|
+
By default the top level multi bar will be rendered as the first bar and have its timer started when one of the registered bars advances. However, if you wish to start timers and draw the top level multi bar do:
|
572
900
|
|
573
901
|
```ruby
|
574
902
|
multibar.start # => sets timer and draws top level multi progress bar
|
@@ -576,7 +904,7 @@ multibar.start # => sets timer and draws top level multi progress bar
|
|
576
904
|
|
577
905
|
### 6.5 finish
|
578
906
|
|
579
|
-
In order to finish all progress bars call `finish`. This will finish the top level progress bar, if it exists,
|
907
|
+
In order to finish all progress bars call `finish`. This will finish the top level progress bar, if it exists, and any registered progress bar still in progress.
|
580
908
|
|
581
909
|
```ruby
|
582
910
|
multibar.finish
|
@@ -590,7 +918,23 @@ Use `stop` to terminate immediately all progress bars registered with the multib
|
|
590
918
|
multibar.stop
|
591
919
|
```
|
592
920
|
|
593
|
-
### 6.7
|
921
|
+
### 6.7 pause
|
922
|
+
|
923
|
+
All running progress bars can be paused at their current positions using the `pause` method:
|
924
|
+
|
925
|
+
```ruby
|
926
|
+
multibar.pause
|
927
|
+
````
|
928
|
+
|
929
|
+
### 6.8 resume
|
930
|
+
|
931
|
+
When one or more registered progress bar is stopped or paused, they can be resumed all at once using the `resume` method:
|
932
|
+
|
933
|
+
```ruby
|
934
|
+
multibar.resume
|
935
|
+
```
|
936
|
+
|
937
|
+
### 6.9 complete?
|
594
938
|
|
595
939
|
To check if all registered progress bars have been successfully finished use `complete?`
|
596
940
|
|
@@ -598,7 +942,23 @@ To check if all registered progress bars have been successfully finished use `co
|
|
598
942
|
multibar.complete? # => true
|
599
943
|
```
|
600
944
|
|
601
|
-
### 6.
|
945
|
+
### 6.10 paused?
|
946
|
+
|
947
|
+
To check whether all progress bars are paused or not use `paused?`:
|
948
|
+
|
949
|
+
```ruby
|
950
|
+
multibar.paused? # => true
|
951
|
+
```
|
952
|
+
|
953
|
+
### 6.11 stopped?
|
954
|
+
|
955
|
+
To check whether all progress bars are stopped or not use `stopped?`:
|
956
|
+
|
957
|
+
```ruby
|
958
|
+
multibar.stopped? # => true
|
959
|
+
```
|
960
|
+
|
961
|
+
### 6.12 on
|
602
962
|
|
603
963
|
Similar to `TTY::ProgressBar` the multi bar fires events when it is progressing, stopped or finished. You can register to listen for events using the `on` message.
|
604
964
|
|
@@ -614,21 +974,27 @@ When all the registered progress bars finish and complete then the `:done` event
|
|
614
974
|
multibar.on(:done) { ... }
|
615
975
|
```
|
616
976
|
|
617
|
-
|
977
|
+
When any of the progress bars gets stopped the `:stopped` event is fired. You can listen for this event:
|
618
978
|
|
619
979
|
```ruby
|
620
980
|
multibar.on(:stopped) { ... }
|
621
981
|
```
|
622
982
|
|
623
|
-
|
983
|
+
Anytime a registered progress bar pauses, a `:paused` event will be fired. To listen for this event do:
|
984
|
+
|
985
|
+
```ruby
|
986
|
+
multibar.on(:paused) { ... }
|
987
|
+
```
|
988
|
+
|
989
|
+
### 6.13 :style
|
624
990
|
|
625
991
|
In addition to all [configuration options](#3-configuration) you can style multi progress bar:
|
626
992
|
|
627
993
|
```ruby
|
628
994
|
TTY::ProgressBar::Multi.new("[:bar]", style: {
|
629
|
-
top:
|
630
|
-
middle:
|
631
|
-
bottom:
|
995
|
+
top: ". ",
|
996
|
+
middle: "|-> ",
|
997
|
+
bottom: "|__ "
|
632
998
|
})
|
633
999
|
```
|
634
1000
|
|
@@ -641,7 +1007,7 @@ This section demonstrates some of the possible uses for the **TTY::ProgressBar**
|
|
641
1007
|
Creating a progress bar that displays in color is as simple as coloring the `:complete` and `:incomplete` character options. In order to help with coloring you can use [pastel](https://github.com/piotrmurach/pastel) library like so:
|
642
1008
|
|
643
1009
|
```ruby
|
644
|
-
require
|
1010
|
+
require "pastel"
|
645
1011
|
|
646
1012
|
pastel = Pastel.new
|
647
1013
|
green = pastel.on_green(" ")
|
@@ -694,6 +1060,10 @@ This will result in output similar to:
|
|
694
1060
|
|
695
1061
|
This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
696
1062
|
|
1063
|
+
## Code of Conduct
|
1064
|
+
|
1065
|
+
Everyone interacting in the TTY::ProgressBar project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/piotrmurach/tty-progressbar/blob/master/CODE_OF_CONDUCT.md).
|
1066
|
+
|
697
1067
|
## Copyright
|
698
1068
|
|
699
|
-
Copyright (c) 2014
|
1069
|
+
Copyright (c) 2014 Piotr Murach. See LICENSE for further details.
|