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

Commit

Permalink
allow cycles in graphs even when printing with AsciiTreeLayout, fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
jrudolph committed Nov 17, 2015
1 parent 5f3c2c2 commit 0c3cf98
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
31 changes: 19 additions & 12 deletions src/main/scala/net/virtualvoid/sbt/graph/util/AsciiTreeLayout.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,27 @@ object AsciiTreeLayout {
}) +
s.slice(at + 1, s.length)
else s
def toAsciiLines(node: A, level: Int): Vector[String] = {
val line = limitLine((twoSpaces * level) + (if (level == 0) "" else "+-") + display(node))
val cs = Vector(children(node): _*)
val childLines = cs map {toAsciiLines(_, level + 1)}
val withBar = childLines.zipWithIndex flatMap {
case (lines, pos) if pos < (cs.size - 1) => lines map {insertBar(_, 2 * (level + 1))}
case (lines, pos) =>
if (lines.last.trim != "") lines ++ Vector(twoSpaces * (level + 1))
else lines
def toAsciiLines(node: A, level: Int, parents: Set[A]): Vector[String] =
if (parents contains node) // cycle
Vector(limitLine((twoSpaces * level) + "#-" + display(node)))
else {
val line = limitLine((twoSpaces * level) + (if (level == 0) "" else "+-") + display(node))
val cs = Vector(children(node): _*)
val childLines = cs map {
toAsciiLines(_, level + 1, parents + node)
}
val withBar = childLines.zipWithIndex flatMap {
case (lines, pos) if pos < (cs.size - 1) => lines map {
insertBar(_, 2 * (level + 1))
}
case (lines, pos) =>
if (lines.last.trim != "") lines ++ Vector(twoSpaces * (level + 1))
else lines
}
line +: withBar
}
line +: withBar
}

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

def defaultColumnSize: Int = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,18 @@ class AsciiTreeLayoutSpecs extends Specification {
| +-2
| | +-4
| | | +-3
| | |
| | |\u0020
| | +-5
| | #-1
| | #-4
| | +-4
| | | +-3
| | |\u0020
| | +-6
| | +-7
| |
| #-3
| #-4""".stripMargin.trim
| |\u0020\u0020\u0020
| +-3
| +-4
| +-3""".stripMargin.trim
}
}
}

0 comments on commit 0c3cf98

Please sign in to comment.