@@ -42,6 +42,7 @@ type DiffView struct {
42 xOffset int
42 xOffset int
43 yOffset int
43 yOffset int
44 style Style
44 style Style
46 isComputed bool
47 isComputed bool
@@ -63,6 +64,7 @@ func New() *DiffView {
63 layout: layoutUnified,
64 layout: layoutUnified,
64 contextLines: udiff.DefaultContextLines,
65 contextLines: udiff.DefaultContextLines,
65 lineNumbers: true,
66 lineNumbers: true,
67 if lipgloss.HasDarkBackground(os.Stdin, os.Stdout) {
69 if lipgloss.HasDarkBackground(os.Stdin, os.Stdout) {
68 dv.style = DefaultDarkStyle
70 dv.style = DefaultDarkStyle
@@ -144,8 +146,16 @@ func (dv *DiffView) YOffset(yOffset int) *DiffView {
144 return dv
146 return dv
149+// TabWidth sets the tab width. Only relevant for code that contains tabs, like
151+func (dv *DiffView) TabWidth(tabWidth int) *DiffView {
152+ dv.tabWidth = tabWidth
147// String returns the string representation of the DiffView.
156// String returns the string representation of the DiffView.
148func (dv *DiffView) String() string {
157func (dv *DiffView) String() string {
149 if err := dv.computeDiff(); err != nil {
159 if err := dv.computeDiff(); err != nil {
150 return err.Error()
160 return err.Error()
@@ -177,6 +187,14 @@ func (dv *DiffView) String() string {
190+// replaceTabs replaces tabs in the before and after file contents with spaces
191+// according to the specified tab width.
192+func (dv *DiffView) replaceTabs() {
193+ spaces := strings.Repeat(" ", dv.tabWidth)
194+ dv.before.content = strings.ReplaceAll(dv.before.content, "\t", spaces)
195+ dv.after.content = strings.ReplaceAll(dv.after.content, "\t", spaces)
180// computeDiff computes the differences between the "before" and "after" files.
198// computeDiff computes the differences between the "before" and "after" files.
181func (dv *DiffView) computeDiff() error {
199func (dv *DiffView) computeDiff() error {
182 if dv.isComputed {
200 if dv.isComputed {