Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange issue with SBT, println, and scala console application

When I run my scala code (I'm using SBT), the prompt is displayed after I enter some text as shown here:

C:\... > sbt run
[info] Loading project definition [...]
[info] Set current project to [...]
Running com[...]
test
>>





exit
>> >> >> >> >> >> [success] Total time[...]

It seems like it's stacking up the print() statements and only displaying them when it runs a different command.

If I use println() it works as it should (except that I don't want a newline)

The code:

...
  def main(args:Array[String]) {
    var endSession:Boolean = false
    var cmd = ""
    def acceptInput:Any = {
      print(">> ")
      cmd = Console.readLine
      if (cmd != "exit") {
        if (cmd != "") runCommand(cmd)
        acceptInput
      }
    }

    acceptInput
  }
...

What's going on here?

like image 579
tsjnsn Avatar asked Jan 13 '23 06:01

tsjnsn


1 Answers

Output from print (and println) can be buffered. Scala sends output through java.io.PrintStream, which suggests that it will only auto-flush on newline, and then only if you ask. It might be OS dependent, though, since my print appears immediately.

If you add Console.out.flush after each print, you'll empty out the buffer to the screen (on any OS).

like image 141
Rex Kerr Avatar answered Jan 31 '23 11:01

Rex Kerr