diff options
author | Joel Kronqvist <joel.kronqvist@iki.fi> | 2024-11-14 19:25:19 +0200 |
---|---|---|
committer | Joel Kronqvist <joel.kronqvist@iki.fi> | 2024-11-14 19:25:19 +0200 |
commit | a43812ed462630850edbf29bda182fbf1e5e1263 (patch) | |
tree | 20e84b60adee6fa31ceb970ffccffa5b6b583d86 /src/main/scala/Client/StdinLineReader.scala | |
parent | c87263e9e493fe6c130f5ad6a523871c08987f4c (diff) | |
download | scalevalapokalypsi-a43812ed462630850edbf29bda182fbf1e5e1263.tar.gz scalevalapokalypsi-a43812ed462630850edbf29bda182fbf1e5e1263.zip |
Immediate printing of actions & no prompt on blocking action & refactoring
Diffstat (limited to 'src/main/scala/Client/StdinLineReader.scala')
-rw-r--r-- | src/main/scala/Client/StdinLineReader.scala | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/main/scala/Client/StdinLineReader.scala b/src/main/scala/Client/StdinLineReader.scala new file mode 100644 index 0000000..42a1f40 --- /dev/null +++ b/src/main/scala/Client/StdinLineReader.scala @@ -0,0 +1,31 @@ +package o1game.Client + +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)) => + 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 |