Skip to content

Commit 06843ec

Browse files
committed
Merge async branch (#1)
1 parent cd1484f commit 06843ec

File tree

5 files changed

+78
-22
lines changed

5 files changed

+78
-22
lines changed

package.json

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
{
2-
"name": "log-update",
3-
"version": "2.0.0",
4-
"description": "Log by overwriting the previous output in the terminal. Useful for rendering progress bars, animations, etc.",
2+
"name": "log-update-async-hook",
3+
"version": "2.0.2",
4+
"description": "log-update fork that uses async-exit-hook internally",
55
"license": "MIT",
6-
"repository": "sindresorhus/log-update",
6+
"repository": "AndreyBelym/log-update-async-hook",
77
"author": {
8+
"name": "Andrey Belym",
9+
"email": "belym.a.2105@gmail.com"
10+
},
11+
"contributors": [{
812
"name": "Sindre Sorhus",
913
"email": "sindresorhus@gmail.com",
1014
"url": "sindresorhus.com"
11-
},
12-
"engines": {
13-
"node": ">=4"
14-
},
15+
}],
1516
"scripts": {
16-
"test": "xo && node test.js"
17+
"test": "node test.js"
1718
},
19+
"main": "src/index.js",
1820
"files": [
19-
"index.js"
21+
"src"
2022
],
2123
"keywords": [
2224
"log",
@@ -38,7 +40,8 @@
3840
],
3941
"dependencies": {
4042
"ansi-escapes": "^2.0.0",
41-
"cli-cursor": "^2.0.0",
43+
"async-exit-hook": "^1.1.2",
44+
"onetime": "^2.0.1",
4245
"wrap-ansi": "^2.1.0"
4346
},
4447
"devDependencies": {

readme.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
# log-update [![Build Status](https://travis-ci.org/sindresorhus/log-update.svg?branch=master)](https://travis-ci.org/sindresorhus/log-update)
1+
# log-update-async-hook [![Build Status](https://travis-ci.org/AndreyBelym/log-update-async-hook.svg?branch=master)](https://travis-ci.org/AndreyBelym/log-update-async-hook)
22

3+
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.
4+
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.
5+
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.
6+
37
> Log by overwriting the previous output in the terminal.<br>
48
> Useful for rendering progress bars, animations, etc.
59

src/cli-cursor.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
var restoreCursor = require('./restore-cursor');
3+
4+
var hidden = false;
5+
6+
exports.show = function (stream) {
7+
var s = stream || process.stderr;
8+
9+
if (!s.isTTY) {
10+
return;
11+
}
12+
13+
hidden = false;
14+
s.write('\u001b[?25h');
15+
};
16+
17+
exports.hide = function (stream) {
18+
var s = stream || process.stderr;
19+
20+
if (!s.isTTY) {
21+
return;
22+
}
23+
24+
restoreCursor();
25+
hidden = true;
26+
s.write('\u001b[?25l');
27+
};
28+
29+
exports.toggle = function (force, stream) {
30+
if (force !== undefined) {
31+
hidden = force;
32+
}
33+
34+
if (hidden) {
35+
exports.show(stream);
36+
} else {
37+
exports.hide(stream);
38+
}
39+
};

index.js renamed to src/index.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
'use strict';
2-
const ansiEscapes = require('ansi-escapes');
3-
const cliCursor = require('cli-cursor');
4-
const wrapAnsi = require('wrap-ansi');
2+
var ansiEscapes = require('ansi-escapes');
3+
var wrapAnsi = require('wrap-ansi');
4+
var cliCursor = require('./cli-cursor');
55

6-
const main = stream => {
7-
let prevLineCount = 0;
86

9-
const render = function () {
7+
function main (stream) {
8+
var prevLineCount = 0;
9+
10+
var render = function () {
1011
cliCursor.hide();
11-
let out = [].join.call(arguments, ' ') + '\n';
12+
var out = [].join.call(arguments, ' ') + '\n';
1213
out = wrapAnsi(out, process.stdout.columns || 80, {wordWrap: false});
1314
stream.write(ansiEscapes.eraseLines(prevLineCount) + out);
1415
prevLineCount = out.split('\n').length;
1516
};
1617

17-
render.clear = () => {
18+
render.clear = function () {
1819
stream.write(ansiEscapes.eraseLines(prevLineCount));
1920
prevLineCount = 0;
2021
};
2122

22-
render.done = () => {
23+
render.done = function () {
2324
prevLineCount = 0;
2425
cliCursor.show();
2526
};
2627

2728
return render;
28-
};
29+
}
2930

3031
module.exports = main(process.stdout);
3132
module.exports.stderr = main(process.stderr);

src/restore-cursor.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
var onetime = require('onetime');
3+
var exitHook = require('async-exit-hook');
4+
5+
module.exports = onetime(function () {
6+
exitHook(function () {
7+
process.stderr.write('\u001b[?25h');
8+
});
9+
});

0 commit comments

Comments
 (0)