log-update
Advanced tools
Comparing version
@@ -78,7 +78,4 @@ /// <reference types="node"/> | ||
) => logUpdate.LogUpdate; | ||
// TODO: Remove this for the next major release | ||
default: typeof logUpdate; | ||
}; | ||
export = logUpdate; |
29
index.js
@@ -5,3 +5,6 @@ 'use strict'; | ||
const wrapAnsi = require('wrap-ansi'); | ||
const sliceAnsi = require('slice-ansi'); | ||
const defaultTerminalHeight = 24; | ||
const getWidth = stream => { | ||
@@ -17,7 +20,18 @@ const {columns} = stream; | ||
const main = (stream, options) => { | ||
options = Object.assign({ | ||
showCursor: false | ||
}, options); | ||
const fitToTerminalHeight = (stream, text) => { | ||
const terminalHeight = stream.rows || defaultTerminalHeight; | ||
const lines = text.split('\n'); | ||
const toRemove = lines.length - terminalHeight; | ||
if (toRemove <= 0) { | ||
return text; | ||
} | ||
return sliceAnsi( | ||
text, | ||
lines.slice(0, toRemove).join('\n').length + 1, | ||
text.length); | ||
}; | ||
const main = (stream, {showCursor = false} = {}) => { | ||
let previousLineCount = 0; | ||
@@ -28,3 +42,3 @@ let previousWidth = getWidth(stream); | ||
const render = (...args) => { | ||
if (!options.showCursor) { | ||
if (!showCursor) { | ||
cliCursor.hide(); | ||
@@ -34,2 +48,3 @@ } | ||
let output = args.join(' ') + '\n'; | ||
output = fitToTerminalHeight(stream, output); | ||
const width = getWidth(stream); | ||
@@ -63,3 +78,3 @@ if (output === previousOutput && previousWidth === width) { | ||
if (!options.showCursor) { | ||
if (!showCursor) { | ||
cliCursor.show(); | ||
@@ -73,5 +88,3 @@ } | ||
module.exports = main(process.stdout); | ||
// TODO: Remove this for the next major release | ||
module.exports.default = module.exports; | ||
module.exports.stderr = main(process.stderr); | ||
module.exports.create = main; |
{ | ||
"name": "log-update", | ||
"version": "3.4.0", | ||
"version": "4.0.0", | ||
"description": "Log by overwriting the previous output in the terminal. Useful for rendering progress bars, animations, etc.", | ||
@@ -14,3 +14,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=6" | ||
"node": ">=10" | ||
}, | ||
@@ -43,13 +43,14 @@ "scripts": { | ||
"dependencies": { | ||
"ansi-escapes": "^3.2.0", | ||
"cli-cursor": "^2.1.0", | ||
"wrap-ansi": "^5.0.0" | ||
"ansi-escapes": "^4.3.0", | ||
"cli-cursor": "^3.1.0", | ||
"slice-ansi": "^4.0.0", | ||
"wrap-ansi": "^6.2.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^11.12.2", | ||
"ava": "^1.4.1", | ||
"@types/node": "^13.7.4", | ||
"ava": "^3.3.0", | ||
"terminal.js": "^1.0.10", | ||
"tsd": "^0.7.1", | ||
"xo": "^0.24.0" | ||
"tsd": "^0.11.0", | ||
"xo": "^0.26.1" | ||
} | ||
} |
7575
3.48%132
6.45%4
33.33%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
Updated
Updated
Updated