forked from bubkoo/get-cursor-position
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (31 loc) · 800 Bytes
/
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
var pos = require('./build/Release/pos');
var tty = require('tty');
var code = '\x1b[6n';
function raw(mode) {
if (process.stdin.setRawMode) {
process.stdin.setRawMode(mode)
} else {
tty.setRawMode(mode)
}
}
pos.async = function (callback, context) {
// start listening
process.stdin.resume();
raw(true);
process.stdin.once('data', function (b) {
var match = /\[(\d+)\;(\d+)R$/.exec(b.toString());
if (match) {
var position = match.slice(1, 3).reverse().map(Number);
callback && callback.call(context, {
row: position[1],
col: position[0]
});
}
// cleanup and close stdin
raw(false);
process.stdin.pause();
});
process.stdout.write(code);
process.stdout.emit('data', code);
};
module.exports = pos;