microsoft / node-pty

Fork pseudoterminals in Node.JS
Other
1.43k stars 232 forks source link

Support for ANSI code "request cursor position" #535

Closed rhergenreder closed 2 years ago

rhergenreder commented 2 years ago

Environment details

Issue description

Certain applications send ANSI escape codes, one of those is \x1b[6n as described here: https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797. Applications sending this escape code usually expect the PTY to respond with an ANSI escape code of the current cursor position in the format of \x1b[#;#R.

As the application I called from node-pty blocks it's execution until receiving this response, I had to implement my own check. Wouldn't it be a better way, if node-pty handles such requests by itself?

I used the following onData handler with encoding set to null, to avoid any conversion errors.

term.onData((data) => {
  if (data.includes("\x1b[6n")) {
    term.write("\x1b[1;31R");
    data = data.replace("\x1b[6n", "");
  }
  // continue data handling
});
Tyriar commented 2 years ago

node-pty doesn't know what the cursor position is, you would run a terminal like xterm.js on that side which is responsible for tracking the cursor:

https://github.com/xtermjs/xterm.js/blob/19367a6042a6360e9130fd0fbee0a66cdd75ddd4/src/common/InputHandler.ts#L2569-L2574