I am trying to get the caret's (console cursor) position in ubuntu. I found a solution (here: https://thoughtsordiscoveries.wordpress.com/2017/04/26/set-and-read-cursor-position-in-terminal-windows-and-linux/ ) that makes use of ANSI codes, which looks like this:
printf("\033[6n");
scanf("\033[%d;%dR", &x, &y); // in x and y I save the position
The problem with this is that printf("\033[6n");
prints stuff in the terminal, which is not something that I want. I tried to hide the output of printf("\033[6n");
using the ANSI code \033[8m
, but this only makes the characters invisible, which is not what I want. I want to get rid of the output completely. I know that you can redirect the output to /dev/null
if I'm not mistaking, but I don't know if that won't mess up the cursor position then, I didn't try this yet.
So, one of the two options:
1. How can I hide the output of printf
without messing up anything?
OR
2. Is there any other way to get the cursor position without external libraries? I believe it is possible with <termios.h>
, but I couldn't find an explanation on how that works.