Repository: pterm/pterm
Pull request #100 (2020-10-31 16:53:26)

refactor(tree): add more spacing between levels
### Description
Added more spacing between levels and added `Tree` to demo.

### Scope
> What is affected by this pull request?

- [ ] Bug Fix
- [ ] New Feature
- [x] Documentation
- [x] Other

### To-Do Checklist
- [x] I tested my changes
- [ ] I have commented every method that I created/changed
- [ ] I updated the examples to fit with my changes
- [ ] I have added tests for my newly created methods



Patch:
diff --git a/_examples/demo/main.go b/_examples/demo/main.go
index 5a689e10..7f33c573 100644
--- a/_examples/demo/main.go
+++ b/_examples/demo/main.go
@@ -26,6 +26,9 @@ func main() {
 	pterm.DefaultSection.WithLevel(2).Println("Program Install Report")
 	installedProgramsSize()
 	time.Sleep(second * 4)
+	pterm.DefaultSection.Println("Tree Printer")
+	installedTree()
+	time.Sleep(second * 4)
 	pterm.DefaultSection.Println("TrueColor Support")
 	fadeText()
 	time.Sleep(second)
@@ -33,6 +36,32 @@ func main() {
 	listPrinter()
 }
 
+func installedTree() {
+	leveledList := pterm.LeveledList{
+		pterm.LeveledListItem{Level: 0, Text: "C:"},
+		pterm.LeveledListItem{Level: 1, Text: "Users"},
+		pterm.LeveledListItem{Level: 1, Text: "Go"},
+		pterm.LeveledListItem{Level: 1, Text: "Windows"},
+		pterm.LeveledListItem{Level: 1, Text: "Programs"},
+	}
+	for _, s := range pseudoProgramList {
+		if s != "pseudo-minecraft" {
+			leveledList = append(leveledList, pterm.LeveledListItem{Level: 2, Text: s})
+		}
+		if s == "pseudo-chrome" {
+			leveledList = append(leveledList, pterm.LeveledListItem{Level: 3, Text: "pseudo-Tabs"})
+			leveledList = append(leveledList, pterm.LeveledListItem{Level: 3, Text: "pseudo-Extensions"})
+			leveledList = append(leveledList, pterm.LeveledListItem{Level: 4, Text: "Refined GitHub"})
+			leveledList = append(leveledList, pterm.LeveledListItem{Level: 4, Text: "GitHub Notifier"})
+			leveledList = append(leveledList, pterm.LeveledListItem{Level: 4, Text: "GitHub Dark Theme"})
+			leveledList = append(leveledList, pterm.LeveledListItem{Level: 3, Text: "pseudo-Bookmarks"})
+			leveledList = append(leveledList, pterm.LeveledListItem{Level: 4, Text: "PTerm"})
+		}
+	}
+
+	pterm.DefaultTree.WithRoot(pterm.NewTreeFromLeveledList(leveledList)).Render()
+}
+
 func installingPseudoList() {
 	pterm.DefaultSection.Println("Installing pseudo programs")
 
diff --git a/_examples/tree/main.go b/_examples/tree/main.go
index a2ee3a65..8ba384b1 100644
--- a/_examples/tree/main.go
+++ b/_examples/tree/main.go
@@ -8,8 +8,8 @@ func main() {
 	// You can use a LeveledList here, for easy generation.
 	leveledList := pterm.LeveledList{
 		pterm.LeveledListItem{Level: 0, Text: "C:"},
-		pterm.LeveledListItem{Level: 1, Text: "User"},
-		pterm.LeveledListItem{Level: 1, Text: "log"},
+		pterm.LeveledListItem{Level: 1, Text: "Users"},
+		pterm.LeveledListItem{Level: 1, Text: "Windows"},
 		pterm.LeveledListItem{Level: 1, Text: "Programs"},
 		pterm.LeveledListItem{Level: 1, Text: "Programs(x86)"},
 		pterm.LeveledListItem{Level: 1, Text: "dev"},
diff --git a/tree_printer.go b/tree_printer.go
index 879db7e1..77199432 100644
--- a/tree_printer.go
+++ b/tree_printer.go
@@ -1,5 +1,9 @@
 package pterm
 
+import (
+	"strings"
+)
+
 // TreeNode is used as items in a Tree.
 type TreeNode struct {
 	Children []TreeNode
@@ -25,6 +29,7 @@ var DefaultTree = Tree{
 	TopRightDownString:   "├",
 	VerticalString:       "│",
 	RightDownLeftString:  "┬",
+	Indent:               2,
 }
 
 // Tree is able to render a list.
@@ -37,6 +42,7 @@ type Tree struct {
 	HorizontalString     string
 	VerticalString       string
 	RightDownLeftString  string
+	Indent               int
 }
 
 // WithTreeStyle returns a new list with a specific tree style.
@@ -81,6 +87,16 @@ func (p Tree) WithRoot(root TreeNode) *Tree {
 	return &p
 }
 
+// WithIndent returns a new list with a specific amount of spacing between the levels.
+// Indent must be at least 1.
+func (p Tree) WithIndent(indent int) *Tree {
+	if indent < 1 {
+		indent = 1
+	}
+	p.Indent = indent
+	return &p
+}
+
 // Render prints the list to the terminal.
 func (p Tree) Render() {
 	Println(p.Srender())
@@ -106,17 +122,21 @@ func walkOverTree(list []TreeNode, p Tree, prefix string) string {
 	for i, item := range list {
 		if len(list) > i+1 { // if not last in list
 			if len(item.Children) == 0 { // if there are no children
-				ret += prefix + p.TreeStyle.Sprint(p.TopRightDownString) + p.TreeStyle.Sprint(p.HorizontalString) + p.TextStyle.Sprint(item.Text) + "\n"
+				ret += prefix + p.TreeStyle.Sprint(p.TopRightDownString) + strings.Repeat(p.TreeStyle.Sprint(p.HorizontalString), p.Indent) +
+					p.TextStyle.Sprint(item.Text) + "\n"
 			} else { // if there are children
-				ret += prefix + p.TreeStyle.Sprint(p.TopRightDownString) + p.TreeStyle.Sprint(p.RightDownLeftString) + p.TextStyle.Sprint(item.Text) + "\n"
-				ret += walkOverTree(item.Children, p, prefix+p.TreeStyle.Sprint(p.VerticalString))
+				ret += prefix + p.TreeStyle.Sprint(p.TopRightDownString) + strings.Repeat(p.TreeStyle.Sprint(p.HorizontalString), p.Indent-1) +
+					p.TreeStyle.Sprint(p.RightDownLeftString) + p.TextStyle.Sprint(item.Text) + "\n"
+				ret += walkOverTree(item.Children, p, prefix+p.TreeStyle.Sprint(p.VerticalString)+strings.Repeat(" ", p.Indent-1))
 			}
 		} else if len(list) == i+1 { // if last in list
 			if len(item.Children) == 0 { // if there are no children
-				ret += prefix + p.TreeStyle.Sprint(p.TopRightCornerString) + p.TreeStyle.Sprint(p.HorizontalString) + p.TextStyle.Sprint(item.Text) + "\n"
+				ret += prefix + p.TreeStyle.Sprint(p.TopRightCornerString) + strings.Repeat(p.TreeStyle.Sprint(p.HorizontalString), p.Indent) +
+					p.TextStyle.Sprint(item.Text) + "\n"
 			} else { // if there are children
-				ret += prefix + p.TreeStyle.Sprint(p.TopRightCornerString) + p.TreeStyle.Sprint(p.RightDownLeftString) + p.TextStyle.Sprint(item.Text) + "\n"
-				ret += walkOverTree(item.Children, p, prefix+" ")
+				ret += prefix + p.TreeStyle.Sprint(p.TopRightCornerString) + strings.Repeat(p.TreeStyle.Sprint(p.HorizontalString), p.Indent-1) +
+					p.TreeStyle.Sprint(p.RightDownLeftString) + p.TextStyle.Sprint(item.Text) + "\n"
+				ret += walkOverTree(item.Children, p, prefix+strings.Repeat(" ", p.Indent))
 			}
 		}
 	}


Test patch:
diff --git a/tree_printer_test.go b/tree_printer_test.go
index b3e681ab..3faae376 100644
--- a/tree_printer_test.go
+++ b/tree_printer_test.go
@@ -182,3 +182,19 @@ func TestTree_WithVerticalString(t *testing.T) {
 	assert.Equal(t, "-", p2.VerticalString)
 	assert.Empty(t, p.VerticalString)
 }
+
+func TestTree_WithIndent(t *testing.T) {
+	p := Tree{}
+	p2 := p.WithIndent(3)
+
+	assert.Equal(t, 3, p2.Indent)
+	assert.Empty(t, p.Indent)
+}
+
+func TestTree_WithIndentInvalid(t *testing.T) {
+	p := Tree{}
+	p2 := p.WithIndent(0)
+
+	assert.Equal(t, 1, p2.Indent)
+	assert.Empty(t, p.Indent)
+}