Skip to content

05 Staying on the current line

Vladyslav Pekker edited this page Dec 26, 2015 · 3 revisions

⬅️ | ➡️

In the ClearPrintModule spells defines methods clearPrint and clearPrintln, which erase the characters in the current line and move the carrot to the beginning of the line, before calling print or println respectively:

object SpinningWheel {
  import scala.concurrent._

  def show()(implicit ec: ExecutionContext = ExecutionContext.Implicits.global): Unit = {
    if (off) {
      on = true
  
      Future {
        while (on) {
          clearPrint('/'); Thread sleep 500
          clearPrint('-'); Thread sleep 500
          clearPrint('\\'); Thread sleep 500
        }
  
        clearPrintln()
      }
    }
  }
  
  def hide(): Unit = on = false

  @volatile private var on: Boolean = false

  private def off = !on
}

⬅️ | ➡️

Clone this wiki locally