-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.go
59 lines (53 loc) · 1.59 KB
/
print.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package block
import (
"fmt"
"io"
"strings"
"github.com/dustin/go-humanize"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-unixfsnode/data"
"github.com/ipld/go-ipld-prime/datamodel"
trustlessutils "github.com/ipld/go-trustless-utils"
)
func WritingVisitor(w io.Writer, duplicates, fullPath bool) func(p datamodel.Path, depth int, blk Block) {
var lastDepth int
seen := make(map[cid.Cid]struct{}, 0)
return func(p datamodel.Path, depth int, blk Block) {
if _, ok := seen[blk.Cid]; !duplicates && ok {
return
}
seen[blk.Cid] = struct{}{}
depthPad := strings.Repeat(" ", depth)
if depth > lastDepth {
depthPad = depthPad[:len(depthPad)-2] + "↳ "
}
fo := ""
if fullPath {
fo = fmt.Sprintf("/%s", blk.UnixfsPath.String())
} else {
fo = fmt.Sprintf("/%s", blk.UnixfsPath.Last().String())
}
if blk.ByteSize > 0 {
fo += fmt.Sprintf(" [%d:%d] (%s B)", blk.ByteOffset, blk.ByteOffset+blk.ByteSize-1, humanize.Comma(blk.ByteSize))
} else if blk.DataType == data.Data_HAMTShard && blk.ShardIndex != "" {
fo += fmt.Sprintf(" [%s]", blk.ShardIndex)
}
fmt.Fprintf(w, "%-10s | %-9s | %s%s\n", blk.Cid, blk.DataTypeString(), depthPad, fo)
lastDepth = depth
}
}
func PrintableQuery(c cid.Cid, path datamodel.Path, scope trustlessutils.DagScope, byteRange *trustlessutils.ByteRange, duplicates bool) string {
pp := path.String()
if pp != "" {
pp = "/" + pp
}
br := ""
if byteRange != nil {
br = fmt.Sprintf("&entity-bytes=%s", byteRange.String())
}
dup := ""
if !duplicates {
dup = "&dups=n"
}
return fmt.Sprintf("/ipfs/%s%s?dag-scope=%s%s%s", c, pp, scope, br, dup)
}