package xsbt.boot

import Pre._

object JAnsi {
  def uninstall(loader: ClassLoader): Unit = callJAnsi("systemUninstall", loader)
  def install(loader: ClassLoader): Unit = callJAnsi("systemInstall", loader)

  private[this] def callJAnsi(methodName: String, loader: ClassLoader): Unit = if (isWindows && !isCygwin) callJAnsiMethod(methodName, loader)
  private[this] def callJAnsiMethod(methodName: String, loader: ClassLoader): Unit =
    try {
      val c = Class.forName("org.fusesource.jansi.AnsiConsole", true, loader)
    } catch {
      case ignore: ClassNotFoundException =>
      /* The below code intentionally traps everything. It technically shouldn't trap the
				* non-StackOverflowError VirtualMachineErrors and AWTError would be weird, but this is PermGen
				* mitigation code that should not render sbt completely unusable if jansi initialization fails.
				* [From Mark Harrah,].
      case ex: Throwable                  => println("Jansi found on class path but initialization failed: " + ex)