diff options
author | Aleksi Heikkila <aleksi.e.heikkila@aalto.fi> | 2024-11-23 22:33:53 +0200 |
---|---|---|
committer | Aleksi Heikkila <aleksi.e.heikkila@aalto.fi> | 2024-11-23 22:33:53 +0200 |
commit | 27dd937617cce1e43df1c16e12050f6e88763d54 (patch) | |
tree | 8247736f7c4d5b1a0c1541c25f477e73e5783944 /src/scalevalapokalypsi/UI/StdinLineReader.scala | |
parent | b11c02e8f1f4dea09847da999c2acb2f96df8a58 (diff) | |
parent | 28b83db50f33cb704311ffe608dcd8c4412635cf (diff) | |
download | scalevalapokalypsi-27dd937617cce1e43df1c16e12050f6e88763d54.tar.gz scalevalapokalypsi-27dd937617cce1e43df1c16e12050f6e88763d54.zip |
Merge branch 'master' of ssh://cron4.fi/~/O1-game
Diffstat (limited to 'src/scalevalapokalypsi/UI/StdinLineReader.scala')
-rw-r--r-- | src/scalevalapokalypsi/UI/StdinLineReader.scala | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/scalevalapokalypsi/UI/StdinLineReader.scala b/src/scalevalapokalypsi/UI/StdinLineReader.scala new file mode 100644 index 0000000..4d0f778 --- /dev/null +++ b/src/scalevalapokalypsi/UI/StdinLineReader.scala @@ -0,0 +1,33 @@ +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 if there are any. */ + def newLine(): Option[String] = + this.nextLine.value match + case Some(Success(s)) => + if s.contains("\u0000") then + println("End of stream!") + this.startReading() + Some(s) + case Some(Failure(e)) => + this.startReading() + None + case None => None + + /** Discards the line that is currently being read and restarts reading */ + def startReading(): Unit = + this.nextLine = Future(readLine()) + + +end StdinLineReader |