-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
107 lines (84 loc) · 2.24 KB
/
index.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
var stream = require("stream"),
util = require("util");
var STATE = {
INITIAL: 1,
CSI: 2,
SEQUENCE: 3,
};
var Yttej = module.exports = function Yttej(options) {
options = options || {};
options.objectMode = true;
options.highWaterMark = options.highWaterMark || 1;
stream.Transform.call(this, options);
this.state = STATE.INITIAL;
this.buffer = null;
};
util.inherits(Yttej, stream.Transform);
Yttej.prototype._transform = function _transform(input, encoding, done) {
if (!Buffer.isBuffer(input)) {
input = Buffer(input, encoding);
}
var offset = 0;
while (offset < input.length) {
if (this.state === STATE.INITIAL) {
var len = 0;
while (offset + len < input.length) {
if (input[offset + len] === 0x1b) {
break;
}
len++;
}
if (len === input.length) {
this.push({type: "data", data: input});
} else if (len > 0) {
this.push({type: "data", data: input.slice(offset, offset + len)});
}
offset += len;
if (offset >= input.length) {
break;
}
}
if (this.state === STATE.INITIAL && input[offset] === 0x1b) {
this.state = STATE.CSI;
offset++;
if (offset >= input.length) {
break;
}
}
if (this.state === STATE.CSI) {
if (input[offset] !== 0x5b) {
this.push({type: "data", data: new Buffer([0x1b])});
this.state = STATE.INITIAL;
continue;
}
this.state = STATE.SEQUENCE;
this.buffer = new Buffer(0);
offset++;
if (offset >= input.length) {
break;
}
}
if (this.state === STATE.SEQUENCE) {
var len = 0;
while (offset + len < input.length) {
if (input[offset + len] >= 64 && input[offset + len] <= 126) {
break;
}
len++;
}
this.buffer = Buffer.concat([this.buffer, input.slice(offset, offset + len)]);
offset += len;
if (offset >= input.length) {
break;
}
this.push({type: "sequence", char: String.fromCharCode(input[offset]), input: this.buffer});
this.buffer = null;
this.state = STATE.INITIAL;
offset++;
if (offset >= input.length) {
break;
}
}
}
return done();
};