tsjnsn
tsjnsn

Reputation: 451

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?

Upvotes: 4

Views: 3278

Answers (1)

Rex Kerr
Rex Kerr

Reputation: 167891

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

Upvotes: 9

Related Questions