I am currently writing a windows console program and need to obtain the number of char on a given line. I know how to do this with the windows console api but it is no longer the Microsoft supported method. The current supported method is to use virtual terminal sequences, in this case \x1b[6n. I've not been able to get the terminal sequence to work in a usable fashion. I've included a stripped down example below.
The problem exists in how the terminal sequence functions. When one provides the terminal sequence via console output [ex: printf("\x1b[6n")], the result is returned as console input. Windows console behavior causes a pause when the input is fetched which requires user response (a key press) & outputs the result to the console. Neither of these are a desired result & I just cannot find a way to avoid them.
I've even tried piping (which I would not use) just to see if I could by-pass one or both of the pause + user response / output to console issues.
I'm at the point now where I've exhausted my abilities. Would any of you care to take a look at it?
#include <stdio.h>
#include <windows.h>
int main (void);
int main() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwMode;
// Enable Virtual Terminal Processing
GetConsoleMode(hConsole, &dwMode);
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hConsole, dwMode);
// Send the query sequence
printf("\x1b[6n");
// Read the response
char response[16];
fgets(response, sizeof(response), stdin);
// Parse the response to extract row and column values
int row, col;
sscanf(response, "\x1b[%d;%dR", &row, &col);
// Print the retrieved row and column values
printf("Row: %d Column: %d\n", row, col);
return 0;
}
Thank you.