drdolphin
drdolphin

Reputation: 127

Setting the input cursor in Linux to a certain position

In the Linux terminal, I want my C program to set the cursor at a certain position. The input prompt should look something like this for the user:

INPUT: I

Where I is supposed to be the cursor where the user can type. With the C syntax

 printf("\033[%d;5HINPUT: ",line);

it does not work, since this cursor only is responsible for the printf-output of my pogram.

Any suggestions for a simple solution?

Upvotes: 1

Views: 389

Answers (1)

Luis Colorado
Luis Colorado

Reputation: 12708

If you want your cursor to be after your last character printed, just print your string and nothing else... the cursor will remain where you left it on last print. The command you show in your sample uses an ANSI escape sequence to locate the cursor at position line indicated by the format %d (this is 5 as the last parameter in the printf call) and column 5. Then you print INPUT: so your cursor will go to line line and column 5 and there it will print the string, leading to something like:

+================================== Top of screen.
|                                   <
|                                   <
|                                   <
|    INPUT: _                       <  Four lines.
|
 ^^^^ four chars before the first I

assuming that line has been initialized to 4.

You don't say what does it mean for you the sentence It doesn't work, but it is far more difficult for me to try to guess what you could mean with it, so I'll appreciate if you edit your question and explain what it does mean. In my opinion the above screen is what you should have obtained, and if that is what you did, then the thing worked fine. And you did bad, but please, explain what are you attempting to do and what you get to reject that output as valid.

Upvotes: 1

Related Questions