diff --git a/src/main/scala/net/virtualvoid/sbt/graph/util/AsciiTreeLayout.scala b/src/main/scala/net/virtualvoid/sbt/graph/util/AsciiTreeLayout.scala index f9f0125..c7f8e63 100644 --- a/src/main/scala/net/virtualvoid/sbt/graph/util/AsciiTreeLayout.scala +++ b/src/main/scala/net/virtualvoid/sbt/graph/util/AsciiTreeLayout.scala @@ -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 = { diff --git a/src/test/scala/net/virtualvoid/sbt/graph/util/AsciiTreeLayoutSpecs.scala b/src/test/scala/net/virtualvoid/sbt/graph/util/AsciiTreeLayoutSpecs.scala index ba1d7d0..cd110c7 100644 --- a/src/test/scala/net/virtualvoid/sbt/graph/util/AsciiTreeLayoutSpecs.scala +++ b/src/test/scala/net/virtualvoid/sbt/graph/util/AsciiTreeLayoutSpecs.scala @@ -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 } } }