Last active
May 3, 2018 21:25
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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