-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
55 lines (43 loc) · 1.06 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
var tty = require('tty');
var pos = require('bindings')('pos');
var code = '\x1b[6n';
function setRawMode(mode) {
if (process.stdin.setRawMode) {
process.stdin.setRawMode(mode)
} else {
tty.setRawMode(mode)
}
}
pos.async = function (callback, context) {
if (process.platform === 'win32') {
process.nextTick(function () {
var position = pos.sync();
if (position) {
callback && callback.call(context, {
row: position.row,
col: position.col
});
}
});
return;
}
// start listening
process.stdin.resume();
setRawMode(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
setRawMode(false);
process.stdin.pause();
});
process.stdout.write(code);
process.stdout.emit('data', code);
};
module.exports = pos;