Skip to content

DiffViewer

KeyAction
/ kMove up one line
/ jMove down one line
Home / gJump to first line
End / GJump to last line
PgUpPage up
PgDnPage down
Ctrl+UHalf-page up
Ctrl+DHalf-page down
nJump to next change hunk
NJump to previous change hunk
uSwitch to unified view
sSwitch to side-by-side view
lToggle line numbers
wToggle word-diff highlighting
EnterSelect line (fires onLineSelect)

Call SetDiff(unified string) with a standard unified diff string (e.g., the output of git diff or diff -u). The viewer parses it into hunks internally.

Default is unified view. s switches to side-by-side, which splits the viewport into before/after columns. u returns to unified. Both views support the same navigation bindings.

DiffViewer preview

DiffViewer displays diff content with syntax highlighting

Constructor func NewDiffViewer() *DiffViewer

Usage

dv := components.NewDiffViewer().
SetTitle("main.go").
SetDiff("line one\nline two\n", "line one\nline two changed\n").
SetSideBySide(false).
SetShowLineNumbers(true).
SetWordDiff(true)
fmt.Println(dv != nil)

Methods

Method Signature Description
ClearSelection func (d *DiffViewer) ClearSelection() ClearSelection clears all line selections
GetCurrentHunkIndex func (d *DiffViewer) GetCurrentHunkIndex() int GetCurrentHunkIndex returns the hunk index of the current line
GetHunkLines func (d *DiffViewer) GetHunkLines(hunkIndex int) []DiffLine GetHunkLines returns all lines in the specified hunk
GetHunks func (d *DiffViewer) GetHunks() []DiffHunk GetHunks returns all hunks from the result
GetSelectedLine func (d *DiffViewer) GetSelectedLine() *DiffLine GetSelectedLine returns the currently selected line
GetSelectedLines func (d *DiffViewer) GetSelectedLines() []DiffLine GetSelectedLines returns all selected lines
GetStats func (d *DiffViewer) GetStats() DiffStats GetStats returns diff statistics
NextChange func (d *DiffViewer) NextChange() NextChange jumps to the next changed line
NextHunk func (d *DiffViewer) NextHunk() NextHunk moves to the next hunk
PrevChange func (d *DiffViewer) PrevChange() PrevChange jumps to the previous changed line
PrevHunk func (d *DiffViewer) PrevHunk() PrevHunk moves to the previous hunk
SelectAllInHunk func (d *DiffViewer) SelectAllInHunk() SelectAllInHunk selects all changed lines in the current hunk
SetContextLines func (d *DiffViewer) SetContextLines(count int) *DiffViewer SetContextLines sets context lines around changes
SetDiff func (d *DiffViewer) SetDiff(old, new string) *DiffViewer SetDiff computes and displays diff between old and new content
SetDiffResult func (d *DiffViewer) SetDiffResult(result *DiffResult) *DiffViewer SetDiffResult sets a pre-computed diff result
SetOnHunkAction func (d *DiffViewer) SetOnHunkAction(fn func(hunkIndex int, lines []DiffLine)) *DiffViewer SetOnHunkAction sets callback for hunk-level operations
SetOnLineSelect func (d *DiffViewer) SetOnLineSelect(fn func(line DiffLine)) *DiffViewer SetOnLineSelect sets callback for line selection
SetOnLinesAction func (d *DiffViewer) SetOnLinesAction(fn func(lines []DiffLine)) *DiffViewer SetOnLinesAction sets callback for selected lines operations
SetSelectionEnabled func (d *DiffViewer) SetSelectionEnabled(enabled bool) *DiffViewer SetSelectionEnabled enables/disables line selection mode (for staging)
SetShowLineNumbers func (d *DiffViewer) SetShowLineNumbers(show bool) *DiffViewer SetShowLineNumbers toggles line number display
SetSideBySide func (d *DiffViewer) SetSideBySide(enabled bool) *DiffViewer SetSideBySide enables/disables side-by-side mode
SetTitle func (d *DiffViewer) SetTitle(title string) *DiffViewer SetTitle sets the header title
SetUnifiedDiff func (d *DiffViewer) SetUnifiedDiff(diff string) *DiffViewer SetUnifiedDiff parses and displays a unified diff string (e.g., from git)
SetWordDiff func (d *DiffViewer) SetWordDiff(enabled bool) *DiffViewer SetWordDiff enables word-level diff highlighting
ToggleLineSelection func (d *DiffViewer) ToggleLineSelection() ToggleLineSelection toggles selection of the current line

Types

type DiffHunk struct DiffHunk represents a contiguous block of changes
FieldTypeDescription
Header string @@ -start,count +start,count @@ context
OldStart int Starting line in old file
OldCount int Number of lines from old file
NewStart int Starting line in new file
NewCount int Number of lines in new file
Lines []DiffLine Lines in this hunk
type DiffLine struct DiffLine represents a single line in the diff
FieldTypeDescription
Type DiffLineType
OldLineNo int Line number in old content (0 if added)
NewLineNo int Line number in new content (0 if removed)
Content string Line content without +/- prefix
Selected bool Whether line is selected (for staging)
HunkIndex int Index of hunk this line belongs to
type DiffResult struct DiffResult is the parsed output of a unified diff. Pass it to DiffViewer.SetResult or let DiffViewer.SetDiff parse the raw diff string for you.
FieldTypeDescription
OldName string Original file name
NewName string New file name
Hunks []DiffHunk All hunks
Binary bool True if binary file
type DiffStats struct DiffStats is a summary count of additions, deletions, and affected files across an entire diff. Returned by DiffViewer.GetStats.
FieldTypeDescription
Additions int
Deletions int
Files int
type DiffLineType DiffLineType indicates the type of diff line
ConstantDescription
DiffLineContext
DiffLineAdded
DiffLineRemoved
DiffLineHeader
DiffLineFile