2

For example, in a program I want to get current cursor position on terminal (tput u7 or \E[6n on xterm):

CSI P s n

P s = 6 → Report Cursor Position (CPR) [row;column] as CSI r ; c R

I write this sequence to standard output in my program. After that I read the reply from terminal on standard input. (tty is in raw mode during this)

The question is: what if a user happens to input an escape sequence (by pressing some key) or a digit or anything right at the moment after request was sent but before reply was sent or in the midst of the reply? How was this feature supposed to work? Is this mechanism flawed, or I'm missing something?

schaiba
  • 7,631

2 Answers2

0

Both the report cursor position and the keyboard input comes from your terminal. If such an escape sequence is received by the terminal, it should be interpreted before the buffered keyboard input is sent. Only after the cursor position has been sent should the keyboard input be sent to the program. I cannot say if all terminals work correctly here. With a client software in the terminal, you have no control over this.

0

From my testing it works for single key presses. But if you paste text e.g. hello, all position reports come after the second letter (e):

example

I assume you need to first need to queue all buffered input before you can ask for the cursor position.