Comparing version 4.0.2 to 4.0.3
@@ -97,2 +97,4 @@ /// <reference types="node"/> | ||
Discard stdin input (except Ctrl+C) while running if it's TTY. This prevents the spinner from twitching on input, outputting broken lines on `Enter` key presses, and prevents buffering of input while the spinner is running. | ||
This has no effect on Windows as there's no good way to implement discarding stdin properly there. | ||
@@ -99,0 +101,0 @@ @default true |
140
index.js
'use strict'; | ||
const readline = require('readline'); | ||
const chalk = require('chalk'); | ||
@@ -9,2 +10,3 @@ const cliCursor = require('cli-cursor'); | ||
const isInteractive = require('is-interactive'); | ||
const MuteStream = require('mute-stream'); | ||
@@ -14,6 +16,84 @@ const TEXT = Symbol('text'); | ||
const noop = () => {}; | ||
const ASCII_ETX_CODE = 0x03; // Ctrl+C emits this code | ||
class StdinDiscarder { | ||
constructor() { | ||
this.requests = 0; | ||
this.mutedStream = new MuteStream(); | ||
this.mutedStream.pipe(process.stdout); | ||
this.mutedStream.mute(); | ||
const self = this; | ||
this.ourEmit = function (event, data, ...args) { | ||
const {stdin} = process; | ||
if (self.requests > 0 || stdin.emit === self.ourEmit) { | ||
if (event === 'keypress') { // Fixes readline behavior | ||
return; | ||
} | ||
if (event === 'data' && data.includes(ASCII_ETX_CODE)) { | ||
process.emit('SIGINT'); | ||
} | ||
Reflect.apply(self.oldEmit, this, [event, data, ...args]); | ||
} else { | ||
Reflect.apply(process.stdin.emit, this, [event, data, ...args]); | ||
} | ||
}; | ||
} | ||
start() { | ||
this.requests++; | ||
if (this.requests === 1) { | ||
this.realStart(); | ||
} | ||
} | ||
stop() { | ||
if (this.requests <= 0) { | ||
throw new Error('`stop` called more times than `start`'); | ||
} | ||
this.requests--; | ||
if (this.requests === 0) { | ||
this.realStop(); | ||
} | ||
} | ||
realStart() { | ||
// No known way to make it work reliably on Windows | ||
if (process.platform === 'win32') { | ||
return; | ||
} | ||
this.rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: this.mutedStream | ||
}); | ||
this.rl.on('SIGINT', () => { | ||
if (process.listenerCount('SIGINT') === 0) { | ||
process.emit('SIGINT'); | ||
} else { | ||
this.rl.close(); | ||
process.kill(process.pid, 'SIGINT'); | ||
} | ||
}); | ||
} | ||
realStop() { | ||
if (process.platform === 'win32') { | ||
return; | ||
} | ||
this.rl.close(); | ||
this.rl = undefined; | ||
} | ||
} | ||
const stdinDiscarder = new StdinDiscarder(); | ||
class Ora { | ||
@@ -50,2 +130,3 @@ constructor(options) { | ||
this.discardStdin = this.options.discardStdin; | ||
this.isDiscardingStdin = false; | ||
} | ||
@@ -189,3 +270,4 @@ | ||
if (this.discardStdin && process.stdin.isTTY) { | ||
this.startDiscardingStdin(); | ||
this.isDiscardingStdin = true; | ||
stdinDiscarder.start(); | ||
} | ||
@@ -212,4 +294,5 @@ | ||
if (this.discardStdin && process.stdin.isTTY) { | ||
this.stopDiscardingStdin(); | ||
if (this.discardStdin && process.stdin.isTTY && this.isDiscardingStdin) { | ||
stdinDiscarder.stop(); | ||
this.isDiscardingStdin = false; | ||
} | ||
@@ -220,49 +303,2 @@ | ||
startDiscardingStdin() { | ||
const {stdin} = process; | ||
this._stdinOldRawMode = stdin.isRaw; | ||
this._stdinOldEmit = stdin.emit; | ||
this._stdinOldEmitOwnProperty = Object.prototype.hasOwnProperty.call(stdin, 'emit'); | ||
stdin.setRawMode(true); | ||
stdin.on('data', noop); | ||
stdin.resume(); | ||
const self = this; | ||
stdin.emit = function (event, data, ...args) { | ||
if (event === 'keypress') { // Fixes readline behavior | ||
return; | ||
} | ||
if (event === 'data' && data.includes(ASCII_ETX_CODE)) { | ||
process.emit('SIGINT'); | ||
} | ||
self._stdinOldEmit.apply(this, [event, data, ...args]); | ||
}; | ||
} | ||
stopDiscardingStdin() { | ||
if (this._stdinOldEmit !== undefined) { | ||
const {stdin} = process; | ||
stdin.setRawMode(this._stdinOldRawMode); | ||
stdin.removeListener('data', noop); | ||
if (stdin.listenerCount('data') === 0) { | ||
stdin.pause(); | ||
} | ||
if (this._stdinOldEmitOwnProperty) { | ||
stdin.emit = this._stdinOldEmit; | ||
} else { | ||
delete stdin.emit; | ||
} | ||
this._stdinOldRawMode = undefined; | ||
this._stdinOldEmit = undefined; | ||
this._stdinOldEmitOwnProperty = undefined; | ||
} | ||
} | ||
succeed(text) { | ||
@@ -269,0 +305,0 @@ return this.stopAndPersist({symbol: logSymbols.success, text}); |
{ | ||
"name": "ora", | ||
"version": "4.0.2", | ||
"version": "4.0.3", | ||
"description": "Elegant terminal spinner", | ||
@@ -39,3 +39,3 @@ "license": "MIT", | ||
"dependencies": { | ||
"chalk": "^2.4.2", | ||
"chalk": "^3.0.0", | ||
"cli-cursor": "^3.1.0", | ||
@@ -45,3 +45,4 @@ "cli-spinners": "^2.2.0", | ||
"log-symbols": "^3.0.0", | ||
"strip-ansi": "^5.2.0", | ||
"mute-stream": "0.0.8", | ||
"strip-ansi": "^6.0.0", | ||
"wcwidth": "^1.0.1" | ||
@@ -53,5 +54,5 @@ }, | ||
"get-stream": "^5.1.0", | ||
"tsd": "^0.8.0", | ||
"xo": "^0.24.0" | ||
"tsd": "^0.10.0", | ||
"xo": "^0.25.3" | ||
} | ||
} |
@@ -58,3 +58,3 @@ # ora [![Build Status](https://travis-ci.org/sindresorhus/ora.svg?branch=master)](https://travis-ci.org/sindresorhus/ora) | ||
Type: `string | object`<br> | ||
Type: `string | object`\ | ||
Default: `'dots'` <img src="screenshot-spinner.gif" width="14"> | ||
@@ -75,4 +75,4 @@ | ||
Type: `string`<br> | ||
Default: `'cyan'`<br> | ||
Type: `string`\ | ||
Default: `'cyan'`\ | ||
Values: `'black'` `'red'` `'green'` `'yellow'` `'blue'` `'magenta'` `'cyan'` `'white'` `'gray'` | ||
@@ -84,3 +84,3 @@ | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `true` | ||
@@ -92,3 +92,3 @@ | ||
Type: `number`<br> | ||
Type: `number`\ | ||
Default: `0` | ||
@@ -100,3 +100,3 @@ | ||
Type: `number`<br> | ||
Type: `number`\ | ||
Default: Provided by the spinner or `100` | ||
@@ -110,3 +110,3 @@ | ||
Type: `stream.Writable`<br> | ||
Type: `stream.Writable`\ | ||
Default: `process.stderr` | ||
@@ -128,3 +128,3 @@ | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `true` | ||
@@ -134,2 +134,4 @@ | ||
This has no effect on Windows as there's no good way to implement discarding stdin properly there. | ||
### Instance | ||
@@ -175,3 +177,3 @@ | ||
Type: `string`<br> | ||
Type: `string`\ | ||
Default: `' '` | ||
@@ -183,3 +185,3 @@ | ||
Type: `string`<br> | ||
Type: `string`\ | ||
Default: Current `'text'` | ||
@@ -191,3 +193,3 @@ | ||
Type: `string`<br> | ||
Type: `string`\ | ||
Default: Current `prefixText` | ||
@@ -194,0 +196,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
21564
483
261
8
+ Addedmute-stream@0.0.8
+ Addedansi-regex@5.0.1(transitive)
+ Addedansi-styles@4.3.0(transitive)
+ Addedchalk@3.0.0(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addedmute-stream@0.0.8(transitive)
+ Addedstrip-ansi@6.0.1(transitive)
+ Addedsupports-color@7.2.0(transitive)
- Removedansi-regex@4.1.1(transitive)
- Removedstrip-ansi@5.2.0(transitive)
Updatedchalk@^3.0.0
Updatedstrip-ansi@^6.0.0