<commit_before>/*
 * sbt
 * Copyright 2011 - 2018, Lightbend, Inc.
 * Copyright 2008 - 2010, <NAME>
 * Licensed under Apache License 2.0 (see LICENSE)
 */

package sbt.internal.util

import java.io.{ InputStream, OutputStream, PrintWriter }
import java.nio.charset.Charset
import java.util.{ Arrays, EnumSet }
import java.util.concurrent.atomic.{ AtomicBoolean, AtomicReference }
import org.jline.utils.InfoCmp.Capability
import org.jline.utils.{ ClosedException, NonBlockingReader }
import org.jline.terminal.{ Attributes, Size, Terminal => JTerminal }
import org.jline.terminal.Attributes.{ InputFlag, LocalFlag }
import org.jline.terminal.Terminal.SignalHandler
import org.jline.terminal.impl.AbstractTerminal
import org.jline.terminal.impl.jansi.JansiSupportImpl
import org.jline.terminal.impl.jansi.win.JansiWinSysTerminal
import org.jline.utils.OSUtils
import scala.collection.JavaConverters._
import scala.util.Try
import java.util.concurrent.LinkedBlockingQueue
import org.fusesource.jansi.internal.WindowsSupport

private[sbt] object JLine3 {
  private[util] val initialAttributes = new AtomicReference[Attributes]

  private[this] val forceWindowsJansiHolder = new AtomicBoolean(false)
  private[sbt] def forceWindowsJansi(): Unit = forceWindowsJansiHolder.set(true)
  private[this] def windowsJansi(): org.jline.terminal.Terminal = {
    val support = new JansiSupportImpl
    val winConsole = support.isWindowsConsole();
    val termType = sys.props.get("org.jline.terminal.type").orElse(sys.env.get("TERM")).orNull
    val term = JansiWinSysTerminal.createTerminal(
      "console",
      termType,
      OSUtils.IS_CONEMU,
      Charset.forName("UTF-8"),
      -1,
      false,
      SignalHandler.SIG_DFL,
      true
    )
    term.disableScrolling()
    term
  }
  private val jansi = {
    val (major, minor) =
      (JansiSupportImpl.getJansiMajorVersion, JansiSupportImpl.getJansiMinorVersion)
    (major > 1 || minor >= 18) && Util.isWindows
  }
  private[util] def system: org.jline.terminal.Terminal = {
    val term =
      if (forceWindowsJansiHolder.get) windowsJansi()
      else {
        // Only use jna on windows. Both jna and jansi use illegal reflective
        // accesses on posix system.
        org.jline.terminal.TerminalBuilder
          .builder()
          .system(System.console != null)
          .jna(Util.isWindows && !jansi)
          .jansi(jansi)
          .paused(true)
          .build()
      }
    initialAttributes.get match {
      case null => initialAttributes.set(term.getAttributes)
      case _    =>
    }
    term
  }
  private[sbt] def apply(term: Terminal): JTerminal = {
    new AbstractTerminal(
      term.name,
      "nocapabilities",
      Charset.forName("UTF-8"),
      SignalHandler.SIG_DFL
    ) {
      val closed = new AtomicBoolean(false)
      setOnClose { () =>
        doClose()
        reader.close()
        if (closed.compareAndSet(false, true)) {
          // This is necessary to shutdown the non blocking input reader
          // so that it doesn't keep blocking
          term.inputStream match {
            case w: Terminal.WriteableInputStream => w.cancel()
            case _                                =>
          }
        }
      }<commit_msg>Revert "Don't ever use jline 3 dumb terminal"<commit_after>/*
 * sbt
 * Copyright 2011 - 2018, Lightbend, Inc.
 * Copyright 2008 - 2010, <NAME>
 * Licensed under Apache License 2.0 (see LICENSE)
 */

package sbt.internal.util

import java.io.{ InputStream, OutputStream, PrintWriter }
import java.nio.charset.Charset
import java.util.{ Arrays, EnumSet }
import java.util.concurrent.atomic.{ AtomicBoolean, AtomicReference }
import org.jline.utils.InfoCmp.Capability
import org.jline.utils.{ ClosedException, NonBlockingReader }
import org.jline.terminal.{ Attributes, Size, Terminal => JTerminal }
import org.jline.terminal.Attributes.{ InputFlag, LocalFlag }
import org.jline.terminal.Terminal.SignalHandler
import org.jline.terminal.impl.{ AbstractTerminal, DumbTerminal }
import org.jline.terminal.impl.jansi.JansiSupportImpl
import org.jline.terminal.impl.jansi.win.JansiWinSysTerminal
import org.jline.utils.OSUtils
import scala.collection.JavaConverters._
import scala.util.Try
import java.util.concurrent.LinkedBlockingQueue
import org.fusesource.jansi.internal.WindowsSupport

private[sbt] object JLine3 {
  private[util] val initialAttributes = new AtomicReference[Attributes]

  private[this] val forceWindowsJansiHolder = new AtomicBoolean(false)
  private[sbt] def forceWindowsJansi(): Unit = forceWindowsJansiHolder.set(true)
  private[this] def windowsJansi(): org.jline.terminal.Terminal = {
    val support = new JansiSupportImpl
    val winConsole = support.isWindowsConsole();
    val termType = sys.props.get("org.jline.terminal.type").orElse(sys.env.get("TERM")).orNull
    val term = JansiWinSysTerminal.createTerminal(
      "console",
      termType,
      OSUtils.IS_CONEMU,
      Charset.forName("UTF-8"),
      -1,
      false,
      SignalHandler.SIG_DFL,
      true
    )
    term.disableScrolling()
    term
  }
  private val jansi = {
    val (major, minor) =
      (JansiSupportImpl.getJansiMajorVersion, JansiSupportImpl.getJansiMinorVersion)
    (major > 1 || minor >= 18) && Util.isWindows
  }
  private[util] def system: org.jline.terminal.Terminal = {
    val term =
      if (forceWindowsJansiHolder.get) windowsJansi()
      else {
        // Only use jna on windows. Both jna and jansi use illegal reflective
        // accesses on posix system.
        org.jline.terminal.TerminalBuilder
          .builder()
          .system(System.console != null)
          .jna(Util.isWindows && !jansi)
          .jansi(jansi)
          .paused(true)
          .build()
      }
    initialAttributes.get match {
      case null => initialAttributes.set(term.getAttributes)
      case _    =>
    }
    term
  }
  private[sbt] def apply(term: Terminal): JTerminal = {
    if (System.getProperty("jline.terminal", "") == "none")
      new DumbTerminal(term.inputStream, term.outputStream)
    else wrapTerminal(term)
  }
  private[this] def wrapTerminal(term: Terminal): JTerminal = {
    new AbstractTerminal(
      term.name,
      "nocapabilities",
      Charset.forName("UTF-8"),
      SignalHandler.SIG_DFL
    ) {
      val closed = new AtomicBoolean(false)
      setOnClose { () =>
        doClose()
        reader.close()
        if (closed.compareAndSet(false, true)) {
          // This is necessary to shutdown the non blocking input reader
          // so that it doesn't keep blocking
          term.inputStream match {
            case w: Terminal.WriteableInputStream => w.cancel()
            case _                                =>
          }
        }
      }
{'fraction_non_alphanumeric': 0.10509931811443819, 'fraction_numerical': 0.007560035576638008, 'mean_word_length': 2.9158444573418456, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4670354', 'n_tokens_mistral': 2173, 'n_tokens_neox': 2072, 'n_words': 448}