aboutsummaryrefslogtreecommitdiff
path: root/src/scalevalapokalypsi/Client/StdinLineReader.scala
blob: 6ba876135b873da9de97d0bd11dd2667fe5d2d37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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