Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork30
A terminal spinner for tasks that have non-deterministic time frame.
License
piotrmurach/tty-spinner
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
A terminal spinner for tasks that have non-deterministic time frame.
TTY::Spinner provides independent spinner component forTTY toolkit.
Add this line to your application's Gemfile:
gem"tty-spinner"
And then execute:
$ bundleOr install it yourself as:
$ gem install tty-spinnerTTY::Spinner by default uses:classic type of formatter and requires no parameters:
spinner=TTY::Spinner.new
In addition you can provide a message with:spinner token and format type you would like for the spinning display:
spinner=TTY::Spinner.new("[:spinner] Loading ...",format::pulse_2)spinner.auto_spin# Automatic animation with default intervalsleep(2)# Perform taskspinner.stop("Done!")# Stop animation
This would produce animation in your terminal:
⎺ Loading ...And when finished output:
_ Loading ... Done!UseTTY::Spinner::Multi to synchronize multiple spinners:
spinners=TTY::Spinner::Multi.new("[:spinner] top")sp1=spinners.register"[:spinner] one"# or sp1 = ::TTY::Spinner.new("[:spinner] one")# spinners.register sp1sp2=spinners.register"[:spinner] two"sp1.auto_spinsp2.auto_spinsleep(2)# Perform worksp1.successsp2.success
The spinners when done will display:
┌ [✔] top├── [✔] one└── [✔] twoFor more usage examples please seeexamples directory
The main workhorse of the spinner is thespin method.
Looping overspin method will animate a given spinner.
loopdospinner.spinend
To perform automatic spinning animation useauto_spin method like so:
spinner.auto_spin
The speed with which the spinning happens is determined by the:interval parameter. All the spinner formats have their default intervals specified (see).
After callingauto_spin you can pause spinner execution:
spinner.pause
You can continue any paused spinner:
spinner.resume
Userun passing a block with a job that will automatically display spinning animation while the block executes and finish animation when the block terminates. The block yields a spinner instance.
spinner.rundo |spinner| ...end
Optionally you can provide a stop message to display when animation is finished.
spinner.run("Done!")do |spinner| ...end
In order to set start time or reuse the same spinner after it has stopped, callstart method:
spinner.start
In order to stop the spinner callstop. This will finish drawing the spinning animation and return to new line.
spinner.stop
You can further pass a message to print when animation is finished.
spinner.stop("Done!")
Usesuccess call to stop the spinning animation and replace the spinning symbol with check mark character to indicate successful completion.
spinner=TTY::Spinner.new("[:spinner] Task name")spinner.success("(successful)")
This will produce:
[✔] Task name (successful)Useerror call to stop the spinning animation and replace the spinning symbol with cross character to indicate error completion.
spinner=TTY::Spinner.new("[:spinner] Task name")spinner.error("(error)")
This will produce:
[✖] Task name (error)Useupdate call to dynamically change label name(s).
Provide an arbitrary token name(s) in the message string, such as:title
spinner=TTY::Spinner.new("[:spinner] :title")
and then pass token name and value:
spinner.update(title:"Downloading file1")
next start animation:
spinner.run{ ...}# => | Downloading file1
Once animation finishes you can kick start another one with a different name:
spinner.update(title:"Downloading file2")spinner.run{ ...}
In order to reset the spinner to its initial frame do:
spinner.reset
One way to wait while the spinning animates is to join the thread started withstart method:
spinner.join
Optionally you can provide timeout:
spinner.join(0.5)
The spinner will not write any output if the output stream is not a TTY. You can check this with:
spinner.tty?
To output log messages to the console above a spinner use thelog method:
spinner.log("Print this log message to the console")
There are number of configuration options that can be provided to customise the behaviour of a spinner.
Use one of the predefined spinner styles by passing the formatting token:format
spinner=TTY::Spinner.new(format::pulse_2)
All spinner formats thatTTY::Spinner accepts are defined in/lib/tty/spinner/formats.rb
If you wish to see all available formats in action run theformats.rb file in examples folder like so:
bundleexecrubyexamples/formats.rb
If you wish to use custom formatting use the:frames option with eitherarray orstring of characters.
spinner=TTY::Spinner.new(frames:[".","o","0","@","*"])
The:interval option acceptsinteger representing number ofHz units, for instance, frequency of 10 will mean that the spinning animation will be displayed 10 times per second.
spinner=TTY::Spinner.new(interval:20)# 20 Hz (20 times per second)
Hides cursor when spinning animation performs. Defaults tofalse.
spinner=TTY::Spinner.new(hide_cursor:true)
After spinner is finished clears its output. Defaults tofalse.
spinner=TTY::Spinner.new(clear:true)
To change marker indicating successful completion use the:success_mark option:
spinner=TTY::Spinner.new(success_mark:"+")
To change marker indicating error completion use the:error_mark option:
spinner=TTY::Spinner.new(error_mark:"x")
The spinner only outputs to a console and when output is redirected to a file or a pipe it does nothing. This is so, for example, your error logs do not overflow with spinner output.
You can change where console output is streamed with:output option:
spinner=TTY::Spinner.new(output: $stdout)
The output stream defaults tostderr.
TTY::Spinner emits:done,:success and:error event types when spinner is stopped.
This event is emitted irrespective of the completion method. In order to listen for this event you need to register callback:
spinner.on(:done){ ...}
This event is fired whensuccess call is made. In order to respond to the event, you need to register callback:
spinner.on(:success){ ...}
This event is fired whenerror completion is called. In order to respond to the event, you need to register callback:
spinner.on(:error){ ...}
Create and register aTTY::Spinner under the multispinner
new_spinner=multi_spinner.register("[:spinner] Task 1 name",options)# or# spinner = ::TTY::Spinner.new("[:spinner] one")# sp1 = multi_spinner.register(spinner)
If no options are given it will use the options given to the multi_spinner when it was initialized to create the new spinner.If options are passed, they will override any options given to the multi spinner.
To create a top level spinner that tracks activity of all the registered spinners, the multispinner has to have been given a message on initialization:
multi_spinner=TTY::Spinner::Multi.new("[:spinner] Top level spinner")
The top level multi spinner will perform spinning animation automatically when at least one of the registered spinners starts spinning.
If you register spinners without any tasks then you will have to manually control when themulti_spinner finishes by callingstop,success orerror (seemanual).
Alternatively, you can register spinners with tasks that will automatically animate and finish spinners when respective tasks are done (seeasync tasks).
The speed with which the spinning happens is determined by the:interval parameter. All the spinner formats have their default intervals specified (see).
In case when you wish to have full control over multiple spinners, you will need to perform all actions manually.
For example, create a multi spinner that will track status of all registered spinners:
multi_spinner=TTY::Spinner::Multi.new("[:spinner] top")
and then register spinners with their formats:
spinner_1 = spinners.register "[:spinner] one"spinner_2 = spinners.register "[:spinner] two"Once registered, you can set spinners running in separate threads:
spinner_1.auto_spinspinner_2.auto_spin
Finally, you need to stop each spinner manually, in our case we mark the second spinner as failure which in turn will stop the top level multi spinner automatically and mark it as failure:
spinner_1.successspinner_2.error
The result may look like this:
┌[✖]top├──[✔]one└──[✖]two
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.
For example, create a multi spinner that will track status of all registered spinners:
multi_spinner=TTY::Spinner::Multi.new("[:spinner] top")
and then register spinners with their respective tasks:
multi_spinner.register("[:spinner] one"){ |sp|sleep(2);sp.success("yes 2")}multi_spinner.register("[:spinner] two"){ |sp|sleep(3);sp.error("no 2")}
Finally, call#auto_spin to kick things off:
multi_spinner.auto_spin
If any of the child spinner stops with error then the top level spinner will be marked as failure.
In order to stop the multi spinner callstop. This will stop the top level spinner, if it exists, and any sub-spinners still spinning.
multi_spinner.stop
Usesuccess call to stop the spinning animation and replace the spinning symbol with a check mark character to indicate successful completion.This will also call#success on any sub-spinners that are still spinning.
multi_spinner.success
Useerror call to stop the spinning animation and replace the spinning symbol with cross character to indicate error completion.This will also call#error on any sub-spinners that are still spinning.
multi_spinner.error
In addition to allconfiguration options you can style multi spinner like so:
multi_spinner=TTY::Spinner::Multi.new("[:spinner] parent",style:{top:". "middle:"|-> "bottom:"|__ "})
Bug reports and pull requests are welcome on GitHub athttps://github.com/piotrmurach/tty-spinner. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to thecode of conduct.
- Fork it (https://github.com/piotrmurach/tty-spinner/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Everyone interacting in the TTY::Spinner project's codebases, issue trackers, chat rooms and mailing lists is expected to follow thecode of conduct.
Copyright (c) 2014 Piotr Murach. See LICENSE for further details.
About
A terminal spinner for tasks that have non-deterministic time frame.
Topics
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Contributors13
Uh oh!
There was an error while loading.Please reload this page.

