Use the ESC [6n escape sequence to query the horizontal cursor position * and return it. On error -1 is returned, on success the position of the * cursor. */
| 902 | * and return it. On error -1 is returned, on success the position of the |
| 903 | * cursor. */ |
| 904 | static int getCursorPosition(int ifd, int ofd) { |
| 905 | char buf[32]; |
| 906 | int cols, rows; |
| 907 | unsigned int i = 0; |
| 908 | |
| 909 | /* Report cursor location */ |
| 910 | if (write(ofd, "\x1b[6n", 4) != 4) |
| 911 | return -1; |
| 912 | |
| 913 | /* Read the response: ESC [ rows ; cols R */ |
| 914 | while (i < sizeof(buf) - 1) { |
| 915 | if (read(ifd, buf + i, 1) != 1) |
| 916 | break; |
| 917 | if (buf[i] == 'R') |
| 918 | break; |
| 919 | i++; |
| 920 | } |
| 921 | buf[i] = '\0'; |
| 922 | |
| 923 | /* Parse it. */ |
| 924 | if (buf[0] != ESC || buf[1] != '[') |
| 925 | return -1; |
| 926 | if (sscanf(buf + 2, "%d;%d", &rows, &cols) != 2) |
| 927 | return -1; |
| 928 | return cols; |
| 929 | } |
| 930 | |
| 931 | /* Try to get the number of columns in the current terminal, or assume 80 |
| 932 | * if it fails. */ |
no test coverage detected