aboutsummaryrefslogtreecommitdiff
path: root/src/scalevalapokalypsi/UI/StdinLineReader.scala
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-22 22:42:22 +0200
committerJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-22 22:42:22 +0200
commitdb5612ed9734d51e6fcd0d7b5a7635e49b773581 (patch)
treee23e607c9d9eeedc377bf44e57c2b58b41d0389d /src/scalevalapokalypsi/UI/StdinLineReader.scala
parent49985d1d11c426968fc298469671326aace96d00 (diff)
downloadscalevalapokalypsi-db5612ed9734d51e6fcd0d7b5a7635e49b773581.tar.gz
scalevalapokalypsi-db5612ed9734d51e6fcd0d7b5a7635e49b773581.zip
Character safety checking, supported terminals updated
Diffstat (limited to 'src/scalevalapokalypsi/UI/StdinLineReader.scala')
-rw-r--r--src/scalevalapokalypsi/UI/StdinLineReader.scala33
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