From 70a23f0d279930410e84a468a6d2ddd55a31f592 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Sun, 8 Nov 2015 12:50:03 -0800 Subject: [PATCH] Better formatting. --- src/main/kotlin/com/beust/kobalt/AsciiArt.kt | 25 ++++++++--- .../com/beust/kobalt/ResolveDependency.kt | 44 +++++++++---------- 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/src/main/kotlin/com/beust/kobalt/AsciiArt.kt b/src/main/kotlin/com/beust/kobalt/AsciiArt.kt index 15c7c6ea..15551b4e 100644 --- a/src/main/kotlin/com/beust/kobalt/AsciiArt.kt +++ b/src/main/kotlin/com/beust/kobalt/AsciiArt.kt @@ -22,7 +22,9 @@ class AsciiArt { val banner : String get() = BANNERS.get(Random().nextInt(BANNERS.size)) - fun box(s: String) : List { + fun box(s: String) : List = box(listOf(s)) + + fun box(strings: List) : List { val ul = "\u2554" val ur = "\u2557" val h = "\u2550" @@ -37,18 +39,27 @@ class AsciiArt { } } - return arrayListOf( - ul + r(s.length + 2, h) + ur, - "$v $s $v", - bl + r(s.length + 2, h) + br) + val maxString: String = strings.maxBy { it.length } ?: "" + val max = maxString.length + val result = arrayListOf(ul + r(max + 2, h) + ur) + result.addAll(strings.map { "$v $it ${fill(max - it.length)}$v" }) + result.add(bl + r(max + 2, h) + br) + return result } + private fun fill(n: Int) = StringBuffer().apply { repeat(n, { append(" ")})}.toString() + val defaultLog : (s: String) -> Unit = { log(1, " $it") } - fun logBox(s: String, print: (String) -> Unit = defaultLog) { - box(s).forEach { + + fun logBox(strings: List, print: (String) -> Unit = defaultLog) { + box(strings).forEach { print(it) } } + + fun logBox(s: String, print: (String) -> Unit = defaultLog) { + logBox(listOf(s)) + } } } diff --git a/src/main/kotlin/com/beust/kobalt/ResolveDependency.kt b/src/main/kotlin/com/beust/kobalt/ResolveDependency.kt index 2ed52fcf..204c09dd 100644 --- a/src/main/kotlin/com/beust/kobalt/ResolveDependency.kt +++ b/src/main/kotlin/com/beust/kobalt/ResolveDependency.kt @@ -10,46 +10,44 @@ import java.util.* * Display information about a Maven id. */ class ResolveDependency @Inject constructor(val repoFinder: RepoFinder) { - val increment = 4 + val increment = 8 val leftFirst = "\u2558" val leftMiddle = "\u255f" val leftLast = "\u2559" val vertical = "\u2551" - class Dep(val dep: IClasspathDependency, val indent: Int) + class Dep(val dep: IClasspathDependency, val level: Int) fun run(id: String) { - val indent = 0 + val indent = -1 val dep = MavenDependency.create(id) val root = Node(Dep(dep, indent)) val seen = hashSetOf(id) root.addChildren(findChildren(root, seen)) val repoResult = repoFinder.findCorrectRepo(id) - AsciiArt.logBox(id, {s -> println(s) }) + val simpleDep = SimpleDep(MavenId(id)) + val url = "Full URL: " + repoResult.repoUrl + simpleDep.toJarFile(repoResult) + AsciiArt.logBox(listOf(id, url), {s -> println(s) }) - println("Full URL: " + repoResult.repoUrl + simpleDep.toJarFile(repoResult)) - display(listOf(root)) + display(root.children) } - private fun fill(n: Int) = StringBuffer().apply { repeat(n, { append(" ")})}.toString() - private fun display(nodes: List>) { nodes.withIndex().forEach { indexNode -> val node = indexNode.value - println(fill(node.value.indent) + node.value.dep.id) - display(node.children) -// with(node.value) { -// val left = -// if (indexNode.index == nodes.size - 1) leftLast -// else leftMiddle -// for(i in 0..indent - increment) { -// if (i % increment == 0) print(vertical) -// else print(" ") -// } -// println(left + " " + dep.id) -// display(node.children) -// } + with(node.value) { + val left = + if (indexNode.index == nodes.size - 1) leftLast + else leftMiddle + val indent = level * increment + for(i in 0..indent - 2) { + if (i % increment == 0) print(vertical) + else print(" ") + } + println(left + " " + dep.id) + display(node.children) + } } } @@ -58,9 +56,9 @@ class ResolveDependency @Inject constructor(val repoFinder: RepoFinder) { val result = arrayListOf>() root.value.dep.directDependencies().forEach { if (! seen.contains(it.id)) { - val dep = Dep(it, root.value.indent + increment) + val dep = Dep(it, root.value.level + 1) val node = Node(dep) - log(2, "Found dependency ${dep.dep.id} indent: ${dep.indent}") + log(2, "Found dependency ${dep.dep.id} level: ${dep.level}") result.add(node) seen.add(it.id) node.addChildren(findChildren(node, seen))