Swiftpack.co - cliss/TreePrinter as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by cliss.
cliss/TreePrinter 1.0.2
Swift Package to pretty print tree-like structures
⭐️ 32
πŸ•“ 4 years ago
.package(url: "https://github.com/cliss/TreePrinter.git", from: "1.0.2")

TreePrinter

A small library to convert a tree structure into a String, designed to be used for debugging purposes.

Quick Start

  1. Include the framework in your project
  2. Conform your type to be TreeRepresentable
  3. let treeString = TreePrinter.printTree(root: rootNodeOfYourTree)

Example

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

Options

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)

Installation

Use SPM

  1. In Xcode 11+, File β†’ Swift Packages β†’ Add Package Dependency
  2. Add the URL for this repository: https://github.com/cliss/TreePrinter.git

Add files

  1. Make copies of Sources/TreePrinter/TreeRepresentable.swift and Sources/TreePrinter/TreePrinter.swift
  2. Add them to your project

GitHub

link
Stars: 32
Last commit: 4 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Related Packages

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics