![JAR search and dependency download from the Maven repository](/logo.png)
io.vavr.render.text.PrettyPrinter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vavr-render Show documentation
Show all versions of vavr-render Show documentation
A rendering module for Vavr.io
The newest version!
/* __ __ __ __ __ ___
* \ \ / / \ \ / / __/
* \ \/ / /\ \ \/ / /
* \____/__/ \__\____/__/.ɪᴏ
* ᶜᵒᵖʸʳᶦᵍʰᵗ ᵇʸ ᵛᵃᵛʳ ⁻ ˡᶦᶜᵉⁿˢᵉᵈ ᵘⁿᵈᵉʳ ᵗʰᵉ ᵃᵖᵃᶜʰᵉ ˡᶦᶜᵉⁿˢᵉ ᵛᵉʳˢᶦᵒⁿ ᵗʷᵒ ᵈᵒᵗ ᶻᵉʳᵒ
*/
package io.vavr.render.text;
import io.vavr.collection.List;
import io.vavr.collection.Tree;
import java.util.Objects;
public class PrettyPrinter {
public static String pp(Tree tree) {
Objects.requireNonNull(tree, "tree is null");
if (tree.isEmpty()) { return "▣"; }
final StringBuilder builder = new StringBuilder();
prettyPrint((Tree.Node) tree, "", "", builder);
return builder.toString();
}
private static void prettyPrint(Tree.Node treeNode, String indent, String indent2, StringBuilder s) {
final String[] lines = treeNode.get().toString().split("\n");
s.append(lines[0]);
for (int i = 1; i < lines.length; i++) {
s.append('\n')
.append(indent2)
.append(lines[i]);
}
for (List> it = treeNode.getChildren(); it.nonEmpty(); it = it.tail()) {
final boolean isLast = it.tail().isEmpty();
s.append('\n')
.append(indent)
.append(isLast ? "└──" : "├──");
prettyPrint(it.head(), indent + (isLast ? " " : "│ "), indent + (isLast ? " " : "│ "), s);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy