Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support highlight #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions modgv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,29 @@ crafted with passion by Luca Sepe - https://github.com/lucasepe/modgv`
)

var version = "0.2.0"
var highlights = ""
var printHelp bool

func init() {
flag.BoolVar(&printHelp, "h", false, "print helps.")
flag.StringVar(&highlights, "H", "", "The modules need highlight, split with comma.")
}

func main() {

flag.Usage = usage
flag.Parse()
if flag.NArg() != 0 {
if printHelp {
usage()
}

if err := modgv.Render(os.Stdin, os.Stdout); err != nil {
highlightModules := strings.Split(highlights, ",")
highlightModulesMap := make(map[string]bool, len(highlightModules))
for _, mod := range highlightModules {
highlightModulesMap[mod] = true
}

if err := modgv.Render(os.Stdin, os.Stdout, modgv.RenderOptions{HighlightModules: highlightModulesMap}); err != nil {
exitOnErr(err)
}
}
Expand All @@ -68,7 +81,7 @@ func usage() {
fmt.Fprintf(os.Stderr, "\n")

fmt.Fprintf(os.Stderr, "USAGE:\n\n")
fmt.Fprintf(os.Stderr, " go mod graph | %s | dot -Tpng -o graph.png\n\n", appName())
fmt.Fprintf(os.Stderr, " go mod graph | %s [-H some modules need highlight, split with comma.] | dot -Tpng -o graph.png\n\n", appName())

fmt.Fprintf(os.Stderr, "For each module:\n")
fmt.Fprintf(os.Stderr, " * the node representing the greatest version (i.e., the version ")
Expand Down
38 changes: 35 additions & 3 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ import (
"github.com/lucasepe/modgv/text"
)

type RenderOptions struct {
HighlightModules map[string]bool
}

// Render translates “go mod graph” output taken from
// the 'in' reader into Graphviz's DOT language, writing
// to the 'out' writer.
func Render(in io.Reader, out io.Writer) error {
func Render(in io.Reader, out io.Writer, options RenderOptions) error {
graph, err := convert(in)
if err != nil {
return err
Expand All @@ -32,11 +36,21 @@ func Render(in io.Reader, out io.Writer) error {

fmt.Fprintf(out, "\t%q [shape=underline style=\"\" fontsize=14 label=<<b>%s</b>>];\n", graph.root, graph.root)

coloring(graph, options)

for _, n := range graph.mvsPicked {
fmt.Fprintf(out, "\t%q [fillcolor=\"#0c5525\" label=<%s>];\n", n, textToHTML(n, "#fafafa"))
if options.HighlightModules[n] {
fmt.Fprintf(out, "\t%q [fillcolor=\"#ff0000\" label=<%s>];\n", n, textToHTML(n, "#fafafa"))
} else {
fmt.Fprintf(out, "\t%q [fillcolor=\"#0c5525\" label=<%s>];\n", n, textToHTML(n, "#fafafa"))
}
}
for _, n := range graph.mvsUnpicked {
fmt.Fprintf(out, "\t%q [fillcolor=\"#a3a3a3\" label=<%s>];\n", n, textToHTML(n, "#0e0e0e"))
if options.HighlightModules[n] {
fmt.Fprintf(out, "\t%q [fillcolor=\"#ff0000\" label=<%s>];\n", n, textToHTML(n, "#fafafa"))
} else {
fmt.Fprintf(out, "\t%q [fillcolor=\"#a3a3a3\" label=<%s>];\n", n, textToHTML(n, "#0e0e0e"))
}
}
out.Write(edgesAsDOT(graph))

Expand All @@ -45,6 +59,24 @@ func Render(in io.Reader, out io.Writer) error {
return nil
}

// coloring highlight all packages that reference package in options.HighlightModules
func coloring(gr *graph, options RenderOptions) {
if len(options.HighlightModules) == 0 {
return
}
finished := false
for !finished {
coloringCnt := 0
for _, e := range gr.edges {
if options.HighlightModules[e.to] && !options.HighlightModules[e.from] {
options.HighlightModules[e.from] = true
coloringCnt++
}
}
finished = coloringCnt == 0
}
}

// edgesAsDOT returns the edges in DOT notation.
func edgesAsDOT(gr *graph) []byte {
var buf bytes.Buffer
Expand Down
2 changes: 1 addition & 1 deletion render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestRender(t *testing.T) {
test.com/[email protected] test.com/[email protected]
test.com/[email protected] test.com/[email protected]
`))
if err := Render(in, out); err != nil {
if err := Render(in, out, RenderOptions{HighlightModules: map[string]bool{}}); err != nil {
t.Fatal(err)
}

Expand Down