Skip to content

Instantly share code, notes, and snippets.

@mnewt
Last active May 3, 2018 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnewt/2f778d047d6283a9908109240e150b5e to your computer and use it in GitHub Desktop.
Save mnewt/2f778d047d6283a9908109240e150b5e to your computer and use it in GitHub Desktop.
Get the current column and row in the terminal using ANSI Device Status Report
#!/usr/bin/env node
// Loosely based on https://stackoverflow.com/questions/36929209/read-ansi-escape-from-terminal
'use strict';
var deasync = require('deasync');
function asyncDSR(opts, callback) {
// opts is an optional object:
// { in: input }
if (callback === null) {
callback = opts;
opts = { in: process.stdin };
}
if(opts.in.isTTY) {
opts.in.setRawMode(true);
}
opts.in.once('data', function(data) {
opts.in.setRawMode(false);
opts.in.pause();
var m = data.toString().match(/^\u001b\[([0-9]+);([0-9]+)R$/);
callback({row: m[1], col: m[2]});
});
process.stdout.write('\u001b[6n');
}
var syncDSR = deasync(asyncDSR);
function DSR(opts) {
try {
return syncDSR(opts);
}
catch(err) {
return err;
}
}
module.exports.asyncDSR = asyncDSR;
module.exports.DSR = DSR;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment