I want to accept commands at all time in my ruby app, however I'm running into an issue with the app's log and the prompt colliding. Say I'm typing exit.
LOG: something happened in the app > exi
I type exi but before I can hit t a new message appears right where I'm typing.
LOG: something happened in the app > exiLOG: something else happened while you were typing
I need some way to separate the log from the prompt.
What I've been trying:
# This thread prints whatever the user types
thread_a = Thread.new do
loop do
print '> '
puts gets.chomp
end
end
# This thread prints a message every 3 seconds.
thread_b = Thread.new do
loop do
puts "I am a message."
sleep 3
end
end
thread_a.join
Edit: I've been trying to make new logs print above the prompt by moving the cursor up, then creating a new line, printing the log, then moving back down to the prompt but it seems I can't insert new lines between existing lines. (print "\e[A\n A log message."
it seems \n just moves the cursor down a line rather than creates a new line.)