patchview

package
v0.25.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColorizedPatchSummary added in v0.24.0

func ColorizedPatchSummary(style PatchViewStyle, patches []*diferenco.Patch) string

ColorizedPatchSummary is like SummarizePatches but renders the +A/-D segments with the same Addition/Deletion colors used in the file list, so the Files: row of the top header matches the per-file stats visually.

Pass DefaultStyle() (or the same style you pass via WithStyle) so the dark/light theme stays consistent. Empty input returns "".

The rest of the string ("N files changed, ") uses the caller's default foreground — we intentionally do NOT wrap it with HeaderMeta because the header renderer already applies HeaderMeta-like keying via the "Files:" prefix and double-styling would dim the +A/-D too much.

func RenderCommitHeader added in v0.24.0

func RenderCommitHeader(style PatchViewStyle, hash, author, date, subject, files string) string

RenderCommitHeader renders the canonical commit-style header block (Commit / Author / Date / Subject [/ Files]) to a plain (but ANSI- colored) string with one row per non-empty field. Unlike the TUI top header it does not truncate to terminal width — callers print it directly to stdout.

Use this when you want to surface commit metadata outside the interactive view (e.g. when there are no patches to navigate and the TUI is skipped entirely).

func RenderHeaderEntries added in v0.24.0

func RenderHeaderEntries(style PatchViewStyle, entries []HeaderEntry) string

RenderHeaderEntries renders a list of HeaderEntry rows as a plain (ANSI-colored) string, using the same key padding + highlight rules as the in-TUI top header. Returns "" for an empty input so callers can cheaply skip the row.

func Run

func Run(patches []*diferenco.Patch, opts ...Option) error

Run starts the interactive patch navigation view.

When patches is empty the TUI is skipped — there is nothing meaningful to navigate. If the caller configured a top header (e.g. via WithCommitHeader) we still print it to stdout followed by a "No changes" line, so callers like `hot show` keep showing commit metadata for merge / empty commits instead of degenerating to a bare "No changes" message.

func SummarizePatches added in v0.24.0

func SummarizePatches(patches []*diferenco.Patch) string

SummarizePatches returns a compact one-line summary of a patch set in the form "N files changed, +A -D". Empty input returns "". This is shared by command_diff / command_show / showdiff / show so the header subtitle stays consistent across entry points.

The returned string is plain text (no ANSI). For a colorized variant suitable for direct use as a HeaderEntry value, use ColorizedPatchSummary.

Types

type CursorSetter

type CursorSetter interface {
	SetCursor(idx int)
}

CursorSetter is an optional interface for StatusBar implementations that need to be notified when the cursor changes.

type DefaultStatusBar

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

DefaultStatusBar is the default status bar implementation.

Layout (single content line inside a rounded border):

┌────────────────────────────────────────────────────────────┐
│ M │ path/to/file.go               +12 -3            2/8    │
└────────────────────────────────────────────────────────────┘

Total height is 3 (1 content line + 2 border lines).

func NewDefaultStatusBar

func NewDefaultStatusBar() *DefaultStatusBar

NewDefaultStatusBar creates a new DefaultStatusBar.

func (*DefaultStatusBar) Height

func (s *DefaultStatusBar) Height() int

Height returns the total height of the status bar including its rounded border (1 content line + 2 border lines = 3).

func (*DefaultStatusBar) SetCursor

func (s *DefaultStatusBar) SetCursor(idx int)

SetCursor sets the current cursor position.

func (*DefaultStatusBar) SetPatches

func (s *DefaultStatusBar) SetPatches(patches []*diferenco.Patch)

SetPatches sets the patches data.

func (*DefaultStatusBar) SetStyle

func (s *DefaultStatusBar) SetStyle(style PatchViewStyle)

SetStyle sets the style for the status bar.

func (*DefaultStatusBar) View

func (s *DefaultStatusBar) View(width int) string

View renders the status bar as a rounded-border card of the requested total width. The contract is:

lipgloss.Width(View(w))  == w
lipgloss.Height(View(w)) == 3

In lipgloss v2 Style.Width(w) is the TOTAL block width (border + content), so we pass `w` directly. The inner content width fed into renderContent is (w - 2) so the inner text does not collide with the side borders.

type DiffViewStyle

type DiffViewStyle struct {
	DividerLine LineStyle      // Hunk divider line style (@@ -1,3 +1,4 @@)
	MissingLine LineStyle      // Missing line style (used in Split view)
	EqualLine   LineStyle      // Unchanged line style
	InsertLine  LineStyle      // Inserted line style
	DeleteLine  LineStyle      // Deleted line style
	FileName    lipgloss.Style // File name style
	FileMeta    lipgloss.Style // File metadata style
}

