austb-tty-spinner 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +475 -0
- data/lib/tty-spinner.rb +4 -0
- data/lib/tty/spinner.rb +512 -0
- data/lib/tty/spinner/formats.rb +127 -0
- data/lib/tty/spinner/multi.rb +259 -0
- data/lib/tty/spinner/version.rb +7 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 22b8d9983b313e81df5d868d8f795882535a36a5
|
4
|
+
data.tar.gz: e1f0abb9a072ba253db4b945114a3527c8268355
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 85d61253e2de6234fafc27c540085d5cfb2fb34df77001ebef21a19893274436cbb1bbb4f0bb6bf7fad6e83ec2322a6b99a46666d6d93e133eda4dc5030d2572
|
7
|
+
data.tar.gz: 4894459f658facb838c3182a41a6a2f41f8480d7b261af37ce0af58a9693330ee0e09578573b810caa05a6bcb2c016c6ae2d176e1fa7d0b9ef894cc8cd09ffd1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Piotr Murach
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,475 @@
|
|
1
|
+
# TTY::Spinner [![Gitter](https://badges.gitter.im/Join%20Chat.svg)][gitter]
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/tty-spinner.svg)][gem]
|
3
|
+
[![Build Status](https://secure.travis-ci.org/piotrmurach/tty-spinner.svg?branch=master)][travis]
|
4
|
+
[![Build status](https://ci.appveyor.com/api/projects/status/2i5lx3tvyi5l8x3j?svg=true)][appveyor]
|
5
|
+
[![Code Climate](https://codeclimate.com/github/piotrmurach/tty-spinner/badges/gpa.png)][codeclimate]
|
6
|
+
[![Coverage Status](https://coveralls.io/repos/piotrmurach/tty-spinner/badge.svg)][coverage]
|
7
|
+
[![Inline docs](http://inch-ci.org/github/piotrmurach/tty-spinner.svg?branch=master)][inchpages]
|
8
|
+
|
9
|
+
[gitter]: https://gitter.im/piotrmurach/tty
|
10
|
+
[gem]: http://badge.fury.io/rb/tty-spinner
|
11
|
+
[travis]: http://travis-ci.org/piotrmurach/tty-spinner
|
12
|
+
[appveyor]: https://ci.appveyor.com/project/piotrmurach/tty-spinner
|
13
|
+
[codeclimate]: https://codeclimate.com/github/piotrmurach/tty-spinner
|
14
|
+
[coverage]: https://coveralls.io/r/piotrmurach/tty-spinner
|
15
|
+
[inchpages]: http://inch-ci.org/github/piotrmurach/tty-spinner
|
16
|
+
|
17
|
+
> A terminal spinner for tasks that have non-deterministic time frame.
|
18
|
+
|
19
|
+
**TTY::Spinner** provides independent spinner component for [TTY](https://github.com/piotrmurach/tty) toolkit.
|
20
|
+
|
21
|
+
![](demo.gif)
|
22
|
+
|
23
|
+
## Installation
|
24
|
+
|
25
|
+
Add this line to your application's Gemfile:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
gem 'tty-spinner'
|
29
|
+
```
|
30
|
+
|
31
|
+
And then execute:
|
32
|
+
|
33
|
+
$ bundle
|
34
|
+
|
35
|
+
Or install it yourself as:
|
36
|
+
|
37
|
+
$ gem install tty-spinner
|
38
|
+
|
39
|
+
## Contents
|
40
|
+
|
41
|
+
* [1. Usage](#1-usage)
|
42
|
+
* [2. TTY::Spinner API](#2-ttyspinner-api)
|
43
|
+
* [2.1 spin](#21-spin)
|
44
|
+
* [2.2 auto_spin](#22-auto_spin)
|
45
|
+
* [2.3 run](#23-run)
|
46
|
+
* [2.4 start](#24-start)
|
47
|
+
* [2.5 stop](#25-stop)
|
48
|
+
* [2.5.1 success](#251-success)
|
49
|
+
* [2.5.2 error](#252-error)
|
50
|
+
* [2.6 update](#26-update)
|
51
|
+
* [2.7 reset](#27-reset)
|
52
|
+
* [2.8 join](#28-join)
|
53
|
+
* [3. Configuration](#3-configuration)
|
54
|
+
* [3.1 :format](#31-format)
|
55
|
+
* [3.2 :frames](#32-frames)
|
56
|
+
* [3.3 :interval](#33-interval)
|
57
|
+
* [3.4 :hide_cursor](#34-hide_cursor)
|
58
|
+
* [3.5 :clear](#35-clear)
|
59
|
+
* [3.6 :success_mark](#36-success_mark)
|
60
|
+
* [3.7 :error_mark](#37-error_mark)
|
61
|
+
* [3.8 :output](#38-output)
|
62
|
+
* [4. Events](#4-events)
|
63
|
+
* [4.1 done](#41-done)
|
64
|
+
* [4.2 success](#42-success)
|
65
|
+
* [4.3 error](#43-error)
|
66
|
+
* [5. TTY::Spinner::Multi API](#5-ttyspinnermulti-api)
|
67
|
+
* [5.1 reigster](#51-register)
|
68
|
+
* [5.2 auto_spin](#52-auto_spin)
|
69
|
+
* [5.2.1 async tasks](#521-async-tasks)
|
70
|
+
* [5.3 stop](#53-stop)
|
71
|
+
* [5.3.1 success](#531-success)
|
72
|
+
* [5.3.2 error](#532-error)
|
73
|
+
* [5.4 :style](#54-style)
|
74
|
+
|
75
|
+
## 1. Usage
|
76
|
+
|
77
|
+
**TTY::Spinner** by default uses `:classic` type of formatter and requires no paramters:
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
spinner = TTY::Spinner.new
|
81
|
+
```
|
82
|
+
|
83
|
+
In addition you can provide a message with `:spinner` token and format type you would like for the spinning display:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
spinner = TTY::Spinner.new("[:spinner] Loading ...", format: :pulse_2)
|
87
|
+
|
88
|
+
spinner.auto_spin # Automatic animation with default interval
|
89
|
+
|
90
|
+
sleep(2) # Perform task
|
91
|
+
|
92
|
+
spinner.stop('Done!') # Stop animation
|
93
|
+
```
|
94
|
+
|
95
|
+
This would produce animation in your terminal:
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
⎺ Loading ...
|
99
|
+
```
|
100
|
+
|
101
|
+
and when finished output:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
_ Loading ... Done!
|
105
|
+
```
|
106
|
+
|
107
|
+
Use **TTY::Spinner::Multi** to synchornize multiple spinners:
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
spinners = TTY::Spinner::Multi.new("[:spinner] top")
|
111
|
+
|
112
|
+
sp1 = spinners.register "[:spinner] one"
|
113
|
+
sp2 = spinners.register "[:spinner] two"
|
114
|
+
|
115
|
+
sp1.auto_spin
|
116
|
+
sp2.auto_spin
|
117
|
+
|
118
|
+
sleep(2) # Perform work
|
119
|
+
|
120
|
+
sp1.success
|
121
|
+
sp2.success
|
122
|
+
```
|
123
|
+
|
124
|
+
which when done will display:
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
┌[✔]] top
|
128
|
+
├──[✔] one
|
129
|
+
└──[✔] two
|
130
|
+
```
|
131
|
+
|
132
|
+
For more usage examples please see [examples directory](https://github.com/piotrmurach/tty-spinner/tree/master/examples)
|
133
|
+
|
134
|
+
## 2. TTY::Spinner API
|
135
|
+
|
136
|
+
### 2.1 spin
|
137
|
+
|
138
|
+
The main workhorse of the spinner is the `spin` method.
|
139
|
+
|
140
|
+
Looping over `spin` method will animate a given spinner.
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
loop do
|
144
|
+
spinner.spin
|
145
|
+
end
|
146
|
+
```
|
147
|
+
|
148
|
+
### 2.2 auto_spin
|
149
|
+
|
150
|
+
To perform automatic spinning animation use `auto_spin` method like so:
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
spinner.auto_spin
|
154
|
+
```
|
155
|
+
|
156
|
+
The speed with which the spinning happens is determined by the `:interval` parameter. All the spinner formats have their default intervals specified ([see](https://github.com/piotrmurach/tty-spinner/blob/master/lib/tty/spinner/formats.rb)).
|
157
|
+
|
158
|
+
### 2.3 run
|
159
|
+
|
160
|
+
Use `run` with a code block that will automatically display spinning animation while the block executes and finish animation when the block terminates. Optionally you can provide a stop message to display when animation is finished.
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
spinner.run('Done!') { ... }
|
164
|
+
```
|
165
|
+
|
166
|
+
### 2.4 start
|
167
|
+
|
168
|
+
In order to set start time or reuse the same spinner after it has stopped, call `start` method:
|
169
|
+
|
170
|
+
```ruby
|
171
|
+
spinner.start
|
172
|
+
```
|
173
|
+
|
174
|
+
### 2.5 stop
|
175
|
+
|
176
|
+
In order to stop the spinner call `stop`. This will finish drawing the spinning animation and return to new line.
|
177
|
+
|
178
|
+
```ruby
|
179
|
+
spinner.stop
|
180
|
+
```
|
181
|
+
|
182
|
+
You can further pass a message to print when animation is finished.
|
183
|
+
|
184
|
+
```ruby
|
185
|
+
spinner.stop('Done!')
|
186
|
+
```
|
187
|
+
|
188
|
+
#### 2.5.1 success
|
189
|
+
|
190
|
+
Use `success` call to stop the spinning animation and replace the spinning symbol with checkmark character to indicate successful completion.
|
191
|
+
|
192
|
+
```ruby
|
193
|
+
spinner = TTY::Spinner.new("[:spinner] Task name")
|
194
|
+
spinner.success('(successful)')
|
195
|
+
```
|
196
|
+
|
197
|
+
This will produce:
|
198
|
+
|
199
|
+
```
|
200
|
+
[✔] Task name (successful)
|
201
|
+
```
|
202
|
+
|
203
|
+
#### 2.5.2 error
|
204
|
+
|
205
|
+
Use `error` call to stop the spining animation and replace the spinning symbol with cross character to indicate error completion.
|
206
|
+
|
207
|
+
```ruby
|
208
|
+
spinner = TTY::Spinner.new("[:spinner] Task name")
|
209
|
+
spinner.error('(error)')
|
210
|
+
```
|
211
|
+
|
212
|
+
This will produce:
|
213
|
+
|
214
|
+
```ruby
|
215
|
+
[✖] Task name (error)
|
216
|
+
```
|
217
|
+
|
218
|
+
|
219
|
+
### 2.6 update
|
220
|
+
|
221
|
+
Use `update` call to dynamically change label name(s).
|
222
|
+
|
223
|
+
Provide an arbitrary token name(s) in the message string, such as `:title`
|
224
|
+
|
225
|
+
```ruby
|
226
|
+
spinner = TTY::Spinner.new("[:spinner] :title")
|
227
|
+
```
|
228
|
+
|
229
|
+
and then pass token name and value:
|
230
|
+
|
231
|
+
```ruby
|
232
|
+
spinner.update(title: 'Downloading file1')
|
233
|
+
```
|
234
|
+
|
235
|
+
next start animation:
|
236
|
+
|
237
|
+
```ruby
|
238
|
+
spinner.run { ... }
|
239
|
+
# => | Downloading file1
|
240
|
+
```
|
241
|
+
|
242
|
+
Once animation finishes you can kick start another one with a different name:
|
243
|
+
|
244
|
+
```ruby
|
245
|
+
spinner.update(title: 'Downloading file2')
|
246
|
+
spinner.run { ... }
|
247
|
+
```
|
248
|
+
|
249
|
+
### 2.7 reset
|
250
|
+
|
251
|
+
In order to reset the spinner to its initial frame do:
|
252
|
+
|
253
|
+
```ruby
|
254
|
+
spinner.reset
|
255
|
+
```
|
256
|
+
|
257
|
+
### 2.8 join
|
258
|
+
|
259
|
+
One way to wait while the spinning animates is to join the thread started with `start` method:
|
260
|
+
|
261
|
+
```ruby
|
262
|
+
spinner.join
|
263
|
+
```
|
264
|
+
|
265
|
+
Optionally you can provide timeout:
|
266
|
+
|
267
|
+
```ruby
|
268
|
+
spinner.join(0.5)
|
269
|
+
```
|
270
|
+
|
271
|
+
## 3. Configuration
|
272
|
+
|
273
|
+
There are number of configuration options that can be provided to customise the behaviour of a spinner.
|
274
|
+
|
275
|
+
### 3.1 :format
|
276
|
+
|
277
|
+
Use one of the predefined spinner styles by passing the formatting token `:format`
|
278
|
+
|
279
|
+
```ruby
|
280
|
+
spinner = TTY::Spinner.new(format: :pulse_2)
|
281
|
+
```
|
282
|
+
|
283
|
+
All spinner formats that **TTY::Spinner** accepts are defined in [/lib/tty/spinner/formats.rb](https://github.com/piotrmurach/tty-spinner/blob/master/lib/tty/spinner/formats.rb)
|
284
|
+
|
285
|
+
If you wish to see all available formats in action run the `formats.rb` file in examples folder like so:
|
286
|
+
|
287
|
+
```ruby
|
288
|
+
bundle exec ruby examples/formats.rb
|
289
|
+
```
|
290
|
+
|
291
|
+
### 3.2 :frames
|
292
|
+
|
293
|
+
If you wish to use custom formatting use the `:frames` option with either `array` or `string` of characters.
|
294
|
+
|
295
|
+
```ruby
|
296
|
+
spinner = TTY::Spinner.new(frames: [".", "o", "0", "@", "*"])
|
297
|
+
```
|
298
|
+
|
299
|
+
### 3.3 :interval
|
300
|
+
|
301
|
+
The `:interval` option accepts `integer` representing number of `Hz` units, for instance, frequency of 10 will mean that the spinning animation will be displayed 10 times per second.
|
302
|
+
|
303
|
+
```ruby
|
304
|
+
spinner = TTY::Spinner.new(interval: 20) # 20 Hz (20 times per second)
|
305
|
+
```
|
306
|
+
|
307
|
+
### 3.4 :hide_cursor
|
308
|
+
|
309
|
+
Hides cursor when spinning animation performs. Defaults to `false`.
|
310
|
+
|
311
|
+
```ruby
|
312
|
+
spinner = TTY::Spinner.new(hide_cursor: true)
|
313
|
+
```
|
314
|
+
|
315
|
+
### 3.5 :clear
|
316
|
+
|
317
|
+
After spinner is finished clears its output. Defaults to `false`.
|
318
|
+
|
319
|
+
```ruby
|
320
|
+
spinner = TTY::Spinner.new(clear: true)
|
321
|
+
```
|
322
|
+
|
323
|
+
### 3.6 :success_mark
|
324
|
+
|
325
|
+
To change marker indicating successful completion use the `:success_mark` option:
|
326
|
+
|
327
|
+
```ruby
|
328
|
+
spinner = TTY::Spinner.new(success_mark: '+')
|
329
|
+
```
|
330
|
+
|
331
|
+
### 3.7 :error_mark
|
332
|
+
|
333
|
+
To change marker indicating error completion use the `:error_mark` option:
|
334
|
+
|
335
|
+
```ruby
|
336
|
+
spinner = TTY::Spinner.new(error_mark: 'x')
|
337
|
+
```
|
338
|
+
|
339
|
+
### 3.8 :output
|
340
|
+
|
341
|
+
To change where data is streamed use `:output` option like so:
|
342
|
+
|
343
|
+
```
|
344
|
+
spinner = TTY::Spinner.new(output: $stdout)
|
345
|
+
```
|
346
|
+
|
347
|
+
The output stream defaults to `stderr`.
|
348
|
+
|
349
|
+
## 4. Events
|
350
|
+
|
351
|
+
**TTY::Spinner** emits `:done`, `:success` and `:error` event types when spinner is stopped.
|
352
|
+
|
353
|
+
### 4.1 done
|
354
|
+
|
355
|
+
This event is emitted irrespective of the completion method. In order to listen for this event you need to register callback:
|
356
|
+
|
357
|
+
```ruby
|
358
|
+
spinner.on(:done) { ... }
|
359
|
+
```
|
360
|
+
|
361
|
+
### 4.2 success
|
362
|
+
|
363
|
+
This event is fired when `success` call is made. In order to respond to the event, you need to register callback:
|
364
|
+
|
365
|
+
```ruby
|
366
|
+
spinner.on(:success) { ... }
|
367
|
+
```
|
368
|
+
|
369
|
+
### 4.3 error
|
370
|
+
|
371
|
+
This event is fired when `error` completion is called. In order to respond to the event, you need to register callback:
|
372
|
+
|
373
|
+
```ruby
|
374
|
+
spinner.on(:error) { ... }
|
375
|
+
```
|
376
|
+
|
377
|
+
## 5. TTY::Spinner::Multi API
|
378
|
+
|
379
|
+
### 5.1 register
|
380
|
+
|
381
|
+
Create and register a `TTY::Spinner` under the multispinner
|
382
|
+
|
383
|
+
```ruby
|
384
|
+
new_spinner = multi_spinner.register("[:spinner] Task 1 name", options)
|
385
|
+
```
|
386
|
+
|
387
|
+
If no options are given it will use the options given to the multi_spinner when it was initialized to create the new spinner.
|
388
|
+
If options are passed, they will override any options given to the multi spinner.
|
389
|
+
|
390
|
+
### 5.2 auto_spin
|
391
|
+
|
392
|
+
The multispinner has to have been given a message on initialization.
|
393
|
+
To perform automatic spinning animation use `auto_spin` method like so:
|
394
|
+
|
395
|
+
```ruby
|
396
|
+
multi_spinner = TTY::Spinner::Multi.new("[:spinner] Top level spinner")
|
397
|
+
multi_spinner.auto_spin
|
398
|
+
```
|
399
|
+
|
400
|
+
If you register spinners without any tasks then you will have to manually control when the `multi_spinner` finishes by calling `stop`, `success` or `error`. Alternatively you can register spinners with tasks (see [async tasks](#521-async-tasks))
|
401
|
+
|
402
|
+
The speed with which the spinning happens is determined by the `:interval` parameter. All the spinner formats have their default intervals specified ([see](https://github.com/piotrmurach/tty-spinner/blob/master/lib/tty/spinner/formats.rb)).
|
403
|
+
|
404
|
+
#### 5.2.1 async tasks
|
405
|
+
|
406
|
+
In case when you wish to execute async tasks and update individual spinners automatically, in any order, about their task status use `#register` and pass additional block parameter with the job to be executed.
|
407
|
+
|
408
|
+
For example, create a multi spinner that will track status of all registered spinners:
|
409
|
+
|
410
|
+
```ruby
|
411
|
+
spinners = TTY::Spinner::Multi.new("[:spinner] top")
|
412
|
+
```
|
413
|
+
|
414
|
+
and then register spinners with their respective tasks:
|
415
|
+
|
416
|
+
```ruby
|
417
|
+
spinners.register("[:spinner] one") { |sp| sleep(2); sp.success('yes 2') }
|
418
|
+
spinners.register("[:spinner] two") { |sp| sleep(3); sp.error('no 2') }
|
419
|
+
```
|
420
|
+
|
421
|
+
Finally, call `#auto_spin` to kick things off:
|
422
|
+
|
423
|
+
```ruby
|
424
|
+
spinners.auto_spin
|
425
|
+
```
|
426
|
+
|
427
|
+
### 5.3 stop
|
428
|
+
|
429
|
+
In order to stop the multi spinner call `stop`. This will stop the top level spinner, if it exists, and any sub-spinners still spinning.
|
430
|
+
|
431
|
+
```ruby
|
432
|
+
multi_spinner.stop
|
433
|
+
```
|
434
|
+
|
435
|
+
#### 5.3.1 success
|
436
|
+
|
437
|
+
Use `success` call to stop the spinning animation and replace the spinning symbol with checkmark character to indicate successful completion.
|
438
|
+
This will also call `#success` on any sub-spinners that are still spinning.
|
439
|
+
|
440
|
+
```ruby
|
441
|
+
multi_spinner.success
|
442
|
+
```
|
443
|
+
|
444
|
+
#### 5.3.2 error
|
445
|
+
|
446
|
+
Use `error` call to stop the spining animation and replace the spinning symbol with cross character to indicate error completion.
|
447
|
+
This will also call `#error` on any sub-spinners that are still spinning.
|
448
|
+
|
449
|
+
```ruby
|
450
|
+
multi_spinner.error
|
451
|
+
```
|
452
|
+
|
453
|
+
### 5.4 :style
|
454
|
+
|
455
|
+
In addition to all [configuration options](#3-configuration) you can style multi spinner like so:
|
456
|
+
|
457
|
+
```ruby
|
458
|
+
multi_spinner = TTY::Spinner::Multi.new("[:spinner] parent", style: {
|
459
|
+
top: '. '
|
460
|
+
middle: '|-> '
|
461
|
+
bottom: '|__ '
|
462
|
+
})
|
463
|
+
```
|
464
|
+
|
465
|
+
## Contributing
|
466
|
+
|
467
|
+
1. Fork it ( https://github.com/piotrmurach/tty-spinner/fork )
|
468
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
469
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
470
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
471
|
+
5. Create a new Pull Request
|
472
|
+
|
473
|
+
## Copyright
|
474
|
+
|
475
|
+
Copyright (c) 2014-2017 Piotr Murach. See LICENSE for further details.
|