1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 16:28:12 -07:00

Better formatting.

This commit is contained in:
Cedric Beust 2015-11-08 12:50:03 -08:00
parent 11c16b0ed1
commit 70a23f0d27
2 changed files with 39 additions and 30 deletions

View file

@ -22,7 +22,9 @@ class AsciiArt {
val banner : String get() = BANNERS.get(Random().nextInt(BANNERS.size)) val banner : String get() = BANNERS.get(Random().nextInt(BANNERS.size))
fun box(s: String) : List<String> { fun box(s: String) : List<String> = box(listOf(s))
fun box(strings: List<String>) : List<String> {
val ul = "\u2554" val ul = "\u2554"
val ur = "\u2557" val ur = "\u2557"
val h = "\u2550" val h = "\u2550"
@ -37,18 +39,27 @@ class AsciiArt {
} }
} }
return arrayListOf( val maxString: String = strings.maxBy { it.length } ?: ""
ul + r(s.length + 2, h) + ur, val max = maxString.length
"$v $s $v", val result = arrayListOf(ul + r(max + 2, h) + ur)
bl + r(s.length + 2, h) + br) 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") } val defaultLog : (s: String) -> Unit = { log(1, " $it") }
fun logBox(s: String, print: (String) -> Unit = defaultLog) {
box(s).forEach { fun logBox(strings: List<String>, print: (String) -> Unit = defaultLog) {
box(strings).forEach {
print(it) print(it)
} }
} }
fun logBox(s: String, print: (String) -> Unit = defaultLog) {
logBox(listOf(s))
}
} }
} }

View file

@ -10,46 +10,44 @@ import java.util.*
* Display information about a Maven id. * Display information about a Maven id.
*/ */
class ResolveDependency @Inject constructor(val repoFinder: RepoFinder) { class ResolveDependency @Inject constructor(val repoFinder: RepoFinder) {
val increment = 4 val increment = 8
val leftFirst = "\u2558" val leftFirst = "\u2558"
val leftMiddle = "\u255f" val leftMiddle = "\u255f"
val leftLast = "\u2559" val leftLast = "\u2559"
val vertical = "\u2551" val vertical = "\u2551"
class Dep(val dep: IClasspathDependency, val indent: Int) class Dep(val dep: IClasspathDependency, val level: Int)
fun run(id: String) { fun run(id: String) {
val indent = 0 val indent = -1
val dep = MavenDependency.create(id) val dep = MavenDependency.create(id)
val root = Node(Dep(dep, indent)) val root = Node(Dep(dep, indent))
val seen = hashSetOf<String>(id) val seen = hashSetOf<String>(id)
root.addChildren(findChildren(root, seen)) root.addChildren(findChildren(root, seen))
val repoResult = repoFinder.findCorrectRepo(id) val repoResult = repoFinder.findCorrectRepo(id)
AsciiArt.logBox(id, {s -> println(s) })
val simpleDep = SimpleDep(MavenId(id)) 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(root.children)
display(listOf(root))
} }
private fun fill(n: Int) = StringBuffer().apply { repeat(n, { append(" ")})}.toString()
private fun display(nodes: List<Node<Dep>>) { private fun display(nodes: List<Node<Dep>>) {
nodes.withIndex().forEach { indexNode -> nodes.withIndex().forEach { indexNode ->
val node = indexNode.value val node = indexNode.value
println(fill(node.value.indent) + node.value.dep.id) with(node.value) {
display(node.children) val left =
// with(node.value) { if (indexNode.index == nodes.size - 1) leftLast
// val left = else leftMiddle
// if (indexNode.index == nodes.size - 1) leftLast val indent = level * increment
// else leftMiddle for(i in 0..indent - 2) {
// for(i in 0..indent - increment) { if (i % increment == 0) print(vertical)
// if (i % increment == 0) print(vertical) else print(" ")
// else print(" ") }
// } println(left + " " + dep.id)
// println(left + " " + dep.id) display(node.children)
// display(node.children) }
// }
} }
} }
@ -58,9 +56,9 @@ class ResolveDependency @Inject constructor(val repoFinder: RepoFinder) {
val result = arrayListOf<Node<Dep>>() val result = arrayListOf<Node<Dep>>()
root.value.dep.directDependencies().forEach { root.value.dep.directDependencies().forEach {
if (! seen.contains(it.id)) { 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) 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) result.add(node)
seen.add(it.id) seen.add(it.id)
node.addChildren(findChildren(node, seen)) node.addChildren(findChildren(node, seen))