9 "github.com/aymanbagabas/go-udiff"
10 "github.com/aymanbagabas/go-udiff/myers"
11 "github.com/charmbracelet/lipgloss/v2"
12 "github.com/charmbracelet/x/ansi"
16 leadingSymbolsSize = 2
28 layoutUnified layout = iota + 1
32// DiffView represents a view for displaying differences between two files.
46 unified udiff.UnifiedDiff
49 splitHunks []splitHunk
52 fullCodeWidth int // with leading symbols
53 extraColOnAfter bool // add extra column on after panel
58// New creates a new DiffView with default settings.
61 layout: layoutUnified,
62 contextLines: udiff.DefaultContextLines,
65 if lipgloss.HasDarkBackground(os.Stdin, os.Stdout) {
66 dv.style = DefaultDarkStyle
68 dv.style = DefaultLightStyle
73// Unified sets the layout of the DiffView to unified.
74func (dv *DiffView) Unified() *DiffView {
75 dv.layout = layoutUnified
79// Split sets the layout of the DiffView to split (side-by-side).
80func (dv *DiffView) Split() *DiffView {
81 dv.layout = layoutSplit
85// Before sets the "before" file for the DiffView.
86func (dv *DiffView) Before(path, content string) *DiffView {
87 dv.before = file{path: path, content: content}
91// After sets the "after" file for the DiffView.
92func (dv *DiffView) After(path, content string) *DiffView {
93 dv.after = file{path: path, content: content}
97// ContextLines sets the number of context lines for the DiffView.
98func (dv *DiffView) ContextLines(contextLines int) *DiffView {
99 dv.contextLines = contextLines
103// Style sets the style for the DiffView.
104func (dv *DiffView) Style(style Style) *DiffView {
109// LineNumbers sets whether to display line numbers in the DiffView.
110func (dv *DiffView) LineNumbers(lineNumbers bool) *DiffView {
111 dv.lineNumbers = lineNumbers
115// SyntaxHightlight sets whether to enable syntax highlighting in the DiffView.
116func (dv *DiffView) SyntaxHightlight(highlight bool) *DiffView {
117 dv.highlight = highlight
121// Height sets the height of the DiffView.
122func (dv *DiffView) Height(height int) *DiffView {
127// Width sets the width of the DiffView.
128func (dv *DiffView) Width(width int) *DiffView {
133// String returns the string representation of the DiffView.
134func (dv *DiffView) String() string {
135 if err := dv.computeDiff(); err != nil {
138 dv.convertDiffToSplit()
148 style := lipgloss.NewStyle()
150 style = style.MaxWidth(dv.width)
155 return style.Render(strings.TrimSuffix(dv.renderUnified(), "\n"))
157 return style.Render(strings.TrimSuffix(dv.renderSplit(), "\n"))
159 panic("unknown diffview layout")
163// computeDiff computes the differences between the "before" and "after" files.
164func (dv *DiffView) computeDiff() error {
169 dv.edits = myers.ComputeEdits( //nolint:staticcheck
173 dv.unified, dv.err = udiff.ToUnifiedDiff(
183// convertDiffToSplit converts the unified diff to a split diff if the layout is
185func (dv *DiffView) convertDiffToSplit() {
186 if dv.layout != layoutSplit {
190 dv.splitHunks = make([]splitHunk, len(dv.unified.Hunks))
191 for i, h := range dv.unified.Hunks {
192 dv.splitHunks[i] = hunkToSplit(h)
196// adjustStyles adjusts adds padding and alignment to the styles.
197func (dv *DiffView) adjustStyles() {
198 dv.style.MissingLine.LineNumber = setPadding(dv.style.MissingLine.LineNumber)
199 dv.style.DividerLine.LineNumber = setPadding(dv.style.DividerLine.LineNumber)
200 dv.style.EqualLine.LineNumber = setPadding(dv.style.EqualLine.LineNumber)
201 dv.style.InsertLine.LineNumber = setPadding(dv.style.InsertLine.LineNumber)
202 dv.style.DeleteLine.LineNumber = setPadding(dv.style.DeleteLine.LineNumber)
205// detectNumDigits calculates the maximum number of digits needed for before and
206// after line numbers.
207func (dv *DiffView) detectNumDigits() {
208 dv.beforeNumDigits = 0
209 dv.afterNumDigits = 0
211 for _, h := range dv.unified.Hunks {
212 dv.beforeNumDigits = max(dv.beforeNumDigits, len(strconv.Itoa(h.FromLine+len(h.Lines))))
213 dv.afterNumDigits = max(dv.afterNumDigits, len(strconv.Itoa(h.ToLine+len(h.Lines))))
217func setPadding(s lipgloss.Style) lipgloss.Style {
218 return s.Padding(0, lineNumPadding).Align(lipgloss.Right)
221// detectCodeWidth calculates the maximum width of code lines in the diff view.
222func (dv *DiffView) detectCodeWidth() {
225 dv.detectUnifiedCodeWidth()
227 dv.detectSplitCodeWidth()
229 dv.fullCodeWidth = dv.codeWidth + leadingSymbolsSize
232// detectUnifiedCodeWidth calculates the maximum width of code lines in a
234func (dv *DiffView) detectUnifiedCodeWidth() {
237 for _, h := range dv.unified.Hunks {
238 shownLines := ansi.StringWidth(dv.hunkLineFor(h))
240 for _, l := range h.Lines {
241 lineWidth := ansi.StringWidth(strings.TrimSuffix(l.Content, "\n")) + 1
242 dv.codeWidth = max(dv.codeWidth, lineWidth, shownLines)
247// detectSplitCodeWidth calculates the maximum width of code lines in a
249func (dv *DiffView) detectSplitCodeWidth() {
252 for i, h := range dv.splitHunks {
253 shownLines := ansi.StringWidth(dv.hunkLineFor(dv.unified.Hunks[i]))
255 for _, l := range h.lines {
257 codeWidth := ansi.StringWidth(strings.TrimSuffix(l.before.Content, "\n")) + 1
258 dv.codeWidth = max(dv.codeWidth, codeWidth, shownLines)
261 codeWidth := ansi.StringWidth(strings.TrimSuffix(l.after.Content, "\n")) + 1
262 dv.codeWidth = max(dv.codeWidth, codeWidth, shownLines)
268// resizeCodeWidth resizes the code width to fit within the specified width.
269func (dv *DiffView) resizeCodeWidth() {
270 fullNumWidth := dv.beforeNumDigits + dv.afterNumDigits
271 fullNumWidth += lineNumPadding * 4 // left and right padding for both line numbers
275 dv.codeWidth = dv.width - fullNumWidth - leadingSymbolsSize
277 remainingWidth := dv.width - fullNumWidth - leadingSymbolsSize*2
278 dv.codeWidth = remainingWidth / 2
279 dv.extraColOnAfter = isOdd(remainingWidth)
282 dv.fullCodeWidth = dv.codeWidth + leadingSymbolsSize
285// renderUnified renders the unified diff view as a string.
286func (dv *DiffView) renderUnified() string {
287 var b strings.Builder
289 fullContentStyle := lipgloss.NewStyle().MaxWidth(dv.fullCodeWidth)
291 for _, h := range dv.unified.Hunks {
293 b.WriteString(dv.style.DividerLine.LineNumber.Render(pad("…", dv.beforeNumDigits)))
294 b.WriteString(dv.style.DividerLine.LineNumber.Render(pad("…", dv.afterNumDigits)))
296 content := ansi.Truncate(dv.hunkLineFor(h), dv.fullCodeWidth, "…")
297 b.WriteString(dv.style.DividerLine.Code.Width(dv.fullCodeWidth).Render(content))
300 beforeLine := h.FromLine
301 afterLine := h.ToLine
303 for _, l := range h.Lines {
304 content := strings.TrimSuffix(l.Content, "\n")
305 content = ansi.Truncate(content, dv.codeWidth, "…")
310 b.WriteString(dv.style.EqualLine.LineNumber.Render(pad(beforeLine, dv.beforeNumDigits)))
311 b.WriteString(dv.style.EqualLine.LineNumber.Render(pad(afterLine, dv.afterNumDigits)))
313 b.WriteString(fullContentStyle.Render(
314 dv.style.EqualLine.Code.Width(dv.fullCodeWidth).Render(" " + content),
320 b.WriteString(dv.style.InsertLine.LineNumber.Render(pad(" ", dv.beforeNumDigits)))
321 b.WriteString(dv.style.InsertLine.LineNumber.Render(pad(afterLine, dv.afterNumDigits)))
323 b.WriteString(fullContentStyle.Render(
324 dv.style.InsertLine.Symbol.Render("+ ") +
325 dv.style.InsertLine.Code.Width(dv.codeWidth).Render(content),
330 b.WriteString(dv.style.DeleteLine.LineNumber.Render(pad(beforeLine, dv.beforeNumDigits)))
331 b.WriteString(dv.style.DeleteLine.LineNumber.Render(pad(" ", dv.afterNumDigits)))
333 b.WriteString(fullContentStyle.Render(
334 dv.style.DeleteLine.Symbol.Render("- ") +
335 dv.style.DeleteLine.Code.Width(dv.codeWidth).Render(content),
346// renderSplit renders the split (side-by-side) diff view as a string.
347func (dv *DiffView) renderSplit() string {
348 var b strings.Builder
350 beforeFullContentStyle := lipgloss.NewStyle().MaxWidth(dv.fullCodeWidth)
351 afterFullContentStyle := lipgloss.NewStyle().MaxWidth(dv.fullCodeWidth + btoi(dv.extraColOnAfter))
353 for i, h := range dv.splitHunks {
355 b.WriteString(dv.style.DividerLine.LineNumber.Render(pad("…", dv.beforeNumDigits)))
357 content := ansi.Truncate(dv.hunkLineFor(dv.unified.Hunks[i]), dv.fullCodeWidth, "…")
358 b.WriteString(dv.style.DividerLine.Code.Width(dv.fullCodeWidth).Render(content))
360 b.WriteString(dv.style.DividerLine.LineNumber.Render(pad("…", dv.afterNumDigits)))
362 b.WriteString(dv.style.DividerLine.Code.Width(dv.fullCodeWidth + btoi(dv.extraColOnAfter)).Render(" "))
365 beforeLine := h.fromLine
366 afterLine := h.toLine
368 for _, l := range h.lines {
369 var beforeContent string
370 var afterContent string
372 beforeContent = strings.TrimSuffix(l.before.Content, "\n")
373 beforeContent = ansi.Truncate(beforeContent, dv.codeWidth, "…")
376 afterContent = strings.TrimSuffix(l.after.Content, "\n")
377 afterContent = ansi.Truncate(afterContent, dv.codeWidth+btoi(dv.extraColOnAfter), "…")
381 case l.before == nil:
383 b.WriteString(dv.style.MissingLine.LineNumber.Render(pad(" ", dv.beforeNumDigits)))
385 b.WriteString(beforeFullContentStyle.Render(
386 dv.style.MissingLine.Code.Width(dv.fullCodeWidth).Render(" "),
388 case l.before.Kind == udiff.Equal:
390 b.WriteString(dv.style.EqualLine.LineNumber.Render(pad(beforeLine, dv.beforeNumDigits)))
392 b.WriteString(beforeFullContentStyle.Render(
393 dv.style.EqualLine.Code.Width(dv.fullCodeWidth).Render(" " + beforeContent),
396 case l.before.Kind == udiff.Delete:
398 b.WriteString(dv.style.DeleteLine.LineNumber.Render(pad(beforeLine, dv.beforeNumDigits)))
400 b.WriteString(beforeFullContentStyle.Render(
401 dv.style.DeleteLine.Symbol.Render("- ") +
402 dv.style.DeleteLine.Code.Width(dv.codeWidth).Render(beforeContent),
410 b.WriteString(dv.style.MissingLine.LineNumber.Render(pad(" ", dv.afterNumDigits)))
412 b.WriteString(afterFullContentStyle.Render(
413 dv.style.MissingLine.Code.Width(dv.fullCodeWidth + btoi(dv.extraColOnAfter)).Render(" "),
415 case l.after.Kind == udiff.Equal:
417 b.WriteString(dv.style.EqualLine.LineNumber.Render(pad(afterLine, dv.afterNumDigits)))
419 b.WriteString(afterFullContentStyle.Render(
420 dv.style.EqualLine.Code.Width(dv.fullCodeWidth + btoi(dv.extraColOnAfter)).Render(" " + afterContent),
423 case l.after.Kind == udiff.Insert:
425 b.WriteString(dv.style.InsertLine.LineNumber.Render(pad(afterLine, dv.afterNumDigits)))
427 b.WriteString(afterFullContentStyle.Render(
428 dv.style.InsertLine.Symbol.Render("+ ") +
429 dv.style.InsertLine.Code.Width(dv.codeWidth+btoi(dv.extraColOnAfter)).Render(afterContent),
441// hunkLineFor formats the header line for a hunk in the unified diff view.
442func (dv *DiffView) hunkLineFor(h *udiff.Hunk) string {
443 beforeShownLines, afterShownLines := dv.hunkShownLines(h)
446 " @@ -%d,%d +%d,%d @@ ",
454// hunkShownLines calculates the number of lines shown in a hunk for both before
455// and after versions.
456func (dv *DiffView) hunkShownLines(h *udiff.Hunk) (before, after int) {
457 for _, l := range h.Lines {
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list