www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Prevent console line advancing on user input

reply anonymouse <anony mouse.com> writes:
Wondering if this is possible? Ask a user at input and wait for 
response:

write("Is the sky blue? ");
readf!" %s\n"(response);

If the user's response is correct, I'd like to change the color 
of provided response to indicate it was correct then advance to 
the next line and ask a different question.

If the user's response is incorrect, I'd like to clear the line 
and repeat the question without advancing down the screen 
vertically.

I'm assuming it can be done by intercepting all input before the 
console processes them and outputting a '\r', a blank line the 
length of the screen, and another '\r' to get back to the 
beginning before reprinting the question. But I have no idea how 
to intercept the raw input.

Would appreciate any pointers.
Thanks
--anonymouse
Apr 06 2023
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 4/6/23 4:01 AM, anonymouse wrote:
 Wondering if this is possible? Ask a user at input and wait for response:
 
 write("Is the sky blue? ");
 readf!" %s\n"(response);
 
 If the user's response is correct, I'd like to change the color of 
 provided response to indicate it was correct then advance to the next 
 line and ask a different question.
 
 If the user's response is incorrect, I'd like to clear the line and 
 repeat the question without advancing down the screen vertically.
 
 I'm assuming it can be done by intercepting all input before the console 
 processes them and outputting a '\r', a blank line the length of the 
 screen, and another '\r' to get back to the beginning before reprinting 
 the question. But I have no idea how to intercept the raw input.
You can't intercept raw input. It's the terminal that decides when to send you the line of text, you cannot control it via `stdin`. You need to use a terminal-control library, such as `arsd.terminal` https://github.com/adamdruppe/arsd/blob/master/terminal.d -Steve
Apr 06 2023
parent anonymouse <anony mouse.com> writes:
On Thursday, 6 April 2023 at 14:51:43 UTC, Steven Schveighoffer 
wrote:
 On 4/6/23 4:01 AM, anonymouse wrote:
 Wondering if this is possible?
[snip]
 You need to use a terminal-control library, such as 
 `arsd.terminal`

 https://github.com/adamdruppe/arsd/blob/master/terminal.d

 -Steve
That works perfectly. Thanks.
Apr 06 2023