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 | function getLastCharacter(string, encoding, fd){
|
29 | module.exports.lastCharacter = string.slice(-1);
|
30 | }
|
31 |
|
32 | var unhook;
|
33 | var unhookStderr;
|
34 | module.exports.enable = function enable(){
|
35 | if(!unhook){
|
36 | unhook = hook_stream(process.stdout, getLastCharacter);
|
37 | }
|
38 | if(!unhookStderr){
|
39 | unhookStderr = hook_stream(process.stderr, getLastCharacter);
|
40 | }
|
41 | };
|
42 |
|
43 | module.exports.disable = function disable(){
|
44 | if(unhook){
|
45 | unhook();
|
46 | unhook = undefined;
|
47 | }
|
48 | if(unhookStderr){
|
49 | unhookStderr();
|
50 | unhookStderr = undefined;
|
51 | }
|
52 | };
|