0

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.)

Net
  • 47
  • 6
  • Well, where do you want the log to go? So long as you're only using the standard terminal output, the prompt and the log *have* to go to the same place. If you're trying to create a full fledged interactive terminal app, then that's a bit more complex. You may want to look at [Writing over previously output lines in the command prompt with ruby](http://stackoverflow.com/q/4762843/1157054) or [Vedeu](https://github.com/gavinlaking/vedeu). – Ajedi32 Feb 10 '15 at 15:55
  • @Ajedi32 Thanks. So for example I should create my own thread that deals solely with printing to the terminal? – Net Feb 10 '15 at 16:02
  • Hmm, well that *could* work; there's a whole lot more to it than that though. If you used one particular thread for handling output, and ensured that that thread didn't overwrite your prompt then yeah, that could solve the problem. – Ajedi32 Feb 10 '15 at 17:51
  • @Ajedi32 have you used Vedeu yourself? I'm having an issue with borders not geometry height. – Net Feb 11 '15 at 22:09
  • Nope, I've never had to build a full-featured terminal app before. If my app needs a UI, I'm probably not going to try to building it as a command line app in the first place. If your app *doesn't* need to be running from the terminal, then I'd suggest you use something like [Shoes](http://shoesrb.com/) instead. Otherwise, either post a new question or, in the case of a possible bug, file an issue on the GitHub repo. – Ajedi32 Feb 11 '15 at 22:16

0 Answers0