aboutsummaryrefslogtreecommitdiff
path: root/src/scalevalapokalypsi/Client/StdinLineReader.scala
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-17 13:45:44 +0200
committerJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-17 13:45:44 +0200
commit4de67b497e0e229fe4a42f66f833640b6e50fd5a (patch)
tree34fb5b0e776f7cd3adcb4556f4d6a7c8ad66de39 /src/scalevalapokalypsi/Client/StdinLineReader.scala
parent8595e892abc0e0554f589ed2eb88c351a347fbd4 (diff)
downloadscalevalapokalypsi-4de67b497e0e229fe4a42f66f833640b6e50fd5a.tar.gz
scalevalapokalypsi-4de67b497e0e229fe4a42f66f833640b6e50fd5a.zip
Moved the project to an IDEA project & wrote part of README.txt
Diffstat (limited to 'src/scalevalapokalypsi/Client/StdinLineReader.scala')
-rw-r--r--src/scalevalapokalypsi/Client/StdinLineReader.scala31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/scalevalapokalypsi/Client/StdinLineReader.scala b/src/scalevalapokalypsi/Client/StdinLineReader.scala
new file mode 100644
index 0000000..6ba8761
--- /dev/null
+++ b/src/scalevalapokalypsi/Client/StdinLineReader.scala
@@ -0,0 +1,31 @@
+package scalevalapokalypsi.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