diff

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package diff renders unified diff text into styled lines, shared by the edit-result chat renderer and the permission prompt (doc 02 section 9). The input is the diff the core already computed and shipped in the event's consequence; this package never re-derives a change from raw tool input (D2, D15). It layers chroma syntax colors under the diff backgrounds, marks the intra-line spans that actually changed, and offers unified and side-by-side layouts.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Render

func Render(diffText string, width int, th theme.Theme, mode Mode) []string

Render turns unified diff text into styled lines at the given width. Same input, width, theme, and mode give the same lines; results are cached because the same edit renders twice, once in the permission prompt and once in the chat result.

Types

type Mode

type Mode int

Mode selects the layout.

const (
	// Auto renders side-by-side past the width threshold, unified below.
	Auto Mode = iota
	Unified
	Split
)

type Viewer

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

Viewer is the stateful view over a single diff: the pure Render draws the cells, and the Viewer adds what a person needs on top of them, namely a live unified/split toggle, per-hunk navigation with a counter, and horizontal scroll for lines too wide to fit (doc 02 section 9, plan 02 slice 2). It holds no terminal state of its own beyond a size, a mode, a focused hunk, and a horizontal offset, so the same Viewer with the same fields yields the same frame, which is what the goldens pin.

func NewViewer

func NewViewer(diffText string, th theme.Theme) Viewer

NewViewer builds a viewer over diffText in Auto layout. Size defaults to something usable until SetSize arrives from the first resize.

func (Viewer) Focus

func (v Viewer) Focus() int

Focus is the zero-based index of the focused hunk, clamped into range.

func (Viewer) Hunks

func (v Viewer) Hunks() int

Hunks is how many hunks the current layout has.

func (Viewer) Mode

func (v Viewer) Mode() Mode

Mode reports the layout the viewer will draw with.

func (*Viewer) NextHunk

func (v *Viewer) NextHunk()

NextHunk and PrevHunk move the focus and reset horizontal scroll, so a jump always lands with the hunk's left edge in view.

func (*Viewer) PrevHunk

func (v *Viewer) PrevHunk()

func (*Viewer) ScrollLeft

func (v *Viewer) ScrollLeft()

func (*Viewer) ScrollRight

func (v *Viewer) ScrollRight()

ScrollRight and ScrollLeft pan a wide line horizontally rather than wrapping it into noise.

func (*Viewer) SetMode

func (v *Viewer) SetMode(m Mode)

SetMode forces a layout; Auto lets width decide.

func (*Viewer) SetSize

func (v *Viewer) SetSize(width, height int)

SetSize records the space the viewer draws into. Width feeds the Auto layout threshold; height bounds the visible window.

func (*Viewer) Toggle

func (v *Viewer) Toggle()

Toggle flips between unified and split, the live toggle the DoD asks for. From Auto it commits to the opposite of what Auto would pick at the current width, so the first press always visibly changes layout.

func (Viewer) View

func (v Viewer) View() []string

View renders the visible window: a counter frame line, then the diff body scrolled so the focused hunk sits at the top and panned by the horizontal offset, each line clipped to width.

Jump to

Keyboard shortcuts

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