-
Notifications
You must be signed in to change notification settings - Fork 4
/
console.js
85 lines (71 loc) · 2.33 KB
/
console.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var pty = require('pty.js');
var AnsiParser = require('node-ansiparser');
var AnsiTerminal = require('../dist/ansiterminal.js').AnsiTerminal;
var fs = require('fs');
var stdin = process.openStdin();
process.stdin.setRawMode(true);
const output = fs.createWriteStream('.log', {'flags': 'a'});
const myconsole = new console.Console(output, output);
var _log = console.log;
console.log = myconsole.log;
// initial terminal size
var COLS = 80;
var ROWS = 25;
// spawn a new pseudoterminal
var ptyterm = pty.spawn('bash', [], {
name: 'xterm',
cols: COLS,
rows: ROWS,
cwd: process.env.HOME,
env: process.env
});
// create terminal emulator and parser
var terminal = new AnsiTerminal(COLS, ROWS, 50000);
terminal.debug = true;
var parser = new AnsiParser(terminal);
function printTerminal() {
process.stdout.write('\x1b[2;2H');
var debug_out = '';
for (var i=0; i<terminal.screen.buffer.length; ++i) {
process.stdout.write(terminal.screen.buffer[i].toEscapeString({rtrim:false, empty_cell: ' '}));
process.stdout.write('\x1b[B\x1b[2G');
debug_out += terminal.screen.buffer[i].toEscapeString({rtrim:true, empty_cell: ' '});
if (!terminal.screen.buffer[i].overflow)
debug_out += '\n';
}
console.log(debug_out);
}
// parse data from pseudoterminal and write to terminal
ptyterm.on('data', function(data) {
parser.parse(data);
process.stdout.write('\x1b[100;93;1m\x1b[0;14H' + terminal.title + '\x1b[K');
printTerminal();
process.stdout.write('\x1b['+(terminal.cursor.row+2)+';'+(terminal.cursor.col+2)+'H');
});
terminal.send = function(s){
ptyterm.write(s);
};
stdin.addListener("data", function(d) {
if (d == '\u0003') {
process.stdin.setRawMode(false);
process.stdout.write('\x1b[0m\x1b[2J\x1b[0;0H');
process.exit();
}
ptyterm.write(d);
});
function drawFrame() {
process.stdout.write('\x1b[100;96;1m\x1b[2J\x1b[0;0HCONSOLETEST');
}
// resize signal handler
process.on('SIGWINCH', function() {
var size = process.stdout.getWindowSize();
terminal.resize(size[0]-2, size[1]-2);
ptyterm.resize(size[0]-2, size[1]-2);
drawFrame();
printTerminal();
});
// initial
drawFrame();
var init_size = process.stdout.getWindowSize();
terminal.resize(init_size[0]-2, init_size[1]-2);
ptyterm.resize(init_size[0]-2, init_size[1]-2);