loading-indicator
Advanced tools
Comparing version 1.2.0 to 2.0.0
@@ -0,1 +1,11 @@ | ||
# v2.0.0 (2015/02/02) | ||
* Added tests. | ||
* Added code linter. | ||
* Removed dependency `log-output` (now uses `log-update`). | ||
* Removed dependency `object-assign`. | ||
* Locks Node.js to version `>=0.10.0`. | ||
* Changed the API completely with a more functional approach. | ||
* Added new API documentation. | ||
# v1.2.0 (2015/08/17) | ||
@@ -2,0 +12,0 @@ |
27
index.js
'use strict'; | ||
module.exports = require('./src/loading-indicator'); | ||
var logUpdate = require('log-update'); | ||
var presets = require('./presets'); | ||
function stop(timer, shouldKeepOutput) { | ||
if (!shouldKeepOutput) { | ||
logUpdate.clear(); | ||
} | ||
clearInterval(timer); | ||
} | ||
function start(text, options) { | ||
var delay = options && options.delay ? options.delay : 100; | ||
var frames = options && options.frames ? options.frames : presets.spinner; | ||
var render = options && options.render ? options.render : logUpdate; | ||
var frame = 0; | ||
return setInterval(function () { | ||
render(frames[frame = ++frame % frames.length] + (text ? (' ' + text) : '')); | ||
}, delay || 100); | ||
} | ||
module.exports = { | ||
start: start, | ||
stop: stop | ||
}; |
{ | ||
"name": "loading-indicator", | ||
"version": "1.2.0", | ||
"description": "Simple and customizable command line idle status indicator.", | ||
"version": "2.0.0", | ||
"description": "Simple and customizable command line idle status indicator", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node test.js" | ||
"lint": "xo", | ||
"test": "npm run lint && ava -v -s" | ||
}, | ||
"xo": { | ||
"space": true | ||
}, | ||
"engines": { | ||
"node": ">=0.10.0" | ||
}, | ||
"repository": { | ||
@@ -16,11 +23,11 @@ "type": "git", | ||
"command", | ||
"idle", | ||
"indicator", | ||
"line", | ||
"loading", | ||
"progress", | ||
"spin", | ||
"spinner", | ||
"loading", | ||
"indicator", | ||
"progress", | ||
"wait", | ||
"ui", | ||
"idle" | ||
"wait" | ||
], | ||
@@ -34,5 +41,8 @@ "author": "Rafael Rinaldi", | ||
"dependencies": { | ||
"log-output": "^2.0.1", | ||
"object-assign": "^3.0.0" | ||
"log-update": "^1.0.2" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
} | ||
} |
112
README.md
@@ -1,7 +0,7 @@ | ||
# loading-indicator [![Build Status](https://travis-ci.org/rafaelrinaldi/loading-indicator.svg?branch=master)](https://travis-ci.org/rafaelrinaldi/loading-indicator) | ||
# loading-indicator [![Build Status](https://semaphoreci.com/api/v1/projects/aa124d8d-865f-4a05-ac30-1b47c246ddbe/681927/badge.svg)](https://semaphoreci.com/rafaelrinaldi/loading-indicator) | ||
> Simple and customizable command line loading indicator. | ||
> Simple and customizable command line loading indicator | ||
## Install | ||
**Warning: `v2.0.0` is a complete rewrite of the previous implementation.** | ||
```sh | ||
@@ -13,76 +13,102 @@ $ npm install loading-indicator --save | ||
```javascript | ||
var LoadingIndicator = require('loading-indicator'); | ||
var spin = new LoadingIndicator(); | ||
```js | ||
import loading from 'loading-indicator'; | ||
spin.start(); | ||
// Initialize the loading animation and saves the timer id | ||
const timer = loading.start(); | ||
// 1500ms later, stop the loading animation passing along the timer id | ||
setTimeout(() => { | ||
loading.stop(timer); | ||
}, 1500); | ||
``` | ||
## API | ||
![demo-default](./demo-default.gif) | ||
### `new LoadingIndicator([options])` | ||
### Presets | ||
### `options` | ||
```js | ||
import loading from 'loading-indicator'; | ||
// Import available presets | ||
import presets from 'loading-indicator/presets'; | ||
Type: `object` | ||
// Use an available preset (or simply provides an array with your custom preset) | ||
const timer = loading.start(null, { | ||
frames: presets.arrows | ||
}); | ||
``` | ||
Available options. | ||
![demo-presets](./demo-presets.gif) | ||
#### `options.preset` | ||
### Custom text | ||
Type: `string` | ||
Default: `sticks` | ||
```js | ||
import loading from 'loading-indicator'; | ||
The visual preset you want. | ||
// Setup a custom loading text | ||
const timer = loading.start('Loading...'); | ||
``` | ||
Available values are: | ||
![demo-custom-text](./demo-custom-text.gif) | ||
* `sticks` | ||
* `circle` | ||
* `dots` | ||
* `bullets` | ||
* `arrows` | ||
## API | ||
#### `options.sequence` | ||
## `loading.start([text], [options])` | ||
Type: `string` or `array` | ||
Returns a `number` with the id that is used to reset the render interval (later referenced as _timer_). | ||
Custom animation sequence. This value will override `preset`. | ||
### `text` | ||
##### `options.delay` | ||
Type: `string` | ||
Default: `null` | ||
Text do append to the indicator symbol. | ||
### `options` | ||
Type: `object` | ||
Available options. | ||
#### `options.delay` | ||
Type: `number` _(milliseconds)_ | ||
Default: `125` | ||
Default: `100` | ||
Delay for the render to be triggered. | ||
##### `options.prefix` | ||
#### `options.frames` | ||
Type: `string` | ||
Default: empty string | ||
Type: `array` | ||
Default: [`presets.spinner`](./presets.js#L2) | ||
String to be added to the begining of the output. | ||
Frames for the loading animation sequence. | ||
##### `options.suffix` | ||
Available presets are: | ||
Type: `string` | ||
Default: empty string | ||
* [`spinner`](./presets.js#L2) (default) | ||
* [`circle`](./presets.js#3) | ||
* [`dots`](./presets.js#4) | ||
* [`bullets`](./presets.js#5) | ||
* [`arrows`](./presets.js#11) | ||
* [`clock`](./presets.js#19) | ||
String to be added to the end of the output. | ||
## `loading.stop(timer, [shouldKeepOutput])` | ||
#### `loadingIndicator.start()` | ||
### `timer` | ||
Start loading animation. | ||
_Required_ | ||
Type: `number` _(integer)_ | ||
#### `loadingIndicator.stop()` | ||
Id of the render function interval. | ||
Stop loading animation. | ||
### `shouldKeepOutput` | ||
#### `loadingIndicator.render()` | ||
Type: `boolean` | ||
Default: `false` | ||
Render animation to the stream. | ||
This method is called internally so you don't need to manually call it. | ||
Whether or not to keep the output when `loading.stop()` is called. | ||
## License | ||
MIT © [Rafael Rinaldi](http://rinaldi.io) | ||
MIT :copyright: [Rafael Rinaldi](http://rinaldi.io) |
81
test.js
@@ -1,11 +0,76 @@ | ||
'use strict'; | ||
/** | ||
* The idea is to use a dummy writable stream, save whatever our program outputs | ||
* to `process.stdout` and then finally compare if the results are different | ||
* from each other, which means the loading animation is working properly. | ||
**/ | ||
var LoadingIndicator = require('./'); | ||
var spin = new LoadingIndicator(); | ||
import Stream from 'stream'; | ||
import logUpdate from 'log-update'; | ||
import test from 'ava'; | ||
import loading from './'; | ||
spin.start(); | ||
/** | ||
* Note that we share the same stream instance, that's why tests must run | ||
* serially, otherwise output data will get messed up. | ||
**/ | ||
setTimeout(function() { | ||
spin.stop(); | ||
process.exit(); | ||
}, 3500); | ||
let output = []; | ||
const stream = new Stream.Duplex({ | ||
write(chunk, encoding, next) { | ||
output.push(chunk); | ||
next(); | ||
} | ||
}); | ||
/** | ||
* Little helper to hang the test for a given period of time through Promises. | ||
**/ | ||
const delay = ms => new Promise(resolve => setTimeout(resolve, ms)); | ||
const createLoadingIndicator = stream => { | ||
return loading.start('loading', { | ||
render: logUpdate.create(stream) | ||
}); | ||
}; | ||
/** | ||
* Reset dummy stream output data at each run. | ||
**/ | ||
test.beforeEach(() => output.length = 0); | ||
test('test for valid output', async t => { | ||
/** | ||
* Creates a new loading indicator passing a custom render fn. | ||
* In this case, the function simply returns a new `logUpdate()` that writes | ||
* to our dummy stream. | ||
**/ | ||
const timer = createLoadingIndicator(stream); | ||
await delay(1000); | ||
loading.stop(timer); | ||
t.not(output[0].toString(), output[1].toString()); | ||
t.not(output[1].toString(), output[2].toString()); | ||
t.not(output[2].toString(), output[3].toString()); | ||
}); | ||
test('test if timeout is properly disposed', t => { | ||
const timer = createLoadingIndicator(stream); | ||
loading.stop(timer); | ||
t.not(timer['0']); | ||
}); | ||
test('test if custom text is properly added', async t => { | ||
const timer = createLoadingIndicator(stream); | ||
await delay(1000); | ||
loading.stop(timer); | ||
const text = output[0].toString().slice(2).trim(); | ||
t.is(text, 'loading'); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
256433
1
16
128
114
2
1
+ Addedlog-update@^1.0.2
+ Addedansi-escapes@1.4.0(transitive)
+ Addedcli-cursor@1.0.2(transitive)
+ Addedexit-hook@1.1.1(transitive)
+ Addedlog-update@1.0.2(transitive)
+ Addedonetime@1.1.0(transitive)
+ Addedrestore-cursor@1.0.1(transitive)
- Removedlog-output@^2.0.1
- Removedobject-assign@^3.0.0
- Removedlog-output@2.0.2(transitive)
- Removedlog-output-node@1.0.1(transitive)
- Removedobject-assign@3.0.0(transitive)