github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/texttree/texttree.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package texttree
    15  
    16  const (
    17  	// TreeBody indicates the current operator sub-tree is not finished, still
    18  	// has child operators to be attached on.
    19  	TreeBody = '│'
    20  	// TreeMidbseNode indicates this operator is not the last child of the
    21  	// current sub-tree rooted by its parent.
    22  	TreeMidbseNode = '├'
    23  	// TreeLastNode indicates this operator is the last child of the current
    24  	// sub-tree rooted by its parent.
    25  	TreeLastNode = '└'
    26  	// TreeGap is used to represent the gap between the branches of the tree.
    27  	TreeGap = ' '
    28  	// TreeNodeIdentifier is used to replace the treeGap once we need to attach
    29  	// a node to a sub-tree.
    30  	TreeNodeIdentifier = '─'
    31  )
    32  
    33  // Indent4Child appends more blank to the `indent` string
    34  func Indent4Child(indent string, isLastChild bool) string {
    35  	if !isLastChild {
    36  		return string(append([]rune(indent), TreeBody, TreeGap))
    37  	}
    38  
    39  	// If the current node is the last node of the current operator tree, we
    40  	// need to end this sub-tree by changing the closest treeBody to a treeGap.
    41  	indentBytes := []rune(indent)
    42  	for i := len(indentBytes) - 1; i >= 0; i-- {
    43  		if indentBytes[i] == TreeBody {
    44  			indentBytes[i] = TreeGap
    45  			break
    46  		}
    47  	}
    48  
    49  	return string(append(indentBytes, TreeBody, TreeGap))
    50  }
    51  
    52  // PrettyIdentifier returns a pretty identifier which contains indent and tree node hierarchy indicator
    53  func PrettyIdentifier(id, indent string, isLastChild bool) string {
    54  	if len(indent) == 0 {
    55  		return id
    56  	}
    57  
    58  	indentBytes := []rune(indent)
    59  	for i := len(indentBytes) - 1; i >= 0; i-- {
    60  		if indentBytes[i] != TreeBody {
    61  			continue
    62  		}
    63  
    64  		// Here we attach a new node to the current sub-tree by changing
    65  		// the closest treeBody to a:
    66  		// 1. treeLastNode, if this operator is the last child.
    67  		// 2. treeMidbseNode, if this operator is not the last child..
    68  		if isLastChild {
    69  			indentBytes[i] = TreeLastNode
    70  		} else {
    71  			indentBytes[i] = TreeMidbseNode
    72  		}
    73  		break
    74  	}
    75  
    76  	// Replace the treeGap between the treeBody and the node to a
    77  	// treeNodeIdentifier.
    78  	indentBytes[len(indentBytes)-1] = TreeNodeIdentifier
    79  	return string(indentBytes) + id
    80  }