@@ -137,7 +137,12 @@ func (dv *DiffView) String() string {
137137 dv.convertDiffToSplit()
138138 dv.adjustStyles()
139139 dv.detectNumDigits()
140− dv.detectCodeWidth()
140+
141+ if dv.width <= 0 {
142+ dv.detectCodeWidth()
143+ } else {
144+ dv.resizeCodeWidth()
145+ }
141146
142147 switch dv.layout {
143148 case layoutUnified:
@@ -254,6 +259,21 @@ func (dv *DiffView) detectSplitCodeWidth() {
254259 }
255260}
256261
262+// resizeCodeWidth resizes the code width to fit within the specified width.
263+func (dv *DiffView) resizeCodeWidth() {
264+ fullNumWidth := dv.beforeNumDigits + dv.afterNumDigits
265+ fullNumWidth += lineNumPadding * 4 // left and right padding for both line numbers
266+
267+ switch dv.layout {
268+ case layoutUnified:
269+ dv.codeWidth = dv.width - fullNumWidth - leadingSymbolsSize
270+ case layoutSplit:
271+ dv.codeWidth = (dv.width - fullNumWidth - leadingSymbolsSize*2) / 2
272+ }
273+
274+ dv.fullCodeWidth = dv.codeWidth + leadingSymbolsSize
275+}
276+
257277// renderUnified renders the unified diff view as a string.
258278func (dv *DiffView) renderUnified() string {
259279 var b strings.Builder
@@ -263,7 +283,8 @@ func (dv *DiffView) renderUnified() string {
263283 b.WriteString(dv.style.DividerLine.LineNumber.Render(pad("…", dv.beforeNumDigits)))
264284 b.WriteString(dv.style.DividerLine.LineNumber.Render(pad("…", dv.afterNumDigits)))
265285 }
266− b.WriteString(dv.style.DividerLine.Code.Width(dv.fullCodeWidth).Render(dv.hunkLineFor(h)))
286+ content := ansi.Truncate(dv.hunkLineFor(h), dv.fullCodeWidth, "…")
287+ b.WriteString(dv.style.DividerLine.Code.Width(dv.fullCodeWidth).Render(content))
267288 b.WriteRune('\n')
268289
269290 beforeLine := h.FromLine
@@ -271,6 +292,7 @@ func (dv *DiffView) renderUnified() string {
271292
272293 for _, l := range h.Lines {
273294 content := strings.TrimSuffix(l.Content, "\n")
295+ content = ansi.Truncate(content, dv.codeWidth, "…")
274296
275297 switch l.Kind {
276298 case udiff.Equal:
@@ -313,7 +335,8 @@ func (dv *DiffView) renderSplit() string {
313335 if dv.lineNumbers {
314336 b.WriteString(dv.style.DividerLine.LineNumber.Render(pad("…", dv.beforeNumDigits)))
315337 }
316− b.WriteString(dv.style.DividerLine.Code.Width(dv.fullCodeWidth).Render(dv.hunkLineFor(dv.unified.Hunks[i])))
338+ content := ansi.Truncate(dv.hunkLineFor(dv.unified.Hunks[i]), dv.fullCodeWidth, "…")
339+ b.WriteString(dv.style.DividerLine.Code.Width(dv.fullCodeWidth).Render(content))
317340 if dv.lineNumbers {
318341 b.WriteString(dv.style.DividerLine.LineNumber.Render(pad("…", dv.afterNumDigits)))
319342 }
@@ -328,9 +351,11 @@ func (dv *DiffView) renderSplit() string {
328351 var afterContent string
329352 if l.before != nil {
330353 beforeContent = strings.TrimSuffix(l.before.Content, "\n")
354+ beforeContent = ansi.Truncate(beforeContent, dv.codeWidth, "…")
331355 }
332356 if l.after != nil {
333357 afterContent = strings.TrimSuffix(l.after.Content, "\n")
358+ afterContent = ansi.Truncate(afterContent, dv.codeWidth, "…")
334359 }
335360
336361 switch {