A small library to convert a tree structure into a String
, designed to be used for debugging purposes.
TreeRepresentable
let treeString = TreePrinter.printTree(root: rootNodeOfYourTree)
Say you already have some structure that represents a tree:
struct SomeTreeStructure {
let title: String
let childNodes: [SomeTreeStructure]
}
You start by confirming your type to TreeRepresentable
:
extension SomeTreeStructure: TreeRepresentable {
var name: String { return self.title }
var subnodes: [SomeTreeStructure] { return self.childNodes }
}
Then, you can easily debug a tree as such:
func someFunctionYoureTesting() {
var treeRoot: SomeTreeStructure = /* Your tree */
print(TreePrinter.printTree(root: treeRoot))
}
By default, for the tree that is used for the purposes of unit testing, the result will be:
Root
βββ Branch Depth One A
β βββ Branch Depth Two A
β βββ Branch Depth Two B
β β βββ Leaf Depth Three
β βββ Branch Depth Two C
βββ Branch Depth One B
TreePrinter
will use a sensible set of default options, however, many things are customizable:
spacesPerDepth
β Amount of indentation; defaults to 4
.spacer
βΒ Actual spacer; defaults to
verticalLine
βΒ Vertical line; defaults to β
intermediateConnector
βΒ Connects a node that is not the leaf to the node above it; defaults to β
finalConnector
βΒ Connects a leaf node to the node above it; defaults to β
connectorSuffix
β Suffix after the connector; defaults to ββ
To customize any part of the tree print, create an instance of TreePrinter.TreePrinterOptions
and
pass alternates for any of the above. The rest, if not provided, will use the default values. Then print the tree
as such:
TreePrinter.printTree(root: treeRoot, options: treePrinterOptions)
File
β Swift Packages
β Add Package Dependency
https://github.com/cliss/TreePrinter.git
Sources/TreePrinter/TreeRepresentable.swift
and Sources/TreePrinter/TreePrinter.swift
link |
Stars: 32 |
Last commit: 4 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics