diffview

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 22, 2026 License: MIT Imports: 4 Imported by: 0

README

Diff View

Color-coded unified diff renderer.

diff-view preview

Install

glyph add diff-view

This copies diff-view.go (and its test file) into your repo at the path your glyph.json aliases declare. After install, the file is yours: edit it, refactor it, rename it. There is no diff-view library to keep in sync.

Hello, world

package main

import (
	"fmt"

	diffview "github.com/truffle-dev/glyph/components/diff-view"
	"github.com/truffle-dev/glyph/components/theme"
)

func main() {
	raw := `--- a/server.go
+++ b/server.go
@@ -1,3 +1,4 @@
 package server
+
 func Run() {}
`
	d := diffview.New(theme.Default).
		WithSize(96, 18).
		WithLines(diffview.ParseUnified(raw))
	fmt.Println(d.View())
}

API surface

Package: diffview

Types

  • Kind
  • Line
  • View

Functions and methods

  • New
  • WithLines
  • WithSize
  • WithLineNumbers
  • Offset
  • Init
  • Update
  • View
  • TotalLines
  • ParseUnified

Dependencies

  • glyph component theme (installed automatically)
  • github.com/charmbracelet/bubbletea@v1.3.10
  • github.com/charmbracelet/lipgloss@v1.1.0

Notes

Pass WithLines(ParseUnified(diff)) to render git diff -u output. To render a diff you generated yourself, construct []Line directly. Up/Down scrolls one line, PgUp/PgDn scrolls a window, Home/End jumps. Lines longer than the body width truncate with an ellipsis.

See also

License

MIT, same as the rest of glyph.

Documentation

Overview

Package diffview renders a unified diff as a color-coded, scrollable terminal block. Additions are green, removals are red, hunk headers are highlighted, context lines are muted. Optional line numbers from either side of the diff.

The component is render-only: a consumer parses (or generates) a list of Line values, hands them to WithLines, and the view does the rest. A ParseUnified helper is provided for the common `diff -u` case.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Kind

type Kind int

Kind classifies a diff line.

const (
	KindContext Kind = iota
	KindAdded
	KindRemoved
	KindHunk // @@ -1,3 +1,4 @@
	KindFile // --- a/file, +++ b/file
)

type Line

type Line struct {
	Kind    Kind
	Old     int // 1-based old line number, 0 if not applicable
	New     int // 1-based new line number, 0 if not applicable
	Content string
}

Line is a single rendered row in the diff.

func ParseUnified

func ParseUnified(diff string) []Line

ParseUnified converts a standard unified-diff string into a slice of Line values. Lines starting with "+++", "---", "diff", or "index" are classified as KindFile. Lines starting with "@@" are KindHunk. The usual leading-space / + / - convention drives KindContext, KindAdded, KindRemoved.

type View

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

View is a Bubble Tea model that renders a slice of Line values.

func New

func New(t theme.Theme) View

New constructs an empty View with line numbers enabled.

func (View) Init

func (v View) Init() tea.Cmd

Init implements tea.Model.

func (View) Offset

func (v View) Offset() int

Offset returns the current scroll offset (lines from top).

func (View) TotalLines

func (v View) TotalLines() int

TotalLines returns the number of rendered rows. Equals len(WithLines).

func (View) Update

func (v View) Update(msg tea.Msg) (View, tea.Cmd)

Update handles scroll keys.

func (View) View

func (v View) View() string

View renders the visible window. Lines longer than the available content width are truncated with an ellipsis.

func (View) WithLineNumbers

func (v View) WithLineNumbers(show bool) View

WithLineNumbers toggles the left-side line-number columns.

func (View) WithLines

func (v View) WithLines(lines []Line) View

WithLines sets the diff content. Offset resets to 0.

func (View) WithSize

func (v View) WithSize(w, h int) View

WithSize sets rendered width and visible-row height.

Jump to

Keyboard shortcuts

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