Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
{
"name": "log-update",
"version": "2.0.0",
"description": "Log by overwriting the previous output in the terminal. Useful for rendering progress bars, animations, etc.",
"name": "log-update-async-hook",
"version": "2.0.2",
"description": "log-update fork that uses async-exit-hook internally",
"license": "MIT",
"repository": "sindresorhus/log-update",
"repository": "AndreyBelym/log-update-async-hook",
"author": {
"name": "Andrey Belym",
"email": "belym.a.2105@gmail.com"
},
"contributors": [{
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
}],
"scripts": {
"test": "xo && node test.js"
"test": "node test.js"
},
"main": "src/index.js",
"files": [
"index.js"
"src"
],
"keywords": [
"log",
Expand All @@ -38,7 +40,8 @@
],
"dependencies": {
"ansi-escapes": "^2.0.0",
"cli-cursor": "^2.0.0",
"async-exit-hook": "^1.1.2",
"onetime": "^2.0.1",
"wrap-ansi": "^2.1.0"
},
"devDependencies": {
Expand Down
6 changes: 5 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# log-update [![Build Status](https://travis-ci.org/sindresorhus/log-update.svg?branch=master)](https://travis-ci.org/sindresorhus/log-update)
# log-update-async-hook [![Build Status](https://travis-ci.org/AndreyBelym/log-update-async-hook.svg?branch=master)](https://travis-ci.org/sindresorhus/log-update)

This is a fork of the [log-update](https://github.com/sindresorhus/log-update) by Sindre Sorhus, that uses `async-exit-hook` to restore terminal cursor state when the process terminates.
Usage of `exit-hook` or `signal-exit` hook in the original `log-update` prevents execution of asynchronous operations on signals (`SIGTERM`, `SIGHUP`, etc.) in the code of the main application.
So I've replaced them by `async-exit-hook`, rewritten code to allow execution on Node versions below `4.x` and bundled some dependencies into the package.

> Log by overwriting the previous output in the terminal.<br>
> Useful for rendering progress bars, animations, etc.

Expand Down
39 changes: 39 additions & 0 deletions src/cli-cursor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';
var restoreCursor = require('./restore-cursor');

var hidden = false;

exports.show = function (stream) {
var s = stream || process.stderr;

if (!s.isTTY) {
return;
}

hidden = false;
s.write('\u001b[?25h');
};

exports.hide = function (stream) {
var s = stream || process.stderr;

if (!s.isTTY) {
return;
}

restoreCursor();
hidden = true;
s.write('\u001b[?25l');
};

exports.toggle = function (force, stream) {
if (force !== undefined) {
hidden = force;
}

if (hidden) {
exports.show(stream);
} else {
exports.hide(stream);
}
};
21 changes: 11 additions & 10 deletions index.js → src/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
'use strict';
const ansiEscapes = require('ansi-escapes');
const cliCursor = require('cli-cursor');
const wrapAnsi = require('wrap-ansi');
var ansiEscapes = require('ansi-escapes');
var wrapAnsi = require('wrap-ansi');
var cliCursor = require('./cli-cursor');

const main = stream => {
let prevLineCount = 0;

const render = function () {
function main (stream) {
var prevLineCount = 0;

var render = function () {
cliCursor.hide();
let out = [].join.call(arguments, ' ') + '\n';
var out = [].join.call(arguments, ' ') + '\n';
out = wrapAnsi(out, process.stdout.columns || 80, {wordWrap: false});
stream.write(ansiEscapes.eraseLines(prevLineCount) + out);
prevLineCount = out.split('\n').length;
};

render.clear = () => {
render.clear = function () {
stream.write(ansiEscapes.eraseLines(prevLineCount));
prevLineCount = 0;
};

render.done = () => {
render.done = function () {
prevLineCount = 0;
cliCursor.show();
};

return render;
};
}

module.exports = main(process.stdout);
module.exports.stderr = main(process.stderr);
Expand Down
9 changes: 9 additions & 0 deletions src/restore-cursor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';
var onetime = require('onetime');
var exitHook = require('async-exit-hook');

module.exports = onetime(function () {
exitHook(function () {
process.stderr.write('\u001b[?25h');
});
});