Skip to content
amterpPublic

About

A Go library for diffing text, with colors.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

go-delta

A Go library for rendering colored terminal diffs with word-level emphasis.

go-delta produces human-readable, unified or side-by-side diffs with ANSI colors. Changed segments are highlighted at the word level so you can see exactly what changed within each line - not just that a line changed.

Inline:

Inline diff example

Side-by-side:

Side-by-side diff example

Install

go get github.com/amterp/go-delta

Quick Start

import gd "github.com/amterp/go-delta"

// Simple diff with defaults
output := gd.Diff(old, new)

// Customized diff
output := gd.DiffWith(old, new,
    gd.WithLayout(gd.LayoutSideBySide),
    gd.WithContextLines(5),
    gd.WithWidth(120),
)

Diff and DiffWith return an empty string when the inputs are identical.

Options

Option Default Description
WithLayout(mode) LayoutInline Layout mode (see below)
WithContextLines(n) 3 Unchanged lines shown around each change
WithColor(on) auto Force color on/off (auto-detects TTY)
WithWidth(cols) 0 Wrap inline lines at this width; auto-detect width for side-by-side modes when 0

Color auto-detection respects NO_COLOR and FORCE_COLOR environment variables.

Layouts

  • LayoutInline (default) - removals and additions on separate lines. Always shows full content.
  • LayoutSideBySide - old and new text in side-by-side panels. Lines that exceed terminal width are truncated.
  • LayoutPreferSideBySide - uses side-by-side when content fits within the terminal width, falls back to inline otherwise.

With LayoutInline, a positive WithWidth wraps long lines at whitespace and aligns continuations under the text, past the line numbers and change marker. Colors and word emphasis continue across wraps. Whitespace is preserved; words longer than the available space split across lines. Tabs use eight-column stops. Widths too small for the gutter still allow at least one text column; a tab, wide character, or character with combining marks that cannot fit alone remains intact. Without a positive width, inline lines stay unwrapped, including the fallback from LayoutPreferSideBySide.

// Always side-by-side
gd.DiffWith(old, new, gd.WithLayout(gd.LayoutSideBySide))

// Side-by-side when it fits, inline otherwise
gd.DiffWith(old, new, gd.WithLayout(gd.LayoutPreferSideBySide))

Features

  • Word-level emphasis - changed words are highlighted within changed lines, not just the whole line
  • Three layouts - inline, side-by-side, or auto-fallback
  • ANSI-aware - correctly handles input that already contains ANSI escape codes
  • Wide character support - CJK and other double-width characters are measured correctly
  • Smart line pairing - modified lines are paired using a greedy forward-search algorithm (inspired by Delta)
  • Hunk separators - groups of changes are separated with context, just like unified diffs

How It Works

go-delta runs a three-stage pipeline:

  1. Myers diff - computes the minimal edit script between old and new
  2. Needleman-Wunsch alignment - aligns tokens within changed line pairs to find word-level differences
  3. Rendering - formats the output as inline or side-by-side with ANSI colors

About

A Go library for diffing text, with colors.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages