5

Wikipedia article on terminal ANSI escape codes shows some codes that could be sent to a terminal AND then some data is returned back to the application. Please provide an example how to send the code and then read the result in Node.js application.

For example this escape sequence:

CSI 6n | DSR – Device Status Report

Reports the cursor position (CPR) to the application as (as though typed at the keyboard) ESC[n;mR, where n is the row and m is the column.)

I spent hours trying to use process.stdout, process.stdin, various fs.* functions, even tried to read from /dev/tty. All in vain, got totally lost.

1
  • 1
    Silently voting for closing the question that does not obviously look like violating the rules is discouraged.
    – exebook
    Apr 29, 2016 at 4:21

1 Answer 1

8

Here's one way:

var util = require("util");

function dsr(callback) {
  process.stdin.setRawMode(true);
  process.stdin.once("data", function(data) {
    process.stdin.setRawMode(false);
    process.stdin.pause();
    callback(data.toString());
  });
  process.stdout.write("\x1b[6n");
}

dsr(function(data) {
  console.log(util.inspect(data));
});

Output:

'\u001b[30;1R'

I'm making stdin go into raw mode so that the result is not printed in the terminal and can be read without the user having to press return.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.