DiffViewStyle defines the complete style for DiffView.

func DefaultDarkDiffViewStyle

func DefaultDarkDiffViewStyle() DiffViewStyle

DefaultDarkDiffViewStyle returns the dark theme style.

func DefaultDiffViewStyle

func DefaultDiffViewStyle() DiffViewStyle

DefaultDiffViewStyle automatically selects theme based on terminal background.

func DefaultLightDiffViewStyle

func DefaultLightDiffViewStyle() DiffViewStyle

DefaultLightDiffViewStyle returns the light theme style. Color scheme based on One Light Pro (clear, bright, moderate contrast).

type HeaderEntry added in v0.24.0

type HeaderEntry struct {
	Key       string // canonical key text without the trailing ":" (e.g. "Commit").
	Value     string
	Highlight bool // if true, render Value with style.HeaderHash (bold + accent).
}

HeaderEntry is a single "Key: value" row in the top header block.

When Key == "", the row is rendered as a plain continuation line indented to topHeaderKeyColumn (used by legacy WithHeader callers that pass a free-form multi-line string).

Highlight = true asks the view to render Value with the bold accent style (style.HeaderHash) — used for commit hashes.

type LineStyle

type LineStyle struct {
	LineNumber lipgloss.Style // Line number style
	Code       lipgloss.Style // Code content style
}

LineStyle defines the style for a single line.

type Option

type Option func(*PatchView)

Option configures the patch view.

func WithCommitHeader added in v0.24.0

func WithCommitHeader(hash, author, date, subject string) Option

WithCommitHeader produces the canonical commit-style top header:

Commit:  <hash>            (hash highlighted via style.HeaderHash)
Author:  <author>
Date:    <date>
Subject: <subject>
Files:   <files>           (only included when non-empty)

Empty fields drop their entire row, so callers can pass "" for missing metadata without producing blank lines.

func WithCommitHeaderWithFiles added in v0.24.0

func WithCommitHeaderWithFiles(hash, author, date, subject, files string) Option

WithCommitHeaderWithFiles is like WithCommitHeader but appends an extra "Files: <summary>" row (typically produced by SummarizePatches).

func WithHeader added in v0.24.0

func WithHeader(text string) Option

WithHeader sets a free-form multi-line top header. Each "\n"-separated segment becomes its own row, rendered as a borderless continuation line (no "Key:" prefix, indented to align with the value column used by WithCommitHeader).

Pass an empty string (or do not call this option) to suppress the top header entirely — the view will collapse back to just the status bar.

For commit-style metadata prefer WithCommitHeader, which produces the canonical 4-row "Commit/Author/Date/Subject" layout with a highlighted hash; use WithHeader for ad-hoc diff range descriptions and similar.

func WithHeaderEntries added in v0.24.0

func WithHeaderEntries(entries ...HeaderEntry) Option

WithHeaderEntries sets a structured top header, one entry per row, of the form "Key: value". A nil key argument produces a continuation row (no key prefix) that still aligns under the value column.

Use this when you need full control (e.g. mixing highlighted and plain values); prefer WithCommitHeader for the common commit-metadata case.

func WithListWidth

func WithListWidth(pct int) Option

WithListWidth sets the file list width percentage (default 20).

func WithStatusBar

func WithStatusBar(sb StatusBar) Option

WithStatusBar sets a custom status bar.

func WithStyle

func WithStyle(style PatchViewStyle) Option

WithStyle sets a custom style.

type PatchRenderer

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

PatchRenderer renders a diferenco.Patch for display. It handles line numbers, syntax highlighting, and horizontal scrolling.

func NewPatchRenderer

func NewPatchRenderer() *PatchRenderer

NewPatchRenderer creates a new PatchRenderer with default style.

func (*PatchRenderer) HunkOffsets

func (r *PatchRenderer) HunkOffsets() []int

HunkOffsets returns the starting line offset for each hunk. This is used for [ and ] navigation between hunks.

func (*PatchRenderer) Render

func (r *PatchRenderer) Render() string

Render renders the patch content for the current viewport.

func (*PatchRenderer) SetDarkBackground

func (r *PatchRenderer) SetDarkBackground(dark bool)

SetDarkBackground sets the terminal background mode.

func (*PatchRenderer) SetLineNumbers

func (r *PatchRenderer) SetLineNumbers(enabled bool)

SetLineNumbers sets whether to show line numbers.

func (*PatchRenderer) SetPatch

func (r *PatchRenderer) SetPatch(p *diferenco.Patch)

SetPatch sets the patch to render.

func (*PatchRenderer) SetSize

func (r *PatchRenderer) SetSize(width, height int)

SetSize sets the rendering area size.

