Output to console while preserving user input in ruby
Posted
by CaptnCraig
on Stack Overflow
See other posts from Stack Overflow
or by CaptnCraig
Published on 2010-03-03T19:30:31Z
Indexed on
2010/03/08
17:51 UTC
Read the original article
Hit count: 326
I have a ruby script that is simultaneously and asynchronously receiving and displaying messages from a server, and allowing user input on the console. When a message is received, it is currently being written in the middle of what the user is typing. The input itself isn't garbled, but it looks horrible. Ideally, it would save the users current input, output the message, and then restore the input on the next line. I've done this in c by intercepting every key stroke, but all I remember is that it was a major hassle. I'm fairly new to ruby, so I'm not sure if there is a good way to do this, or how to do it.
Example: User is typing >abcde
, and message hello
comes in, and user types fgh
after. The console would now show:
>abcdehellofgh
and user can continue typing at the end. I would like it to show:
hello
>abcdefgh
© Stack Overflow or respective owner