Comparing version 0.2.3 to 0.3.0
147
index.js
'use strict'; | ||
var chalk = require('chalk'); | ||
var cliCursor = require('cli-cursor'); | ||
var cliSpinners = require('cli-spinners'); | ||
var objectAssign = require('object-assign'); | ||
const chalk = require('chalk'); | ||
const cliCursor = require('cli-cursor'); | ||
const cliSpinners = require('cli-spinners'); | ||
const logSymbols = require('log-symbols'); | ||
function Ora(options) { | ||
if (!(this instanceof Ora)) { | ||
return new Ora(options); | ||
} | ||
class Ora { | ||
constructor(options) { | ||
if (typeof options === 'string') { | ||
options = { | ||
text: options | ||
}; | ||
} | ||
if (typeof options === 'string') { | ||
options = { | ||
text: options | ||
}; | ||
} | ||
this.options = Object.assign({ | ||
text: '', | ||
color: 'cyan', | ||
stream: process.stderr | ||
}, options); | ||
this.options = objectAssign({ | ||
text: '', | ||
color: 'cyan', | ||
stream: process.stderr | ||
}, options); | ||
const sp = this.options.spinner; | ||
this.spinner = typeof sp === 'object' ? sp : (process.platform === 'win32' ? cliSpinners.line : (cliSpinners[sp] || cliSpinners.dots)); // eslint-disable-line no-nested-ternary | ||
var sp = this.options.spinner; | ||
this.spinner = typeof sp === 'object' ? sp : (process.platform === 'win32' ? cliSpinners.line : (cliSpinners[sp] || cliSpinners.dots)); // eslint-disable-line | ||
if (this.spinner.frames === undefined) { | ||
throw new Error('Spinner must define `frames`'); | ||
} | ||
if (this.spinner.frames === undefined) { | ||
throw new Error('Spinner must define `frames`'); | ||
this.text = this.options.text; | ||
this.color = this.options.color; | ||
this.interval = this.options.interval || this.spinner.interval || 100; | ||
this.stream = this.options.stream; | ||
this.id = null; | ||
this.frameIndex = 0; | ||
this.enabled = this.options.enabled || ((this.stream && this.stream.isTTY) && !process.env.CI); | ||
} | ||
frame() { | ||
const frames = this.spinner.frames; | ||
let frame = frames[this.frameIndex]; | ||
this.text = this.options.text; | ||
this.color = this.options.color; | ||
this.interval = this.options.interval || this.spinner.interval || 100; | ||
this.stream = this.options.stream; | ||
this.id = null; | ||
this.frameIndex = 0; | ||
this.enabled = this.options.enabled || ((this.stream && this.stream.isTTY) && !process.env.CI); | ||
} | ||
if (this.color) { | ||
frame = chalk[this.color](frame); | ||
} | ||
Ora.prototype.frame = function () { | ||
var frames = this.spinner.frames; | ||
var frame = frames[this.frameIndex]; | ||
this.frameIndex = ++this.frameIndex % frames.length; | ||
if (this.color) { | ||
frame = chalk[this.color](frame); | ||
return frame + ' ' + this.text; | ||
} | ||
clear() { | ||
if (!this.enabled) { | ||
return this; | ||
} | ||
this.frameIndex = ++this.frameIndex % frames.length; | ||
this.stream.clearLine(); | ||
this.stream.cursorTo(0); | ||
return frame + ' ' + this.text; | ||
}; | ||
return this; | ||
} | ||
render() { | ||
this.clear(); | ||
this.stream.write(this.frame()); | ||
Ora.prototype.clear = function () { | ||
if (!this.enabled) { | ||
return this; | ||
} | ||
start() { | ||
if (!this.enabled || this.id) { | ||
return this; | ||
} | ||
this.stream.clearLine(); | ||
this.stream.cursorTo(0); | ||
cliCursor.hide(); | ||
this.render(); | ||
this.id = setInterval(this.render.bind(this), this.interval); | ||
return this; | ||
}; | ||
return this; | ||
} | ||
stop() { | ||
if (!this.enabled) { | ||
return this; | ||
} | ||
Ora.prototype.render = function () { | ||
this.clear(); | ||
this.stream.write(this.frame()); | ||
clearInterval(this.id); | ||
this.id = null; | ||
this.frameIndex = 0; | ||
this.clear(); | ||
cliCursor.show(); | ||
return this; | ||
}; | ||
Ora.prototype.start = function () { | ||
if (!this.enabled || this.id) { | ||
return this; | ||
} | ||
succeed() { | ||
return this.stopAndPersist(logSymbols.success); | ||
} | ||
fail() { | ||
return this.stopAndPersist(logSymbols.error); | ||
} | ||
stopAndPersist(symbol) { | ||
this.stop(); | ||
this.stream.write(`${symbol || ' '} ${this.text}\n`); | ||
cliCursor.hide(); | ||
this.render(); | ||
this.id = setInterval(this.render.bind(this), this.interval); | ||
return this; | ||
}; | ||
Ora.prototype.stop = function () { | ||
if (!this.enabled) { | ||
return this; | ||
} | ||
} | ||
clearInterval(this.id); | ||
this.id = null; | ||
this.frameIndex = 0; | ||
this.clear(); | ||
cliCursor.show(); | ||
return this; | ||
module.exports = function (opts) { | ||
return new Ora(opts); | ||
}; | ||
module.exports = Ora; |
{ | ||
"name": "ora", | ||
"version": "0.2.3", | ||
"version": "0.3.0", | ||
"description": "Elegant terminal spinner", | ||
@@ -13,3 +13,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=4" | ||
}, | ||
@@ -41,10 +41,13 @@ "scripts": { | ||
"cli-cursor": "^1.0.2", | ||
"cli-spinners": "^0.1.2", | ||
"object-assign": "^4.0.1" | ||
"cli-spinners": "^0.2.0", | ||
"log-symbols": "^1.0.2" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"hook-std": "^0.2.0", | ||
"get-stream": "^2.3.0", | ||
"xo": "*" | ||
}, | ||
"xo": { | ||
"esnext": true | ||
} | ||
} |
@@ -106,2 +106,16 @@ # ora [![Build Status](https://travis-ci.org/sindresorhus/ora.svg?branch=master)](https://travis-ci.org/sindresorhus/ora) | ||
### .succeed() | ||
Stop the spinner, change it to a green `✔` and persist the `text`. Returns the instance. See the below GIF below. | ||
### .fail() | ||
Stop the spinner, change it to a red `✖` and persist the `text`. Returns the instance. See the below GIF below. | ||
### .stopAndPersist([symbol]) | ||
Stop the spinner, change it to `symbol` (or `' '` if `symbol` is not provided) and persist the `text`. Returns the instance. See the below GIF below. | ||
<img src="screenshot-2.gif" width="480"> | ||
#### .clear() | ||
@@ -131,2 +145,3 @@ | ||
- [cli-spinners](https://github.com/sindresorhus/cli-spinners) - Spinners for use in the terminal | ||
- [listr](https://github.com/SamVerschueren/listr) - Terminal task list | ||
@@ -133,0 +148,0 @@ |
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
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
6998
87
150
+ Addedlog-symbols@^1.0.2
+ Addedcli-spinners@0.2.0(transitive)
+ Addedlog-symbols@1.0.2(transitive)
- Removedobject-assign@^4.0.1
- Removedcli-spinners@0.1.2(transitive)
- Removedobject-assign@4.1.1(transitive)
Updatedcli-spinners@^0.2.0