Skip to content

Commit

Permalink
chore: add more styling options to pertify (#9)
Browse files Browse the repository at this point in the history
chore: add more styling options to pertify
  • Loading branch information
moul authored Jul 8, 2019
2 parents 5438ce9 + 1fd7940 commit afc96d4
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 11 deletions.
12 changes: 12 additions & 0 deletions attr.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,15 @@ func (a Attrs) GetColor() string {
}
return ""
}

func (a Attrs) GetComment() string {
if attr, found := a["comment"]; found {
return attr.(string)
}
return ""
}

func (a Attrs) SetComment(comment string) Attrs {
a["comment"] = comment
return a
}
1 change: 0 additions & 1 deletion cmd/pertify/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require moul.io/graphman v0.0.0

require (
github.com/pkg/errors v0.8.1
github.com/urfave/cli v1.20.0 // indirect
gopkg.in/urfave/cli.v2 v2.0.0-20180128182452-d3ae77c26ac8
gopkg.in/yaml.v3 v3.0.0-20190705120443-117fdf03f45f
moul.io/graphman/viz v0.0.0
Expand Down
7 changes: 1 addition & 6 deletions cmd/pertify/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ github.com/awalterschulze/gographviz v0.0.0-20190522210029-fa59802746ab h1:+cdNq
github.com/awalterschulze/gographviz v0.0.0-20190522210029-fa59802746ab/go.mod h1:GEV5wmg4YquNw7v1kkyoX9etIk8yVmXj+AkDHuuETHs=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/urfave/cli.v2 v2.0.0-20180128182452-d3ae77c26ac8 h1:Ggy3mWN4l3PUFPfSG0YB3n5fVYggzysUmiUQ89SnX6Y=
gopkg.in/urfave/cli.v2 v2.0.0-20180128182452-d3ae77c26ac8/go.mod h1:cKXr3E0k4aosgycml1b5z33BVV6hai1Kh7uDgFOkbcs=
gopkg.in/yaml.v3 v3.0.0-20190705120443-117fdf03f45f h1:nqBZgl3FUZD7Xe4yitNx/0mqkydzbl4Y89WSTjG6/ok=
gopkg.in/yaml.v3 v3.0.0-20190705120443-117fdf03f45f/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
moul.io/graphman v0.0.0-20190630224620-6ea064d17b62 h1:XdYdzzhh7M83I+cV2Vz9avWr4/HW03JjEHpSoH5DQXs=
moul.io/graphman v0.0.0-20190630224620-6ea064d17b62/go.mod h1:R9SYks3P7x9mSW1ESiP2n5hvgN2KXo4hb2bi0wJH2cM=
moul.io/graphman/viz v0.0.0-20190630224620-6ea064d17b62 h1:SuSJKN8JP4nB6fWfEf1kpiHzMz/p3Wcf6PGunNSN6qY=
moul.io/graphman/viz v0.0.0-20190630224620-6ea064d17b62/go.mod h1:kdgZu9fto6GBvFTFaRcOeEDEUV+B/kvN62Gm75rjYw8=
14 changes: 12 additions & 2 deletions cmd/pertify/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func main() {
Flags: []cli.Flag{
&cli.StringFlag{Name: "file", Aliases: []string{"f"}, Value: "-", Usage: `path to the graph file ("-" for stdin)`},
&cli.BoolFlag{Name: "dot", Usage: "print 'dot' compatible output"},
&cli.BoolFlag{Name: "vertical", Usage: "displaying steps from top to bottom"},
&cli.BoolFlag{Name: "debug", Aliases: []string{"D"}, Usage: "verbose mode"},
},
Action: graph,
}
Expand All @@ -39,18 +41,26 @@ func graph(c *cli.Context) error {
}

graph := graphman.FromPertConfig(config)
log.Println(graph)
if c.Bool("debug") {
log.Println(graph)
}

// compute and highlight the shortest path
shortestPath, distance := graph.FindShortestPath("Start", "Finish")
log.Println("Shortest path:", shortestPath, "distance:", distance)
if c.Bool("debug") {
log.Println("Shortest path:", shortestPath, "distance:", distance)
}
for _, edge := range shortestPath {
edge.Dst().SetColor("red")
edge.SetColor("red")
}
graph.GetVertex("Start").SetColor("blue")
graph.GetVertex("Finish").SetColor("blue")

if c.Bool("vertical") {
graph.Attrs["rankdir"] = "TB"
}

s, err := viz.ToGraphviz(graph)
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func New(attrs ...Attrs) *Graph {
var a Attrs
if len(attrs) > 0 {
a = attrs[0]
} else {
a = make(map[string]interface{})
}
return &Graph{
vertices: make(Vertices, 0),
Expand Down
14 changes: 12 additions & 2 deletions viz/graphviz.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,20 @@ func attrsGeneric(a graphman.Attrs, attrs map[string]string) {
attrs[string(graphviz.Label)] = title
ac.Del("title")
}
if comment := a.GetComment(); comment != "" {
attrs[string(graphviz.Comment)] = comment
ac.Del("comment")
}
if len(ac) > 0 {
attrs[string(graphviz.Comment)] = ""
for k, v := range ac {
line := fmt.Sprintf("\n%s: %v", k, v)
attrs[string(graphviz.Comment)] += line
switch k {
case "rankdir", "shape", "style":
attrs[k] = v.(string)
default:
line := fmt.Sprintf("\n%s: %v", k, v)
attrs[string(graphviz.Comment)] += line
}
}
}

Expand All @@ -105,6 +114,7 @@ func attrsGeneric(a graphman.Attrs, attrs map[string]string) {
delete(attrs, string(key))
}
}

}

func escape(input string) string {
Expand Down

0 comments on commit afc96d4

Please sign in to comment.