-
Notifications
You must be signed in to change notification settings - Fork 6
/
test-mouse.js
57 lines (48 loc) · 1.17 KB
/
test-mouse.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
"use strict";
process.stdin.setRawMode(true);
const ENABLE_MOUSE = "\x1B[?1000;1006h";
const DISABLE_MOUSE = "\x1B[?1000;1006l";
console.log(process.pid);
process.stdout.write(ENABLE_MOUSE);
process.on("exit", () => {
process.stdout.write(DISABLE_MOUSE);
});
process.stdin.on("data", (buffer) => {
const data = buffer.toString();
console.log(JSON.stringify(data), parse(data));
if (data === "\x03") {
process.exit();
}
});
const REGEX = /\x1B\[M([ #])(.+)/;
const REGEX_2 = /\x1B\[<0;(\d+);(\d+)([Mm])/;
/**
* @param {string} data
* @returns {string}
*/
function parse(data) {
const match = REGEX.exec(data);
if (match === null) {
return parse2(data);
}
const [, upDownRaw, chars] = match;
const upDown = upDownRaw === "#" ? "up" : "down";
const nums = chars
.split("")
.map((c) => c.charCodeAt(0) - 32)
.join(", ");
return `${upDown}: ${nums}`;
}
/**
* @param {string} data
* @returns {string}
*/
function parse2(data) {
const match = REGEX_2.exec(data);
if (match === null) {
return "(parse error)";
}
const [, x, y, upDownRaw] = match;
const upDown = upDownRaw === "m" ? "up" : "down";
return `${upDown}: ${x},${y}`;
}