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.
70 lines
1.6 KiB
70 lines
1.6 KiB
package wow.doge.mygame.utils
|
|
|
|
import java.io.PrintStream
|
|
import java.io.OutputStream
|
|
import java.io.ByteArrayOutputStream
|
|
import scalafx.scene.control.TextArea
|
|
import cats.effect.Resource
|
|
import monix.bio.Task
|
|
|
|
trait JFXConsoleStreamable[T] {
|
|
def println(inst: T, text: String): Unit
|
|
def print(inst: T, text: String): Unit
|
|
}
|
|
|
|
class JFXConsoleStream[T](
|
|
outputStream: OutputStream,
|
|
val config: JFXConsoleStream.Config = JFXConsoleStream.Config.default,
|
|
control: T
|
|
)(implicit
|
|
jcs: JFXConsoleStreamable[T]
|
|
) extends PrintStream(outputStream, true) {
|
|
private lazy val defaultOut = System.out
|
|
override def println(text: String): Unit =
|
|
if (config.exclusive) {
|
|
jcs.println(control, text + "\n")
|
|
} else {
|
|
defaultOut.println(text)
|
|
jcs.println(control, text + "\n")
|
|
}
|
|
override def print(text: String): Unit = jcs.println(control, text)
|
|
}
|
|
|
|
object JFXConsoleStream {
|
|
|
|
/**
|
|
* for future use
|
|
*/
|
|
case class Config(exclusive: Boolean = false)
|
|
object Config {
|
|
lazy val default = Config()
|
|
}
|
|
|
|
implicit val implJFXConsoleStreamForTextArea =
|
|
new JFXConsoleStreamable[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 textAreaStream(ta: TextArea) =
|
|
Resource.make(
|
|
Task(
|
|
new JFXConsoleStream(
|
|
outputStream = new ByteArrayOutputStream(),
|
|
control = ta
|
|
)
|
|
)
|
|
)(s => Task(s.close()))
|
|
|
|
}
|