github.com/ethersphere/bee/v2@v2.2.0/pkg/manifest/mantaray/stringer.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package mantaray
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"io"
    11  	"strconv"
    12  )
    13  
    14  //nolint:errcheck
    15  func (n *Node) String() string {
    16  	buf := bytes.NewBuffer(nil)
    17  	io.WriteString(buf, tableCharsMap["bottom-left"])
    18  	io.WriteString(buf, tableCharsMap["bottom"])
    19  	io.WriteString(buf, tableCharsMap["top-right"])
    20  	io.WriteString(buf, "\n")
    21  	nodeStringWithPrefix(n, "  ", buf)
    22  	return buf.String()
    23  }
    24  
    25  //nolint:errcheck
    26  func nodeStringWithPrefix(n *Node, prefix string, writer io.Writer) {
    27  	io.WriteString(writer, prefix)
    28  	io.WriteString(writer, tableCharsMap["left-mid"])
    29  	io.WriteString(writer, fmt.Sprintf("r: '%x'\n", n.ref))
    30  	io.WriteString(writer, prefix)
    31  	io.WriteString(writer, tableCharsMap["left-mid"])
    32  	io.WriteString(writer, fmt.Sprintf("t: '%s'", strconv.FormatInt(int64(n.nodeType), 2)))
    33  	io.WriteString(writer, " [")
    34  	if n.IsValueType() {
    35  		io.WriteString(writer, " Value")
    36  	}
    37  	if n.IsEdgeType() {
    38  		io.WriteString(writer, " Edge")
    39  	}
    40  	if n.IsWithPathSeparatorType() {
    41  		io.WriteString(writer, " PathSeparator")
    42  	}
    43  	io.WriteString(writer, " ]")
    44  	io.WriteString(writer, "\n")
    45  	io.WriteString(writer, prefix)
    46  	if len(n.forks) > 0 || len(n.metadata) > 0 {
    47  		io.WriteString(writer, tableCharsMap["left-mid"])
    48  	} else {
    49  		io.WriteString(writer, tableCharsMap["bottom-left"])
    50  	}
    51  	io.WriteString(writer, fmt.Sprintf("e: '%s'\n", string(n.entry)))
    52  	if len(n.metadata) > 0 {
    53  		io.WriteString(writer, prefix)
    54  		if len(n.forks) > 0 {
    55  			io.WriteString(writer, tableCharsMap["left-mid"])
    56  		} else {
    57  			io.WriteString(writer, tableCharsMap["bottom-left"])
    58  		}
    59  		io.WriteString(writer, fmt.Sprintf("m: '%s'\n", n.metadata))
    60  	}
    61  	counter := 0
    62  	for k, f := range n.forks {
    63  		isLast := counter != len(n.forks)-1
    64  		io.WriteString(writer, prefix)
    65  		if isLast {
    66  			io.WriteString(writer, tableCharsMap["left-mid"])
    67  		} else {
    68  			io.WriteString(writer, tableCharsMap["bottom-left"])
    69  		}
    70  		io.WriteString(writer, tableCharsMap["mid"])
    71  		io.WriteString(writer, fmt.Sprintf("[%s]", string(k)))
    72  		io.WriteString(writer, tableCharsMap["mid"])
    73  		io.WriteString(writer, tableCharsMap["top-mid"])
    74  		io.WriteString(writer, tableCharsMap["mid"])
    75  		io.WriteString(writer, fmt.Sprintf("`%s`\n", string(f.prefix)))
    76  		newPrefix := prefix
    77  		if isLast {
    78  			newPrefix += tableCharsMap["middle"]
    79  		} else {
    80  			newPrefix += " "
    81  		}
    82  		newPrefix += "     "
    83  		nodeStringWithPrefix(f.Node, newPrefix, writer)
    84  		counter++
    85  	}
    86  }
    87  
    88  var tableCharsMap = map[string]string{
    89  	"top":          "─",
    90  	"top-mid":      "┬",
    91  	"top-left":     "┌",
    92  	"top-right":    "┐",
    93  	"bottom":       "─",
    94  	"bottom-mid":   "┴",
    95  	"bottom-left":  "└",
    96  	"bottom-right": "┘",
    97  	"left":         "│",
    98  	"left-mid":     "├",
    99  	"mid":          "─",
   100  	"mid-mid":      "┼",
   101  	"right":        "│",
   102  	"right-mid":    "┤",
   103  	"middle":       "│",
   104  }