func (*PatchRenderer) SetStyle

func (r *PatchRenderer) SetStyle(style PatchViewStyle)

SetStyle sets the style for rendering.

func (*PatchRenderer) SetSyntaxHighlight

func (r *PatchRenderer) SetSyntaxHighlight(enabled bool)

SetSyntaxHighlight sets whether to enable syntax highlighting.

func (*PatchRenderer) SetXOffset

func (r *PatchRenderer) SetXOffset(offset int)

SetXOffset sets the horizontal scroll offset. Note: Unlike SetYOffset, there's no upper bound because line widths vary and may contain ANSI escape sequences. The render function handles out-of-bounds offsets gracefully by showing empty content.

func (*PatchRenderer) SetYOffset

func (r *PatchRenderer) SetYOffset(offset int)

SetYOffset sets the vertical scroll offset.

func (*PatchRenderer) TotalLines

func (r *PatchRenderer) TotalLines() int

TotalLines returns the total number of lines in the patch.

func (*PatchRenderer) XOffset

func (r *PatchRenderer) XOffset() int

XOffset returns the current horizontal offset.

func (*PatchRenderer) YOffset

func (r *PatchRenderer) YOffset() int

YOffset returns the current vertical offset.

type PatchView

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

PatchView is an interactive patch navigation view.

func NewPatchView

func NewPatchView(patches []*diferenco.Patch, opts ...Option) *PatchView

NewPatchView creates a new PatchView.

func (*PatchView) Init

func (pv *PatchView) Init() tea.Cmd

func (*PatchView) Update

func (pv *PatchView) Update(msg tea.Msg) (tea.Model, tea.Cmd)

func (*PatchView) View

func (pv *PatchView) View() tea.View

type PatchViewStyle

type PatchViewStyle struct {
	// File list styles
	Addition lipgloss.Style
	Deletion lipgloss.Style
	Selected lipgloss.Style

	// Diff view styles (using LineStyle for background fill)
	DiffStyle DiffViewStyle

	// UI styles
	HeaderBg    lipgloss.Style
	FileCount   lipgloss.Style
	Separator   lipgloss.Style
	PathDisplay lipgloss.Style
	FilesTitle  lipgloss.Style
	FooterBg    lipgloss.Style

	// Border colors for the top header / status bar cards.
	// These are intentionally neutral (no focus highlight) so the cards
	// stay visually stable while the user navigates panes.
	HeaderBorder color.Color

	// Subdued text used inside the top header card (e.g. commit metadata).
	HeaderMeta lipgloss.Style

	// Highlight style for the commit hash shown in the top header.
	HeaderHash lipgloss.Style

	// Status styles for header
	StatusAdded    lipgloss.Style
	StatusDeleted  lipgloss.Style
	StatusRenamed  lipgloss.Style
	StatusModified lipgloss.Style
}

PatchViewStyle defines the visual style for the patch view.

func DefaultDarkStyle

func DefaultDarkStyle() PatchViewStyle

DefaultDarkStyle returns the dark theme style.

func DefaultLightStyle

func DefaultLightStyle() PatchViewStyle

DefaultLightStyle returns the light theme style.

func DefaultStyle

func DefaultStyle() PatchViewStyle

DefaultStyle returns the default style with auto-detected theme.

type PatchesSetter

type PatchesSetter interface {
	SetPatches(patches []*diferenco.Patch)
}

PatchesSetter is an optional interface for StatusBar implementations that need access to the patches data.

type StatusBar

type StatusBar interface {
	View(width int) string
	Height() int
}

StatusBar is the interface for rendering a status bar in the patch view.

Contract (locked down by view_test.go):

  • lipgloss.Width(View(w)) == w
  • lipgloss.Height(View(w)) == Height()

Violating either side causes JoinVertical(header, mainContent, footer) to either misalign columns or push the diff pane into the file list region.

type SyntaxHighlighter

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

SyntaxHighlighter is a syntax highlighter.

func NewSyntaxHighlighter

func NewSyntaxHighlighter(filename string, isDark bool) *SyntaxHighlighter

NewSyntaxHighlighter creates a syntax highlighter. filename: used for language detection isDark: whether the background is dark

func (*SyntaxHighlighter) ClearCache

func (h *SyntaxHighlighter) ClearCache()

ClearCache clears the cache.

func (*SyntaxHighlighter) Enabled

func (h *SyntaxHighlighter) Enabled() bool

Enabled returns whether the highlighter is enabled.

func (*SyntaxHighlighter) Highlight

func (h *SyntaxHighlighter) Highlight(source, bgColor string) string

Highlight highlights code. source: original code bgColor: background color (hex format, e.g. "#303a30")

Jump to

Keyboard shortcuts

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