prettyprinters

package
v0.0.0-...-02c76fb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2020 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package prettyprinters provides a general interface and concrete implementations for implementing prettyprinters. This package was originally created to facilitate the development of graphviz visualizations for resource graphs, however it is intended to be useful for creating arbitrary output generators so that resource graph data can be used in other applications.

Example (CreateACustomPrintProvider)
package main

import (
	"fmt"

	"github.com/asteris-llc/converge/graph"
	"github.com/asteris-llc/converge/graph/node"
	"github.com/asteris-llc/converge/prettyprinters"
	"github.com/asteris-llc/converge/prettyprinters/graphviz"
	"golang.org/x/net/context"
)

func main() {
	g := graph.New()
	g.Add(node.New(graph.ID("a"), 1))
	g.Add(node.New(graph.ID("a", "b"), 2))
	g.Add(node.New(graph.ID("a", "c"), 3))
	g.Add(node.New(graph.ID("a", "c", "d"), 4))
	g.Add(node.New(graph.ID("a", "c", "e"), 5))
	g.Add(node.New(graph.ID("a", "b", "f"), 6))
	g.Add(node.New(graph.ID("a", "b", "g"), 7))

	g.Add(node.New(graph.ID("a", "c", "d", "h"), 8))
	g.Add(node.New(graph.ID("a", "c", "d", "i"), 9))
	g.Add(node.New(graph.ID("a", "c", "d", "j"), 10))

	g.Add(node.New(graph.ID("a", "c", "e", "k"), 11))
	g.Add(node.New(graph.ID("a", "c", "e", "l"), 12))
	g.Add(node.New(graph.ID("a", "c", "e", "m"), 13))

	g.Add(node.New(graph.ID("a", "b", "f", "n"), 14))
	g.Add(node.New(graph.ID("a", "b", "f", "o"), 15))
	g.Add(node.New(graph.ID("a", "b", "f", "p"), 16))

	g.Add(node.New(graph.ID("a", "b", "g", "q"), 17))
	g.Add(node.New(graph.ID("a", "b", "g", "r"), 18))
	g.Add(node.New(graph.ID("a", "b", "g", "s"), 19))

	g.Connect(graph.ID("a"), graph.ID("a", "b"))
	g.Connect(graph.ID("a"), graph.ID("a", "c"))
	g.Connect(graph.ID("a", "c"), graph.ID("a", "c", "d"))
	g.Connect(graph.ID("a", "c"), graph.ID("a", "c", "e"))
	g.Connect(graph.ID("a", "b"), graph.ID("a", "b", "f"))
	g.Connect(graph.ID("a", "b"), graph.ID("a", "b", "g"))

	g.Connect(graph.ID("a", "c", "d"), graph.ID("a", "c", "d", "h"))
	g.Connect(graph.ID("a", "c", "d"), graph.ID("a", "c", "d", "i"))
	g.Connect(graph.ID("a", "c", "d"), graph.ID("a", "c", "d", "j"))

	g.Connect(graph.ID("a", "c", "e"), graph.ID("a", "c", "e", "k"))
	g.Connect(graph.ID("a", "c", "e"), graph.ID("a", "c", "e", "l"))
	g.Connect(graph.ID("a", "c", "e"), graph.ID("a", "c", "e", "m"))

	g.Connect(graph.ID("a", "b", "f"), graph.ID("a", "b", "f", "n"))
	g.Connect(graph.ID("a", "b", "f"), graph.ID("a", "b", "f", "o"))
	g.Connect(graph.ID("a", "b", "f"), graph.ID("a", "b", "f", "p"))

	g.Connect(graph.ID("a", "b", "g"), graph.ID("a", "b", "g", "q"))
	g.Connect(graph.ID("a", "b", "g"), graph.ID("a", "b", "g", "r"))
	g.Connect(graph.ID("a", "b", "g"), graph.ID("a", "b", "g", "s"))

	numberPrinter := prettyprinters.New(graphviz.New(graphviz.DefaultOptions(), NumberProvider{}))
	dotCode, _ := numberPrinter.Show(context.Background(), g)
	fmt.Println(dotCode)

	
Output:

Example (GenerateAGraphFromAFileOnDisk)
package main

import (
	"fmt"
	"os"

	"github.com/asteris-llc/converge/load"
	"github.com/asteris-llc/converge/prettyprinters"
	"github.com/asteris-llc/converge/prettyprinters/graphviz"
	"github.com/asteris-llc/converge/prettyprinters/graphviz/providers"
	"golang.org/x/net/context"
)

func main() {
	g, err := load.Load(context.Background(), os.Args[1], false)
	if err != nil {
		fmt.Println(err)
		return
	}
	printer := prettyprinters.New(graphviz.New(graphviz.DefaultOptions(), providers.NewRPCProvider()))
	dotCode, err := printer.Show(context.Background(), g)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(dotCode)
}
Output:

Example (UsingADefaultProvider)
package main

import (
	"fmt"

	"github.com/asteris-llc/converge/graph"
	"github.com/asteris-llc/converge/graph/node"
	"github.com/asteris-llc/converge/prettyprinters"
	"github.com/asteris-llc/converge/prettyprinters/graphviz"
	"golang.org/x/net/context"
)

func main() {
	g := createTestGraph()
	valuePrinter := prettyprinters.New(graphviz.New(graphviz.DefaultOptions(), graphviz.DefaultProvider()))
	valueDotCode, _ := valuePrinter.Show(context.Background(), g)
	fmt.Println(valueDotCode)

	
Output:

Example (UsingTheIDProvider)
package main

import (
	"fmt"

	"github.com/asteris-llc/converge/graph"
	"github.com/asteris-llc/converge/graph/node"
	"github.com/asteris-llc/converge/prettyprinters"
	"github.com/asteris-llc/converge/prettyprinters/graphviz"
	"golang.org/x/net/context"
)

func main() {
	g := createTestGraph()

	namePrinter := prettyprinters.New(graphviz.New(graphviz.DefaultOptions(), graphviz.IDProvider()))
	nameDotCode, _ := namePrinter.Show(context.Background(), g)
	fmt.Println(nameDotCode)

	
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var SubgraphBottomID interface{}

SubgraphBottomID defines the SubgraphID for the bottom subgraph. "⊥" is a reserved SubgraphID and shouldn't be returned as the SubgraphID by any calls to MakeNode.

Functions

This section is empty.

Types

type BasePrinter

type BasePrinter interface{}

BasePrinter is an implementation of some output format that can be used to prettyprint a Directed Graph. Printer is the zero value for these kinds of printers. To get any functionality, printers should implement one or more of the following interfaces.

type EdgePrinter

type EdgePrinter interface {
	// DrawEdge will be called once for each edge in the graph.  It is called with
	// a graph, the ID of the source vertex, and the ID of the target vertex.  It
	// should return a string representing the edge in the final output, or an
	// error that will be returned to the user.
	DrawEdge(graph *graph.Graph, srcNodeID string, dstNodeID string) (Renderable, error)
}

EdgePrinter should be implemented by printers that want to render Edges

type EdgeSectionPrinter

type EdgeSectionPrinter interface {
	// StartEdgeSection will be given a graph and should return a string used to
	// start the edge section, or an error that will be returned to the user.
	StartEdgeSection(graph *graph.Graph) (Renderable, error)

	// FinishEdgeSection will be given a graph and should return a string used to
	// finish the edge section in the final output, or an error that will be
	// returned to the user.
	FinishEdgeSection(graph *graph.Graph) (Renderable, error)
}

EdgeSectionPrinter should be implemented by printers that want to render edge sections (TODO: what are these?)

type GraphPrinter

type GraphPrinter interface {
	// StartPP should take a graph and shall return a string used to start the
	// graph output, or an error which will be returned to the user.
	StartPP(graph *graph.Graph) (Renderable, error)

	// FinishPP will be given a graph and shall return a string used to finalize
	// graph output, or an error which will be returned to the user.
	FinishPP(graph *graph.Graph) (Renderable, error)
}

GraphPrinter should be implemented by printers that need to add a preamble or addendum to the pretty printed output

type NodePrinter

type NodePrinter interface {
	// DrawNode will be called once for each node in the graph.  The function will
	// be given a graph and a string ID for the current node in the graph, and
	// should return a string representing the node in the final output, or an
	// error that will be returned to the user.
	DrawNode(graph *graph.Graph, nodeID string) (Renderable, error)
}

NodePrinter should be implemented by printers that want to render nodes

type Printer

type Printer struct {
	// contains filtered or unexported fields
}

Printer is the top-level structure for a pretty printer.

func New

func New(p BasePrinter) Printer

New creates a new prettyprinter instance from the specified DigraphPrettyPrinter instance.

func (Printer) Show

func (p Printer) Show(ctx context.Context, g *graph.Graph) (string, error)

Show will take the given graph and return a string representing the text output of the graph according to the associated prettyprinter. If an error is returned at any stage of the prettyprinting process it is returned unmodified to the user.

type Renderable

type Renderable interface {
	// The Renderable interface should provide an instance of String() to render
	// the object.
	fmt.Stringer
}

Renderable provides an interface for printable objects

type Subgraph

type Subgraph struct {
	StartNode *string
	EndNodes  []string
	ID        SubgraphID
	Nodes     []string
}

Subgraph is a type that represents a subgraph containing it's ID, it's root node, a list of it's terminal nodes, and any other included nodes.

type SubgraphID

type SubgraphID interface{}

A SubgraphID is an opaque type that is handled by the DigraphPrettyPrinter. SubgraphID must adhere to the following conditions:

  • Unique per graph
  • Comparable

type SubgraphMap

type SubgraphMap map[SubgraphID]Subgraph

SubgraphMap is a simple type alias for keeping track of SubgraphID -> Subgraph mappings

type SubgraphMarker

type SubgraphMarker struct {
	SubgraphID SubgraphID // The ID for this subgraph (if Start is true)
	Start      bool       // True if this is the start of a new subgraph
}

A SubgraphMarker is a structure used to identify graph nodes that mark the beginning or end of a Subgraph. SubgraphMakers are returned by MarkNode(). The SubgraphID field should be set to the name of a unique SubgraphID if start is true. When Start is false the SubgraphID field is ignored.

type SubgraphPrinter

type SubgraphPrinter interface {
	// MarkNode() is a function used to identify the boundaries of subgraphs
	// within the larger graph.  MarkNode() is called with a graph and the id of a
	// node within the graph, and should return a *SubgraphMarker with the
	// SubgraphID and Start fields set to indicate the beginning or end of a
	// subgraph.  If nil is returned the node will be included in the subgraph of
	// it's parent (or ⊥ if it is not part of any subgraph).
	// The string "⊥" is reserved for the bottom subgraph element and shouldn't be
	// used as a NodeID.
	MarkNode(graph *graph.Graph, nodeID string) *SubgraphMarker

	// StartSubgraph will be given a graph, the ID of the root node of the
	// subgraph, and the SubgraphID returned as part of MarkNode().  It should
	// return the string used to start the subgraph or an error that will be
	// returned to the user.
	StartSubgraph(graph *graph.Graph, nodeID string, subgraphID SubgraphID) (Renderable, error)

	// FinishSubgraph will be given a graph and a SubgraphID returned by MarkNode
	// and should return a string used to end a subgraph, or an error that will be
	// returned to the user.
	FinishSubgraph(graph *graph.Graph, subgraphID SubgraphID) (Renderable, error)
}

SubgraphPrinter should be implemented by printers that need to control subgraph rendering (that is, grouping of nodes of any kind)

type VisibleRenderable

type VisibleRenderable interface {
	Renderable

	// Visible returns true if the object should be rendered, and false
	// otherwise.  If a consumer chooses to ignore this value, the instance should
	// still provide a valid string value.
	Visible() bool
}

VisibleRenderable allows checking if a string is visible

func HiddenString

func HiddenString() VisibleRenderable

HiddenString creates a non-renderable string

func RenderableString

func RenderableString(s string, visible bool) VisibleRenderable

RenderableString creates a RenderableString with visibility on or off depending on the value of a boolean parameter.

func SprintfRenderable

func SprintfRenderable(visible bool, fmtStr string, args ...interface{}) VisibleRenderable

SprintfRenderable creates a new visible Renderable from a Sprintf call

func VisibleString

func VisibleString(s string) VisibleRenderable

VisibleString creates a new Renderable that is visible

Directories

Path Synopsis
Package graphviz provides a concrete prettyprinters.DigraphPrettyPrinter implementation for rendering directed graphs as Graphviz-compatible dot source files.
Package graphviz provides a concrete prettyprinters.DigraphPrettyPrinter implementation for rendering directed graphs as Graphviz-compatible dot source files.
providers
Package providers contains an implementation of specific Graphviz PrintProviders that can be used for generating graphs.
Package providers contains an implementation of specific Graphviz PrintProviders that can be used for generating graphs.
Package jsonl implements a pretty printer for JSONL format (jsonlines.org)
Package jsonl implements a pretty printer for JSONL format (jsonlines.org)
Package tmpltools provides extensions to 'text/template' that are useful for prettyprinter formatting.
Package tmpltools provides extensions to 'text/template' that are useful for prettyprinter formatting.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL