diff

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package diff computes fast pure-Go line diffs (Myers SES) with rune-safe intraline change spans for real-time TUI and SSE visualization without spawning external git processes.

Index

Constants

View Source
const (
	DefaultContextLines = 3
	MaxDiffHunkLines    = 400
	MaxMyersWindowLines = 4000
	MaxMyersEditDist    = 1000
	MaxDiffLineBytes    = 4096
	MaxIntralineBytes   = 2048
	MaxDiffInputBytes   = 2 * 1024 * 1024
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DiffLine

type DiffLine struct {
	Kind    LineKind `json:"k"`
	OldLine int      `json:"o,omitempty"`
	NewLine int      `json:"n,omitempty"`
	Text    string   `json:"t"`
	Spans   []Span   `json:"sp,omitempty"`
}

DiffLine represents a single line in a diff hunk.

type Hunk

type Hunk struct {
	OldStart int        `json:"os"`
	OldCount int        `json:"oc"`
	NewStart int        `json:"ns"`
	NewCount int        `json:"nc"`
	Lines    []DiffLine `json:"lines"`
}

Hunk is one contiguous diff block with surrounding context lines.

type LineKind

type LineKind string

LineKind classifies one line inside a unified diff hunk.

const (
	LineContext LineKind = "ctx"
	LineAdd     LineKind = "add"
	LineDel     LineKind = "del"
)

type Result

type Result struct {
	Added     int    `json:"added"`
	Removed   int    `json:"removed"`
	Hunks     []Hunk `json:"hunks,omitempty"`
	Truncated bool   `json:"truncated,omitempty"`
	IsBinary  bool   `json:"isBinary,omitempty"`
}

Result holds line totals and structured hunks for a file change.

func Compute

func Compute(before, after []byte, existedBefore, existsAfter bool, contextLines int) Result

Compute generates a structured diff between before and after file contents.

type Span

type Span struct {
	Start   int  `json:"s"`
	End     int  `json:"e"`
	Changed bool `json:"c,omitempty"`
}

Span marks a byte range [Start, End) inside a DiffLine's Text for intraline highlighting.

Jump to

Keyboard shortcuts

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