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.

Community
  • 1
  • 1
exebook
  • 32,014
  • 33
  • 141
  • 226
  • 1
    Silently voting for closing the question that does not obviously look like violating the rules is discouraged. – exebook Apr 29 '16 at 04:21

1 Answers1

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.

Dogbert
  • 212,659
  • 41
  • 396
  • 397