forked from bubkoo/get-cursor-position
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (43 loc) · 1.05 KB
/
index.js
File metadata and controls
55 lines (43 loc) · 1.05 KB
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 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) {
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();
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;