Skip to content
This repository has been archived by the owner on Apr 8, 2021. It is now read-only.

Added support for overriding column length: Resolves issue 173 #174

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package rendering

import com.github.mdr.ascii.layout._
import net.virtualvoid.sbt.graph.DependencyGraphKeys._
import sbt.dependencygraph.DependencyGraphSbtCompat.Settings.asciiGraphMaxColumnWidth
import sbt.Keys._

object AsciiGraph {
Expand All @@ -42,14 +43,15 @@ object AsciiGraph {
DependencyGraphKeys.asciiGraph := asciiGraph(moduleGraph.value),
dependencyGraph := {
val force = DependencyGraphSettings.shouldForceParser.parsed
val columnWidth = asciiGraphMaxColumnWidth.value
val log = streams.value.log
if (force || moduleGraph.value.nodes.size < 15) {
log.info(rendering.AsciiGraph.asciiGraph(moduleGraph.value))
log.info("\n\n")
log.info("Note: The old tree layout is still available by using `dependency-tree`")
}

log.info(rendering.AsciiTree.asciiTree(moduleGraph.value))
log.info(rendering.AsciiTree.asciiTree(moduleGraph.value, columnWidth))

if (!force) {
log.info("\n")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sbt
package dependencygraph

import scala.util.Try
import scala.language.implicitConversions

object DependencyGraphSbtCompat {
Expand All @@ -12,4 +13,18 @@ object DependencyGraphSbtCompat {
updateConfig.copy(missingOk = missingOk)
}
}

object Settings {
private val sbtAsciiGraphWidth = "SBT_ASCII_GRAPH_WIDTH"

val asciiGraphMaxColumnWidth = Def.setting(defaultColumnSize)

private def defaultColumnSize: Int = {
val envAsciiWidth = sys.env.get(sbtAsciiGraphWidth).flatMap(s ⇒ Try(s.toInt).toOption)
val termWidth = envAsciiWidth.getOrElse(SbtAccess.getTerminalWidth)
if (termWidth > 20) termWidth - 8
else 80 // ignore termWidth
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ package dependencygraph

object DependencyGraphSbtCompat {
object Implicits

object Settings {
val asciiGraphMaxColumnWidth = Def.setting(sbt.Keys.asciiGraphWidth.value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import internal.librarymanagement._
import librarymanagement._
import sbt.dependencygraph.SbtAccess
import sbt.dependencygraph.DependencyGraphSbtCompat.Implicits._
import sbt.dependencygraph.DependencyGraphSbtCompat.Settings.asciiGraphMaxColumnWidth
import sbt.complete.Parsers

object DependencyGraphSettings {
Expand All @@ -48,12 +49,12 @@ object DependencyGraphSettings {
def reportSettings =
Seq(Compile, Test, IntegrationTest, Runtime, Provided, Optional).flatMap(ivyReportForConfig)

val renderingAlternatives: Seq[(TaskKey[Unit], ModuleGraph ⇒ String)] =
val renderingAlternatives: Seq[(TaskKey[Unit], Def.Initialize[Task[ModuleGraph ⇒ String]])] =
Seq(
dependencyTree -> rendering.AsciiTree.asciiTree _,
dependencyList -> rendering.FlatList.render(_.id.idString),
dependencyStats -> rendering.Statistics.renderModuleStatsList _,
licenseInfo -> rendering.LicenseInfo.render _)
dependencyTree -> Def.task(rendering.AsciiTree.asciiTree(_, asciiGraphMaxColumnWidth.value)),
dependencyList -> Def.task(rendering.FlatList.render(_.id.idString)),
dependencyStats -> Def.task(rendering.Statistics.renderModuleStatsList _),
licenseInfo -> Def.task(rendering.LicenseInfo.render _))

def ivyReportForConfig(config: Configuration) = inConfig(config)(
Seq(
Expand Down Expand Up @@ -107,6 +108,7 @@ object DependencyGraphSettings {

whatDependsOn := {
val ArtifactPattern(org, name, versionFilter) = artifactPatternParser.parsed
val maxColumnSize = asciiGraphMaxColumnWidth.value
val graph = moduleGraph.value
val modules =
versionFilter match {
Expand All @@ -116,7 +118,7 @@ object DependencyGraphSettings {
val output =
modules
.map { module ⇒
rendering.AsciiTree.asciiTree(GraphTransformations.reverseGraphStartingAt(graph, module))
rendering.AsciiTree.asciiTree(GraphTransformations.reverseGraphStartingAt(graph, module), maxColumnSize)
}
.mkString("\n")

Expand All @@ -128,9 +130,9 @@ object DependencyGraphSettings {
renderingAlternatives.flatMap((renderingTaskSettings _).tupled) ++
AsciiGraph.asciiGraphSetttings)

def renderingTaskSettings(key: TaskKey[Unit], renderer: ModuleGraph ⇒ String): Seq[Setting[_]] =
def renderingTaskSettings(key: TaskKey[Unit], renderer: Def.Initialize[Task[ModuleGraph ⇒ String]]): Seq[Setting[_]] =
Seq(
asString in key := renderer(moduleGraph.value),
asString in key := renderer.value(moduleGraph.value),
printToConsole in key := streams.value.log.info((asString in key).value),
toFile in key := {
val (targetFile, force) = targetFileAndForceParser.parsed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import util.AsciiTreeLayout
import util.ConsoleUtils._

object AsciiTree {
def asciiTree(graph: ModuleGraph): String = {
def asciiTree(graph: ModuleGraph, maxColumnWidth: Int): String = {
val deps = graph.dependencyMap

// there should only be one root node (the project itself)
val roots = graph.roots
roots.map { root ⇒
AsciiTreeLayout.toAscii[Module](root, node ⇒ deps.getOrElse(node.id, Seq.empty[Module]), displayModule)
AsciiTreeLayout.toAscii[Module](root, node ⇒ deps.getOrElse(node.id, Seq.empty[Module]), displayModule, maxColumnWidth)
}.mkString("\n")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object AsciiTreeLayout {
top: A,
children: A ⇒ Seq[A],
display: A ⇒ String,
maxColumn: Int = defaultColumnSize): String = {
maxColumn: Int): String = {
val twoSpaces = " " + " " // prevent accidentally being converted into a tab
def limitLine(s: String): String =
if (s.length > maxColumn) s.slice(0, maxColumn - 2) + ".."
Expand Down Expand Up @@ -53,10 +53,4 @@ object AsciiTreeLayout {

toAsciiLines(top, 0, Set.empty).mkString("\n")
}

def defaultColumnSize: Int = {
val termWidth = SbtAccess.getTerminalWidth
if (termWidth > 20) termWidth - 8
else 80 // ignore termWidth
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class AsciiTreeLayoutSpecs extends Specification {
4 -> Seq(3),
5 -> Seq(1, 4, 6, 7),
6 -> Seq(),
7 -> Seq()), _.toString).trim ===
7 -> Seq()), _.toString, 80).trim ===
"""1
| +-2
| | +-4
Expand Down