forked from nova/jmonkey-test
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
2.4 KiB
104 lines
2.4 KiB
package wow.doge.mygame.utils
|
|
|
|
import java.io.ByteArrayOutputStream
|
|
import java.io.OutputStream
|
|
import java.io.PrintStream
|
|
|
|
import cats.effect.Resource
|
|
import monix.bio.Task
|
|
import scalafx.scene.control.TextArea
|
|
import scalafx.application.Platform
|
|
|
|
trait ConsoleStreamable[T] {
|
|
def println(inst: T, text: String): Unit
|
|
def print(inst: T, text: String): Unit
|
|
}
|
|
|
|
class GenericConsoleStream[T](
|
|
outputStream: OutputStream,
|
|
val config: GenericConsoleStream.Config =
|
|
GenericConsoleStream.Config.default,
|
|
private var streamable: Option[T] = None
|
|
)(implicit
|
|
cs: ConsoleStreamable[T]
|
|
) extends PrintStream(outputStream, true) {
|
|
private lazy val defaultOut = System.out
|
|
|
|
def printToStreamable(stble: Option[T], text: String) =
|
|
stble.foreach(s => cs.println(s, text))
|
|
|
|
override def println(text: String): Unit =
|
|
if (config.exclusive) {
|
|
printToStreamable(streamable, text)
|
|
} else {
|
|
defaultOut.println(text)
|
|
printToStreamable(streamable, text)
|
|
}
|
|
|
|
override def print(text: String): Unit =
|
|
streamable.foreach(s =>
|
|
if (config.exclusive) {
|
|
printToStreamable(streamable, text)
|
|
} else {
|
|
defaultOut.println(text)
|
|
printToStreamable(streamable, text)
|
|
}
|
|
)
|
|
|
|
def :=(s: T) = {
|
|
streamable match {
|
|
case Some(value) =>
|
|
case None => streamable = Some(s)
|
|
}
|
|
}
|
|
}
|
|
|
|
object GenericConsoleStream {
|
|
|
|
/**
|
|
* for future use
|
|
*/
|
|
case class Config(exclusive: Boolean = false)
|
|
object Config {
|
|
lazy val default = Config()
|
|
}
|
|
|
|
implicit val implJFXConsoleStreamForTextArea =
|
|
new ConsoleStreamable[scalafx.scene.control.TextArea] {
|
|
|
|
override def println(
|
|
ta: scalafx.scene.control.TextArea,
|
|
text: String
|
|
): Unit =
|
|
ta.appendText(text + "\n")
|
|
|
|
override def print(
|
|
ta: scalafx.scene.control.TextArea,
|
|
text: String
|
|
): Unit =
|
|
ta.appendText(text)
|
|
|
|
}
|
|
|
|
// def textAreaStreamResource(ta: TextArea) =
|
|
// Resource.make(
|
|
// Task(
|
|
// new GenericConsoleStream(
|
|
// outputStream = new ByteArrayOutputStream(),
|
|
// streamable = ta
|
|
// )
|
|
// )
|
|
// )(s => Task(s.close()))
|
|
|
|
def textAreaStream(
|
|
// ta: TextArea
|
|
) =
|
|
// Task(
|
|
new GenericConsoleStream[TextArea](
|
|
outputStream = new ByteArrayOutputStream()
|
|
// streamable = ta
|
|
)
|
|
// )
|
|
// (s => Task(s.close()))
|
|
|
|
}
|