Skip to content

Latest commit

 

History

History
137 lines (95 loc) · 4.6 KB

README.md

File metadata and controls

137 lines (95 loc) · 4.6 KB

npm version Build Status Coverage Status Code Climate Inch CI

Dependency Status devDependency Status

text-spinner

Spinning progress indicator for console applications

Example app1.js

If you have different needs regarding the functionality, please add a feature request.

Installation

npm install --save text-spinner

Usage

// Require the module and set options
var spinner = require('../')({ 
  interval: 100  // default value 
}); 

// Update spinner one time
spinner.spin(); 

Options:

  • interval- minimum interval to print the spinner; if spinner.spin() is called several times during interval, spinner will be updated only once. Default: 100 (ms).
  • prefix - prefix to print before the spinner. Default: ''. You may set it to '\x1B['+column+'G' to position cursor to specific column.
  • postfix - postfix to print after the spinner. Default: \x1B[0G (Move to start of line).
  • auto - automatically rotate spinner (i.e.without calling progress/rotate methods
  • spinner - spinner text definition. You may look to examples/app4.js for the examples.

Methods:

  • progress() - rotate and print spinner, but not frequently than options.interval
  • spin() - same as progress()
  • rotate([index]) - rotate and print spinner now, not checking options.interval. if index is set, then set index of
  • print() - print spinner now at current position
  • start() - start auto rotation
  • stop() - stop auto rotation

Properties:

  • active - status of auto rotation for spinner

You may change the look of the spinner by using spinner option:

Example app4.js

Examples

Example 1 (examples/app1.js)

Simplest example.

var spinner = require('../')();

setInterval(function() {
  spinner.spin();
}, 10); // Spinner is triggered every 10ms, but output is refreshed with 100ms (value of options.interval parameter)

Example 2 (examples/app2.js)

This is minimal example to show progress for file download.

var request = require('request');
var spinner = require('text-spinner')({ interval: 100 });

var url = 'http://mirror.internode.on.net/pub/test/5meg.test1'; // 5 MB File

console.log('* Downloading test file...');

request
  .get(url)
  .on('data', spinner.spin)
;

Example 3 (examples/app3.js)

Extended version of previous example.

var request = require('request');
var spinner = require('text-spinner')({ interval: 100 });

var url = 'http://mirror.internode.on.net/pub/test/5meg.test1'; // 5 MB File

console.log('* Downloading test file...');

request
  .get(url)
  .on('response', function (res) {
    var contentLength = parseInt(res.headers[ 'content-length' ]);
    console.log('* Download started, size: ' + (contentLength ? contentLength + ' bytes' : 'unknown') + '.'); })
  .on('data',     function (chunk) {
    spinner.spin();
  })
  .on('end',      function() {
    console.log('* Download finished.');
  })
  .on('error',      function(err) {
    console.log('* ERROR:', err);
  })
;

Example 4 (examples/app4.js)

Example 4 shows how to customize outlook for the spinner. Please, refer to source of examples/app4.js for more info.

Credits

Alexander

Links to package pages:

github.com   npmjs.com   travis-ci.org   coveralls.io   inch-ci.org

License

MIT