package scalevalapokalypsi.UI import scala.concurrent.Future import scala.concurrent.ExecutionContext.Implicits.global import scala.io.StdIn.readLine import scala.util.{Try, Success, Failure} /** This class is for taking new lines from stdin when they are available. * reading starts when either newLine or clear or startReading are called. */ class StdinLineReader: private var nextLine: Future[String] = Future.failed(Exception()) /** Returns a new line of input as a Right when there are any. * If there is no new line due to EOF, returns Left(true), * if there is no new line due to some other error, returns Left(false) */ def newLine(): Either[Boolean, String] = this.nextLine.value match case Some(Success(s)) => if s == null then Left(true) else this.startReading() Right(s) case Some(Failure(e)) => this.startReading() Left(false) case None => Left(false) /** Discards the line that is currently being read and restarts reading */ def startReading(): Unit = this.nextLine = Future(readLine()) end StdinLineReader