From 9ba8fca1c401499a264f74185b8ce5f9e5840ca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Czhouchaofan=E2=80=9D?= <“zhoucf@codoon.com”> Date: Fri, 17 Jun 2022 17:15:30 +0800 Subject: [PATCH 1/3] support high light --- modgv/main.go | 17 +++++++++++++++-- render.go | 38 +++++++++++++++++++++++++++++++++++--- render_test.go | 2 +- 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/modgv/main.go b/modgv/main.go index 64bbd7c..278791b 100644 --- a/modgv/main.go +++ b/modgv/main.go @@ -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 packages need high light color, 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 { + highlightPackages := strings.Split(highlights, ",") + highlightPackagesMap := make(map[string]bool, len(highlightPackages)) + for _, pkg := range highlightPackages { + highlightPackagesMap[pkg] = true + } + + if err := modgv.Render(os.Stdin, os.Stdout, modgv.RenderOptions{HighlightPackages: highlightPackagesMap}); err != nil { exitOnErr(err) } } diff --git a/render.go b/render.go index 915eb7c..bbfc55a 100644 --- a/render.go +++ b/render.go @@ -13,10 +13,14 @@ import ( "github.com/lucasepe/modgv/text" ) +type RenderOptions struct { + HighlightPackages 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 @@ -32,11 +36,21 @@ func Render(in io.Reader, out io.Writer) error { fmt.Fprintf(out, "\t%q [shape=underline style=\"\" fontsize=14 label=<%s>];\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.HighlightPackages[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.HighlightPackages[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)) @@ -45,6 +59,24 @@ func Render(in io.Reader, out io.Writer) error { return nil } +// coloring highlight all packages that reference package in options.HighlightPackages +func coloring(gr *graph, options RenderOptions) { + if len(options.HighlightPackages) == 0 { + return + } + finished := false + for !finished { + coloringCnt := 0 + for _, e := range gr.edges { + if options.HighlightPackages[e.to] && !options.HighlightPackages[e.from] { + options.HighlightPackages[e.from] = true + coloringCnt++ + } + } + finished = coloringCnt == 0 + } +} + // edgesAsDOT returns the edges in DOT notation. func edgesAsDOT(gr *graph) []byte { var buf bytes.Buffer diff --git a/render_test.go b/render_test.go index 1a212f8..272a4c7 100644 --- a/render_test.go +++ b/render_test.go @@ -11,7 +11,7 @@ func TestRender(t *testing.T) { test.com/A@v1.0.0 test.com/B@v1.2.3 test.com/B@v1.0.0 test.com/C@v4.5.6 `)) - if err := Render(in, out); err != nil { + if err := Render(in, out, RenderOptions{HighlightPackages: map[string]bool{}}); err != nil { t.Fatal(err) } From 5b8b258b64c6073053398808136cdddc9174f6f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Czhouchaofan=E2=80=9D?= <“zhoucf@codoon.com”> Date: Fri, 17 Jun 2022 17:28:55 +0800 Subject: [PATCH 2/3] support highlight --- modgv/main.go | 14 +++++++------- render.go | 14 +++++++------- render_test.go | 2 +- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/modgv/main.go b/modgv/main.go index 278791b..1ad6cc7 100644 --- a/modgv/main.go +++ b/modgv/main.go @@ -48,7 +48,7 @@ var printHelp bool func init() { flag.BoolVar(&printHelp, "h", false, "print helps.") - flag.StringVar(&highlights, "H", "", "The packages need high light color, split with comma.") + flag.StringVar(&highlights, "H", "", "The modules need highlight, split with comma.") } func main() { @@ -59,13 +59,13 @@ func main() { usage() } - highlightPackages := strings.Split(highlights, ",") - highlightPackagesMap := make(map[string]bool, len(highlightPackages)) - for _, pkg := range highlightPackages { - highlightPackagesMap[pkg] = true + 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{HighlightPackages: highlightPackagesMap}); err != nil { + if err := modgv.Render(os.Stdin, os.Stdout, modgv.RenderOptions{HighlightModules: highlightModulesMap}); err != nil { exitOnErr(err) } } @@ -81,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 [-H some modules need highlight, split with comma.] | %s | 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 ") diff --git a/render.go b/render.go index bbfc55a..b1e3180 100644 --- a/render.go +++ b/render.go @@ -14,7 +14,7 @@ import ( ) type RenderOptions struct { - HighlightPackages map[string]bool + HighlightModules map[string]bool } // Render translates “go mod graph” output taken from @@ -39,14 +39,14 @@ func Render(in io.Reader, out io.Writer, options RenderOptions) error { coloring(graph, options) for _, n := range graph.mvsPicked { - if options.HighlightPackages[n] { + 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 { - if options.HighlightPackages[n] { + 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")) @@ -59,17 +59,17 @@ func Render(in io.Reader, out io.Writer, options RenderOptions) error { return nil } -// coloring highlight all packages that reference package in options.HighlightPackages +// coloring highlight all packages that reference package in options.HighlightModules func coloring(gr *graph, options RenderOptions) { - if len(options.HighlightPackages) == 0 { + if len(options.HighlightModules) == 0 { return } finished := false for !finished { coloringCnt := 0 for _, e := range gr.edges { - if options.HighlightPackages[e.to] && !options.HighlightPackages[e.from] { - options.HighlightPackages[e.from] = true + if options.HighlightModules[e.to] && !options.HighlightModules[e.from] { + options.HighlightModules[e.from] = true coloringCnt++ } } diff --git a/render_test.go b/render_test.go index 272a4c7..2a2fc0a 100644 --- a/render_test.go +++ b/render_test.go @@ -11,7 +11,7 @@ func TestRender(t *testing.T) { test.com/A@v1.0.0 test.com/B@v1.2.3 test.com/B@v1.0.0 test.com/C@v4.5.6 `)) - if err := Render(in, out, RenderOptions{HighlightPackages: map[string]bool{}}); err != nil { + if err := Render(in, out, RenderOptions{HighlightModules: map[string]bool{}}); err != nil { t.Fatal(err) } From e99aa2c8c3f452a72f727e163281f20215ddfae4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Czhouchaofan=E2=80=9D?= <“zhoucf@codoon.com”> Date: Fri, 17 Jun 2022 17:42:18 +0800 Subject: [PATCH 3/3] highlight help --- modgv/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modgv/main.go b/modgv/main.go index 1ad6cc7..2a8dcfd 100644 --- a/modgv/main.go +++ b/modgv/main.go @@ -81,7 +81,7 @@ func usage() { fmt.Fprintf(os.Stderr, "\n") fmt.Fprintf(os.Stderr, "USAGE:\n\n") - fmt.Fprintf(os.Stderr, " go mod graph [-H some modules need highlight, split with comma.] | %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 ")