diffview

package
v0.125.0 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package diffview renders unified or side-by-side file diffs to a string of ANSI-styled output, using go-udiff for the underlying diff algorithm and chroma for in-line syntax highlighting.

The structure (file/style/split/builder) mirrors charmbracelet/crush's internal/ui/diffview package, adapted to lipgloss v1 and the existing infer CLI styling abstractions.

Typical use:

out := diffview.New().
    Before(path, oldContent).
    After(path, newContent).
    FileName(path).
    Width(termWidth).
    Style(diffview.DefaultDarkStyle()).
    String()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Highlight

func Highlight(path, content string, style *chroma.Style, lineNumbers bool) string

Highlight renders a whole file's contents as ANSI-styled, optionally line-numbered text for a preview pane (NOT a diff). It reuses the same chroma formatter as the in-line diff highlighter, so colors stay consistent. A nil style disables colorization; any tokenise/format error falls back to the plain (uncolored) content, so it never returns an error string.

Types

type DiffView

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

DiffView is the fluent builder + renderer.

func New

func New() *DiffView

New returns a DiffView with sensible defaults (unified layout, 3 context lines, 4-space tab expansion, line numbers on, dark style).

func (*DiffView) After

func (dv *DiffView) After(path, content string) *DiffView

After sets the "after" file.

func (*DiffView) Before

func (dv *DiffView) Before(path, content string) *DiffView

Before sets the "before" file (path is used for chroma lexer selection).

func (*DiffView) ContextLines

func (dv *DiffView) ContextLines(n int) *DiffView

ContextLines sets the number of unchanged context lines around each hunk.

func (*DiffView) FileName

func (dv *DiffView) FileName(name string) *DiffView

FileName sets the file header rendered above the first hunk. Empty disables it.

func (*DiffView) Layout

func (dv *DiffView) Layout(l Layout) *DiffView

Layout overrides the layout selection. Defaults to Auto.

func (*DiffView) String

func (dv *DiffView) String() string

String renders the diff.

func (*DiffView) Style

func (dv *DiffView) Style(s Style) *DiffView

Style overrides the visual style. Defaults to DefaultDarkStyle.

func (*DiffView) Width

func (dv *DiffView) Width(w int) *DiffView

Width sets the total render width. Pass 0 (or omit) to render at natural width.

type Layout

type Layout int

Layout controls whether the diff is rendered as a single column (Unified) or side-by-side (Split). Auto picks Split when the available width is >= SplitThreshold, else Unified.

const (
	LayoutAuto Layout = iota
	LayoutUnified
	LayoutSplit
)

type LineStyle

type LineStyle struct {
	LineNumber lipgloss.Style
	Symbol     lipgloss.Style
	Code       lipgloss.Style
}

LineStyle holds the lipgloss styles for a single line type's three regions: the gutter line-number cell, the +/- symbol cell, and the code body.

type Style

type Style struct {
	DividerLine LineStyle // hunk header @@ ... @@
	MissingLine LineStyle // empty paired side in split layout
	EqualLine   LineStyle // unchanged / context
	InsertLine  LineStyle // additions
	DeleteLine  LineStyle // deletions
	Filename    LineStyle // file header above first hunk
}

Style aggregates LineStyles for every diff line kind.

func DefaultDarkStyle

func DefaultDarkStyle() Style

DefaultDarkStyle returns a diff Style tuned for dark terminal themes.

func DefaultLightStyle

func DefaultLightStyle() Style

DefaultLightStyle returns a diff Style tuned for light terminal themes.

func NewThemeAwareStyle

func NewThemeAwareStyle(p ThemePalette) Style

NewThemeAwareStyle builds a diff Style whose semantic foreground colours come from the active theme, while the carefully-tuned background tints are inherited from the dark or light base style. This lets every theme show its own add/remove/accent colours without re-tuning the row backgrounds (which are what keep additions and deletions legible and colour-blind distinct).

type ThemePalette

type ThemePalette struct {
	Add    string // additions foreground (+ symbol and line number)
	Remove string // deletions foreground (- symbol and line number)
	Accent string // hunk header (@@ ... @@) foreground
	Dim    string // gutter / context line numbers
	Dark   bool   // pick the dark vs light tuned background base
}

ThemePalette carries the semantic colours a diff Style derives from the active application theme. Callers build it from their theme so the diffview package stays decoupled from the app's theme types. Empty colour fields keep the tuned base value.

Jump to

Keyboard shortcuts

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