mirror of
https://github.com/grdl/git-get.git
synced 2026-02-04 19:09:45 +00:00
Fix tree view indentation
This commit is contained in:
@@ -138,12 +138,7 @@ func (p *TreePrinter) printTree(node *Node, tp treeprint.Tree) {
|
|||||||
status = green("ok")
|
status = green("ok")
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: This was rushed, see if there's a cleaner way to do it.
|
str.WriteString(fmt.Sprintf("\n%s%s %s", indentation(node), blue(branch), yellow(status)))
|
||||||
pipes := nextRowDepth(node)
|
|
||||||
spaces := node.depth - pipes
|
|
||||||
indentation := strings.Repeat("│ ", pipes) + strings.Repeat(" ", 4*spaces) + strings.Repeat(" ", len(node.val)+1)
|
|
||||||
|
|
||||||
str.WriteString(fmt.Sprintf("\n%s%s %s", indentation, blue(branch), yellow(status)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tp.SetValue(str.String())
|
tp.SetValue(str.String())
|
||||||
@@ -155,25 +150,41 @@ func (p *TreePrinter) printTree(node *Node, tp treeprint.Tree) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// nextRowDepth returns a depth level of the node that will be printed on the next row.
|
// indentation generates a correct indentation for the branches row to match the links to lower rows.
|
||||||
// It traverses the tree "upwards" and checks if a node is the youngest one (are there no more siblings in its parent's children slice).
|
// It traverses the tree "upwards" and checks if a parent node is the youngest one (ie, there are no more sibling at the same level).
|
||||||
func nextRowDepth(node *Node) int {
|
// If it is, it means that level should be indented with empty spaces because there is nothing to link to anymore.
|
||||||
depth := node.depth
|
// If it isn't the youngest, that level needs to be indented using a "|" link.
|
||||||
n := node
|
func indentation(node *Node) string {
|
||||||
for n.amIYoungest() {
|
// Slice of levels. Slice index is node depth, true value means the node is the youngest.
|
||||||
n = n.parent
|
levels := make([]bool, node.depth)
|
||||||
if n == nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
depth--
|
// Traverse until node has no parents (ie, we reached the root).
|
||||||
|
n := node
|
||||||
|
for n.parent != nil {
|
||||||
|
levels[n.depth-1] = n.isYoungest()
|
||||||
|
n = n.parent
|
||||||
}
|
}
|
||||||
|
|
||||||
return depth
|
var indent strings.Builder
|
||||||
|
|
||||||
|
const space = " "
|
||||||
|
const link = "│ "
|
||||||
|
for _, y := range levels {
|
||||||
|
if y {
|
||||||
|
indent.WriteString(space)
|
||||||
|
} else {
|
||||||
|
indent.WriteString(link)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finally, indent by the size of node name (to match the rest of the branches)
|
||||||
|
indent.WriteString(strings.Repeat(" ", len(node.val)+1))
|
||||||
|
|
||||||
|
return indent.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// amIYoungest checks if the node is the last one in the slice of children
|
// isYoungest checks if the node is the last one in the slice of children
|
||||||
func (n *Node) amIYoungest() bool {
|
func (n *Node) isYoungest() bool {
|
||||||
if n.parent == nil {
|
if n.parent == nil {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user