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 ¶
- func Highlight(path, content string, style *chroma.Style, lineNumbers bool) string
- type DiffView
- func (dv *DiffView) After(path, content string) *DiffView
- func (dv *DiffView) Before(path, content string) *DiffView
- func (dv *DiffView) ContextLines(n int) *DiffView
- func (dv *DiffView) FileName(name string) *DiffView
- func (dv *DiffView) Layout(l Layout) *DiffView
- func (dv *DiffView) String() string
- func (dv *DiffView) Style(s Style) *DiffView
- func (dv *DiffView) Width(w int) *DiffView
- type Layout
- type LineStyle
- type Style
- type ThemePalette
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Highlight ¶
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) ContextLines ¶
ContextLines sets the number of unchanged context lines around each hunk.
func (*DiffView) FileName ¶
FileName sets the file header rendered above the first hunk. Empty disables it.
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.
type LineStyle ¶
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.