Skip to content

Commit

Permalink
use dotted line to indicate node has multiple parents (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
climech committed Apr 5, 2021
1 parent 0b4738f commit 53b8d5b
Showing 1 changed file with 11 additions and 23 deletions.
34 changes: 11 additions & 23 deletions multitree/repr.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,13 @@ func (n *Node) String() string {
return fmt.Sprintf("%s %s %s", accent(n.checkbox()), name, accent(id))
}

type treeIndent int

const (
treeIndentBlank = iota
treeIndentExtend
treeIndentSplit
treeIndentTerminate
treeIndentBlank = " "
treeIndentExtend = " │ "
treeIndentSplit = " ├──"
treeIndentTerminate = " └──"
)

func (i treeIndent) String() string {
switch i {
case treeIndentBlank:
return " "
case treeIndentExtend:
return " │ "
case treeIndentSplit:
return " ├──"
case treeIndentTerminate:
return " └──"
default:
panic("invalid treeIndent value")
}
}

// StringTree returns a string representation of a tree rooted at n.
//
// [~] Clean up the house (234)
Expand All @@ -94,7 +77,7 @@ func (n *Node) StringTree() string {
// line should be extended or "split". Otherwise, the line should be
// terminated or left blank.
traverse = func(n *Node, stack []bool) {
var indents []treeIndent
var indents []string

if len(stack) != 0 {
// Previous levels -- extend or leave blank.
Expand All @@ -111,10 +94,15 @@ func (n *Node) StringTree() string {
} else {
indents = append(indents, treeIndentTerminate)
}
// Change to "dotted line" if node has multiple parents.
if len(n.parents) > 1 {
i := len(indents) - 1
indents[i] = string([]rune(indents[i])[:2]) + "··"
}
}

for _, i := range indents {
sb.WriteString(i.String())
sb.WriteString(i)
}
sb.WriteString(n.String())
sb.WriteString("\n")
Expand Down

0 comments on commit 53b8d5b

Please sign in to comment.