All Downloads are FREE. Search and download functionalities are using the official Maven repository.

coursier.util.Tree.scala Maven / Gradle / Ivy

There is a newer version: 1.1.0-M7
Show newest version
package coursier.util

import scala.collection.mutable.ArrayBuffer

object Tree {

  def apply[T](roots: IndexedSeq[T])(children: T => Seq[T], print: T => String): String = {

    def helper(elems: Seq[T], prefix: String, acc: String => Unit): Unit =
      for ((elem, idx) <- elems.zipWithIndex) {
        val isLast = idx == elems.length - 1

        val tee = if (isLast) "└─ " else "├─ "

        acc(prefix + tee + print(elem))

        val children0 = children(elem)

        if (children0.nonEmpty) {
          val extraPrefix = if (isLast) "   " else "|  "
          helper(children0, prefix + extraPrefix, acc)
        }
      }

    val b = new ArrayBuffer[String]
    helper(roots, "", b += _)
    b.mkString("\n")
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy