formatters

package
v0.29.3 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultDirection = DirectionLR

DefaultDirection is the default layout direction for graphs.

Variables

This section is empty.

Functions

func BuildNodeNames

func BuildNodeNames(paths []string) map[string]string

BuildNodeNames returns stable, distinct display names for file paths. Paths that share the same base name are disambiguated by increasing path suffix depth.

func EdgeLabel added in v0.16.6

func EdgeLabel(from, to string) string

EdgeLabel returns a deterministic 3-character alphanumeric label for the directed edge from → to. The same pair always produces the same label.

func Render added in v0.27.0

func Render(scene Scene, r Renderer) (string, error)

Render walks a Scene through a Renderer. As primitives migrate into the Scene, this becomes the single rendering path shared by every formatter.

func SupportedDirections added in v0.16.2

func SupportedDirections() string

SupportedDirections returns a list of all supported direction names.

func SupportedFormats

func SupportedFormats() string

SupportedFormats returns a list of all supported output format names.

Types

type Formatter

type Formatter interface {
	// Format converts a dependency graph to a formatted string representation.
	Format(g depgraph.FileDependencyGraph, opts RenderOptions) (string, error)
	// GenerateURL creates a shareable URL for the formatted output.
	// Returns the URL and true if supported, or ("", false) if not.
	GenerateURL(output string) (string, bool)
}

Formatter is the interface that all graph formatters must implement.

func NewFormatter

func NewFormatter(format string) (Formatter, error)

NewFormatter creates a Formatter for the provided output format string.

type GraphDirection added in v0.16.2

type GraphDirection string

GraphDirection represents the direction of the graph layout.

const (
	DirectionLR GraphDirection = "LR"
	DirectionRL GraphDirection = "RL"
	DirectionTB GraphDirection = "TB"
	DirectionBT GraphDirection = "BT"
)

func ParseDirection added in v0.16.2

func ParseDirection(s string) (GraphDirection, bool)

ParseDirection converts a string to GraphDirection.

func (GraphDirection) String added in v0.16.2

func (d GraphDirection) String() string

String returns the canonical string representation.

func (GraphDirection) StringLower added in v0.16.2

func (d GraphDirection) StringLower() string

StringLower returns the canonical string representation in lowercase.

type GraphHeader added in v0.27.0

type GraphHeader struct {
	// Orientation is the resolved layout direction (already defaulted).
	Orientation GraphDirection
	// Title is an optional graph label/title; empty when unset.
	Title string
	// TrailingNewline records whether the layout direction was set explicitly,
	// which both formatters use to decide a trailing newline.
	TrailingNewline bool
}

GraphHeader carries the graph-level render attributes.

type NodeKind added in v0.27.0

type NodeKind int

NodeKind is what a graph node fundamentally is. The kinds are mutually exclusive; cycle membership, prune state, and lifecycle apply independently and are modeled separately.

const (
	// NodeKindFile is a regular (non-test) source file.
	NodeKindFile NodeKind = iota
	// NodeKindTest is a test file.
	NodeKindTest
	// NodeKindModule is a collapsed module node.
	NodeKindModule
)

type OutputFormat

type OutputFormat int

OutputFormat represents an output format type

const (
	OutputFormatDOT OutputFormat = iota
	OutputFormatMermaid
)

func ParseOutputFormat

func ParseOutputFormat(s string) (OutputFormat, bool)

ParseOutputFormat converts a string to OutputFormat

func (OutputFormat) String

func (f OutputFormat) String() string

String returns the string representation of the format

type RenderOptions

type RenderOptions struct {
	// Label is an optional title or label for the graph output.
	Label string
	// Direction is the layout direction for the graph.
	Direction GraphDirection
	// BasePath is an optional filesystem base used to derive stable relative node IDs.
	BasePath string
	// EdgeLabels enables deterministic short labels on edges.
	EdgeLabels bool
}

RenderOptions contains output-specific rendering options.

type Renderer added in v0.27.0

type Renderer interface {
	// Begin emits the graph header/preamble from the resolved header primitive.
	Begin(GraphHeader)
	// OpenCluster begins a labeled boundary; the member node declarations that
	// follow are drawn inside it until CloseCluster.
	OpenCluster(SceneCluster)
	// CloseCluster ends the boundary opened by OpenCluster.
	CloseCluster()
	// Finish emits any trailer and returns the complete output.
	Finish() (string, error)
}

