Skip to content

Commit

Permalink
nicer dependencies tree
Browse files Browse the repository at this point in the history
  • Loading branch information
chavacava committed Mar 2, 2019
1 parent fec4dc3 commit 4469463
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions cmd/depth/depth.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (
)

const (
outputPadding = " "
outputPrefix = "├ "
outputPrefixLast = "└ "
outputClosedPadding = " "
outputOpenPadding = "│ "
outputPrefix = "├ "
outputPrefixLast = "└ "
)

var outputJSON bool
Expand Down Expand Up @@ -70,7 +71,7 @@ func handlePkgs(t *depth.Tree, pkgs []string, outputJSON bool, explainPkg string
continue
}

writePkg(os.Stdout, *t.Root, 0, false)
writePkg(os.Stdout, *t.Root)
writePkgSummary(os.Stdout, *t.Root)
}
return nil
Expand Down Expand Up @@ -114,21 +115,39 @@ func writePkgJSON(w io.Writer, p depth.Pkg) {
e.Encode(p)
}

func writePkg(w io.Writer, p depth.Pkg) {
fmt.Fprintf(w, "%s\n", p.String())

for idx, d := range p.Deps {
writePkgRec(w, d, []bool{true}, idx == len(p.Deps)-1)
}
}

// writePkg recursively prints a Pkg and its dependencies to the Writer provided.
func writePkg(w io.Writer, p depth.Pkg, indent int, isLast bool) {
func writePkgRec(w io.Writer, p depth.Pkg, closed []bool, isLast bool) {
var prefix string
if indent > 0 {
prefix = outputPrefix

if isLast {
prefix = outputPrefixLast
for _, c := range closed {
if c {
prefix += outputClosedPadding
continue
}

prefix += outputOpenPadding
}

closed = append(closed, false)
if isLast {
prefix += outputPrefixLast
closed[len(closed)-1] = true
} else {
prefix += outputPrefix
}

fmt.Fprintf(w, "%v%v%v\n", strings.Repeat(outputPadding, indent), prefix, p.String())
fmt.Fprintf(w, "%v%v\n", prefix, p.String())

for idx, d := range p.Deps {
writePkg(w, d, indent+1, idx == len(p.Deps)-1)
writePkgRec(w, d, closed, idx == len(p.Deps)-1)
}
}

Expand Down

0 comments on commit 4469463

Please sign in to comment.