@@ -1,6 +1,7 @@
1package diffview
1package diffview
@@ -125,22 +126,34 @@ func (dv *DiffView) String() string {
126 dv.detectWidth()
127 dv.detectWidth()
128− lineNumberWidth := func(start, num int) int {
129+ codeWidth := dv.width - leadingSymbolsSize
129− return len(strconv.Itoa(start + num))
130+ beforeNumDigits, afterNumDigits := dv.lineNumberDigits()
132 var b strings.Builder
132 var b strings.Builder
134− for _, h := range dv.unified.Hunks {
134+ for i, h := range dv.unified.Hunks {
135+ beforeShownLines, afterShownLines := dv.hunkShownLines(i)
138+ b.WriteString(dv.style.DividerLine.LineNumber.Render(pad("…", beforeNumDigits)))
139+ b.WriteString(dv.style.DividerLine.LineNumber.Render(pad("…", afterNumDigits)))
141+ b.WriteString(dv.style.DividerLine.Code.Width(codeWidth + leadingSymbolsSize).Render(
143+ " @@ -%d,%d +%d,%d @@",
135 beforeLine := h.FromLine
152 beforeLine := h.FromLine
136 afterLine := h.ToLine
153 afterLine := h.ToLine
138− beforeNumDigits := lineNumberWidth(h.FromLine, len(h.Lines))
139− afterNumDigits := lineNumberWidth(h.ToLine, len(h.Lines))
141 for _, l := range h.Lines {
155 for _, l := range h.Lines {
142 content := strings.TrimSuffix(l.Content, "\n")
156 content := strings.TrimSuffix(l.Content, "\n")
143− codeWidth := dv.width - leadingSymbolsSize
145 switch l.Kind {
158 switch l.Kind {
146 case udiff.Equal:
159 case udiff.Equal:
@@ -194,6 +207,33 @@ func (dv *DiffView) computeDiff() error {
194 return dv.err
207 return dv.err
210+// lineNumberDigits calculates the maximum number of digits needed for before and
211+// after line numbers.
212+func (dv *DiffView) lineNumberDigits() (maxBefore, maxAfter int) {
213+ for _, h := range dv.unified.Hunks {
214+ maxBefore = max(maxBefore, len(strconv.Itoa(h.FromLine+len(h.Lines))))
215+ maxAfter = max(maxAfter, len(strconv.Itoa(h.ToLine+len(h.Lines))))
220+// hunkShownLines calculates the number of lines shown in a hunk for both before
221+// and after versions.
222+func (dv *DiffView) hunkShownLines(i int) (before, after int) {
223+ for _, l := range dv.unified.Hunks[i].Lines {
197func (dv *DiffView) detectWidth() {
237func (dv *DiffView) detectWidth() {
198 if dv.width > 0 {
238 if dv.width > 0 {