Renderer translates a Scene's primitives into a concrete output syntax (DOT, Mermaid, ...). It is the contract that keeps the formatters in lockstep: every render primitive is a method here, so adding or removing one is a compile error in any formatter that has not kept up — enforced by the static assertions below. A Renderer must contain only syntax, never graph logic.

The contract grows one primitive at a time as logic migrates out of the formatters; today it covers the graph header and trailer.

type Scene added in v0.27.0

type Scene struct {
	Header GraphHeader
	// FilePaths are the graph's node keys, sorted for deterministic output.
	FilePaths []string
	// NodeNames maps each node key to its display name.
	NodeNames map[string]string
	// CycleNodes is the set of nodes that participate in a dependency cycle.
	CycleNodes map[string]bool
	// Nodes holds the resolved presentation for each graph node, keyed by node
	// key. Built once here so both formatters render identical content.
	Nodes map[string]SceneNode
	// Edges are the dependency edges in deterministic render order.
	Edges []SceneEdge
	// Cluster, when set, is the labeled module boundary to draw around its
	// member nodes; nil when no boundary is rendered.
	Cluster *SceneCluster
	// MajorityType is the most common file type; nodes of this type render
	// neutral rather than type-colored.
	MajorityType string
	// HasMultipleTypes is true when more than one file type is present.
	HasMultipleTypes bool
}

Scene is the renderer-agnostic plan for one graph render. It is built once by BuildScene from the dependency graph and render options, and holds the resolved primitives that every formatter renders.

Keeping the derivation here — rather than in each formatter — is what stops the DOT and Mermaid outputs from drifting apart: a formatter translates Scene primitives into its own syntax and contains no graph logic of its own. New primitives are added to the Scene and to the Renderer contract together, so a capability cannot exist in one formatter but not the other.

func BuildScene added in v0.27.0

func BuildScene(g depgraph.FileDependencyGraph, opts RenderOptions) (Scene, error)

BuildScene resolves a dependency graph and render options into a renderer-agnostic Scene. Formatter-specific syntax is applied later by a Renderer; this function owns all of the shared derivation.

type SceneCluster added in v0.27.0

type SceneCluster struct {
	Name    string
	Members []string
}

SceneCluster is a labeled boundary drawn around a set of member node keys. It is populated only for the single-module boundary view.

type SceneEdge added in v0.27.0

type SceneEdge struct {
	From string
	To   string
	// State is the edge's lifecycle: present, deleted, or renamed.
	State depgraph.EdgeState
	// InCycle marks an edge that participates in a dependency cycle.
	InCycle bool
	// Labels are the underlying dependency labels, used when edge labels are on
	// (a collapsed module edge keeps one labeled arrow per original dependency).
	Labels []string
}

SceneEdge is the domain view of a dependency edge for the formatters: a directed dependency with its lifecycle and cycle membership.

type SceneNode added in v0.27.0

type SceneNode struct {
	Key        string
	Name       string
	LabelLines []string
	// Type is the file type: its extension, or its base name when extensionless.
	Type string
	// Kind is what the node fundamentally is: a regular file, a test file, or a
	// collapsed module. These are mutually exclusive.
	Kind NodeKind
	// State is the node's lifecycle: present, deleted, or renamed.
	State depgraph.FileState
	// IsPruned marks a node whose subtree was elided — orthogonal to Kind.
	IsPruned bool
	// InCycle marks a node in a dependency cycle — orthogonal to Kind.
	InCycle bool
	// Phantom describes the node's test-sibling, or nil when it has none.
	Phantom *depgraph.PhantomMetadata
}

SceneNode is the domain view of a graph node for the formatters: a file with its type, lifecycle, role, and cycle membership. Formatters render from these fields and do not reach back into the dependency graph.

LabelLines is the node's label as ordered rows with no separator, so each formatter joins them with its own line break (DOT "\n", Mermaid "<br/>") and applies its own escaping — keeping the two label outputs in step.

Jump to

Keyboard shortcuts

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