status-logger
Advanced tools
Comparing version 2.0.1 to 3.0.0
82
index.js
@@ -1,72 +0,26 @@ | ||
var singleLineLog = require('single-line-log') | ||
var differ = require('ansi-diff-stream') | ||
module.exports = function (opts) { | ||
module.exports = function (messageGroups, opts) { | ||
if (!messageGroups || !Array.isArray(messageGroups)) return new Error('Message list required') | ||
if (!Array.isArray(messageGroups[0])) messageGroups = [messageGroups] | ||
if (!opts) opts = {} | ||
var logger = getLogger(opts) | ||
var messageQueue = [] | ||
var statusLines = [] | ||
var statusLastLine = '' | ||
var diff = differ() | ||
if (!opts.debug && !opts.quiet) diff.pipe(process.stdout) | ||
return { | ||
message: message, | ||
status: status, | ||
print: print | ||
} | ||
function message (msg) { | ||
messageQueue.push(msg) | ||
} | ||
function status (msg, lineNum) { | ||
if (typeof lineNum === 'undefined') statusLines = [msg] | ||
else if (lineNum === -1) statusLastLine = msg | ||
else if (lineNum < statusLines.length) statusLines[lineNum] = msg | ||
else statusLines.push(msg) | ||
} | ||
function print () { | ||
logger.stdout() // Clear old stdout before printing messages | ||
while (true) { | ||
if (messageQueue.length === 0) break | ||
logger.log(messageQueue[0]) | ||
messageQueue.shift() | ||
groups: messageGroups, | ||
print: function () { | ||
var msg = '' | ||
var prevGroup = false | ||
messageGroups.forEach(function (messages) { | ||
if (!messages.length) return | ||
if (prevGroup) msg += '\n' | ||
msg += messages.join('\n') | ||
prevGroup = true | ||
}) | ||
if (opts.debug) console.log(msg) | ||
else if (!opts.quiet) diff.write(msg) | ||
} | ||
if (statusLines.length || statusLastLine.length) { | ||
var msg = statusLines.join('\n') | ||
msg += '\n' + statusLastLine | ||
logger.stdout(msg) | ||
} | ||
} | ||
} | ||
function getLogger (opts) { | ||
if (opts.quiet) { | ||
return { | ||
stderr: logQuiet, | ||
stdout: logQuiet, | ||
log: logQuiet, | ||
error: logQuiet | ||
} | ||
} | ||
if (opts.debug) { | ||
return { | ||
stderr: console.error.bind(console), | ||
stdout: console.log.bind(console), | ||
log: console.error.bind(console), | ||
error: console.log.bind(console) | ||
} | ||
} | ||
return { | ||
stderr: singleLineLog.stderr, | ||
stdout: singleLineLog.stdout, | ||
log: console.log.bind(console), | ||
error: console.error.bind(console) | ||
} | ||
} | ||
function logQuiet () { | ||
// do nothing | ||
} |
{ | ||
"name": "status-logger", | ||
"version": "2.0.1", | ||
"version": "3.0.0", | ||
"description": "print updating progress to console and stdout", | ||
@@ -10,5 +10,5 @@ "main": "index.js", | ||
"author": "Joe Hand <joe@joeahand.com> (http://joeahand.com/)", | ||
"license": "ISC", | ||
"license": "MIT", | ||
"dependencies": { | ||
"single-line-log": "^1.1.1" | ||
"ansi-diff-stream": "^1.1.1" | ||
}, | ||
@@ -15,0 +15,0 @@ "devDependencies": {}, |
# Status Logger | ||
Log a progress status while also queuing messages. | ||
Send groups of messages, update the messages, and print them to stdout via [ansi-diff-stream](https://github.com/mafintosh/ansi-diff-stream). | ||
* Messages are added to queue and printed above status lines. | ||
* Status can be multi-line, specify line with each message. | ||
Each message group is an array of lines to print. Lines and groups can be updated and printed. Groups are printed in order with a line in between them. | ||
## Example | ||
## Usage | ||
```js | ||
var outputLines = [] | ||
var log = statusLogger([outputLines]) | ||
setInterval(function () { | ||
log.print() | ||
}, 100) | ||
```javascript | ||
var statusLogger = require('status-logger') | ||
var sec = 0 | ||
outputLines.push('I am starting.') | ||
setInterval(function () { | ||
sec++ | ||
outputLines[1] = 'I am progressing & overwriting this line. Seconds = ' + sec | ||
}, 1000) | ||
``` | ||
var log = statusLogger(opts) | ||
See `example.js` for full example with multiple message groups. | ||
log.message('Send a regular console log message') // queue message for next interval | ||
## Installation | ||
log.status('Print to stdout line 0', 0) // print to stdout line 0 in next interval | ||
log.status('Print to stdout line 1', 1) | ||
``` | ||
npm install status-logger | ||
``` | ||
var percentage = 0 | ||
log.status('Percentage' + percentage, 2) | ||
## API | ||
// update percentage & will print updated info with interval | ||
### `var log = statusLogger(messageGroups, opts)` | ||
log.status('Last line', -1) // print to last status line | ||
log.status('Overwrite line 1', 1) | ||
`messageGroups` is an array groups with of lines to print. They will be printed in order. | ||
setInterval(function () { | ||
log.print() // print on interval | ||
}, 500) | ||
``` | ||
### Options | ||
### `log.print()`: print status + messages | ||
* `quiet`: do not print anything | ||
* `debug`: print everything to console.log or console.error | ||
### `log.message(msg)`: log a message | ||
### `log.print()`: print messages from all groups | ||
### `log.status(msg, lineNum)`: print to stdout line | ||
### `log.groups`: array of message groups | ||
## Options | ||
* `quiet`: do not print anything | ||
* `debug`: print everything to console.log or console.error | ||
## License | ||
MIT |
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
4736
6
91
51
+ Addedansi-diff-stream@^1.1.1
+ Addedansi-diff-stream@1.2.1(transitive)
+ Addedbuffer-from@1.1.2(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedthrough2@2.0.5(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedxtend@4.0.2(transitive)
- Removedsingle-line-log@^1.1.1
- Removedcode-point-at@1.1.0(transitive)
- Removedis-fullwidth-code-point@1.0.0(transitive)
- Removednumber-is-nan@1.0.1(transitive)
- Removedsingle-line-log@1.1.2(transitive)
- Removedstring-width@1.0.2(transitive)
- Removedstrip-ansi@3.0.1(transitive)