1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | function hook_stream(stream, callback) {
|
12 | var old_write = stream.write;
|
13 |
|
14 | stream.write = (function(write) {
|
15 | return function(string, encoding, fd) {
|
16 | write.apply(stream, arguments);
|
17 | callback(string, encoding, fd);
|
18 | };
|
19 | })(stream.write);
|
20 |
|
21 | return function() {
|
22 | stream.write = old_write;
|
23 | };
|
24 | };
|
25 |
|
26 |
|
27 | module.exports.lastCharacter = '\n';
|
28 |
|
29 | function getLastCharacter(string, encoding, fd) {
|
30 | module.exports.lastCharacter = string.slice(-1);
|
31 | }
|
32 |
|
33 | var unhook;
|
34 | var unhookStderr;
|
35 | module.exports.enable = function enable() {
|
36 | if (!unhook) {
|
37 | unhook = hook_stream(process.stdout, getLastCharacter);
|
38 | }
|
39 | if (!unhookStderr) {
|
40 | unhookStderr = hook_stream(process.stderr, getLastCharacter);
|
41 | }
|
42 | };
|
43 |
|
44 | module.exports.disable = function disable() {
|
45 | if (unhook) {
|
46 | unhook();
|
47 | unhook = undefined;
|
48 | }
|
49 | if (unhookStderr) {
|
50 | unhookStderr();
|
51 | unhookStderr = undefined;
|
52 | }
|
53 | };
|