refactor(diffview): move padding out of `styles.go`

Andrey Nering created

Change summary

internal/exp/diffview/diffview.go | 18 ++++++++++++++
internal/exp/diffview/style.go    | 38 ++++++++------------------------
2 files changed, 27 insertions(+), 29 deletions(-)

Detailed changes

internal/exp/diffview/diffview.go 🔗

@@ -12,7 +12,10 @@ import (
 	"github.com/charmbracelet/x/ansi"
 )
 
-const leadingSymbolsSize = 2
+const (
+	leadingSymbolsSize = 2
+	lineNumPadding     = 1
+)
 
 type file struct {
 	path    string
@@ -132,6 +135,7 @@ func (dv *DiffView) String() string {
 		return err.Error()
 	}
 	dv.convertDiffToSplit()
+	dv.adjustStyles()
 	dv.detectNumDigits()
 	dv.detectCodeWidth()
 
@@ -145,6 +149,18 @@ func (dv *DiffView) String() string {
 	}
 }
 
+func (dv *DiffView) adjustStyles() {
+	dv.style.MissingLine.LineNumber = setPadding(dv.style.MissingLine.LineNumber)
+	dv.style.DividerLine.LineNumber = setPadding(dv.style.DividerLine.LineNumber)
+	dv.style.EqualLine.LineNumber = setPadding(dv.style.EqualLine.LineNumber)
+	dv.style.InsertLine.LineNumber = setPadding(dv.style.InsertLine.LineNumber)
+	dv.style.DeleteLine.LineNumber = setPadding(dv.style.DeleteLine.LineNumber)
+}
+
+func setPadding(s lipgloss.Style) lipgloss.Style {
+	return s.Padding(0, lineNumPadding).Align(lipgloss.Right)
+}
+
 // renderUnified renders the unified diff view as a string.
 func (dv *DiffView) renderUnified() string {
 	var b strings.Builder

internal/exp/diffview/style.go 🔗

@@ -23,26 +23,21 @@ var DefaultLightStyle = Style{
 	DividerLine: LineStyle{
 		LineNumber: lipgloss.NewStyle().
 			Foreground(charmtone.Iron).
-			Background(charmtone.Thunder).
-			Align(lipgloss.Right).
-			Padding(0, 1),
+			Background(charmtone.Thunder),
 		Code: lipgloss.NewStyle().
 			Foreground(charmtone.Oyster).
 			Background(charmtone.Anchovy),
 	},
 	MissingLine: LineStyle{
 		LineNumber: lipgloss.NewStyle().
-			Background(charmtone.Ash).
-			Padding(0, 1),
+			Background(charmtone.Ash),
 		Code: lipgloss.NewStyle().
 			Background(charmtone.Ash),
 	},
 	EqualLine: LineStyle{
 		LineNumber: lipgloss.NewStyle().
 			Foreground(charmtone.Charcoal).
-			Background(charmtone.Ash).
-			Align(lipgloss.Right).
-			Padding(0, 1),
+			Background(charmtone.Ash),
 		Code: lipgloss.NewStyle().
 			Foreground(charmtone.Pepper).
 			Background(charmtone.Salt),
@@ -50,9 +45,7 @@ var DefaultLightStyle = Style{
 	InsertLine: LineStyle{
 		LineNumber: lipgloss.NewStyle().
 			Foreground(charmtone.Turtle).
-			Background(lipgloss.Color("#c8e6c9")).
-			Align(lipgloss.Right).
-			Padding(0, 1),
+			Background(lipgloss.Color("#c8e6c9")),
 		Symbol: lipgloss.NewStyle().
 			Foreground(charmtone.Turtle).
 			Background(lipgloss.Color("#e8f5e9")),
@@ -63,9 +56,7 @@ var DefaultLightStyle = Style{
 	DeleteLine: LineStyle{
 		LineNumber: lipgloss.NewStyle().
 			Foreground(charmtone.Cherry).
-			Background(lipgloss.Color("#ffcdd2")).
-			Align(lipgloss.Left).
-			Padding(0, 1),
+			Background(lipgloss.Color("#ffcdd2")),
 		Symbol: lipgloss.NewStyle().
 			Foreground(charmtone.Cherry).
 			Background(lipgloss.Color("#ffebee")),
@@ -79,26 +70,21 @@ var DefaultDarkStyle = Style{
 	DividerLine: LineStyle{
 		LineNumber: lipgloss.NewStyle().
 			Foreground(charmtone.Smoke).
-			Background(charmtone.Sapphire).
-			Align(lipgloss.Right).
-			Padding(0, 1),
+			Background(charmtone.Sapphire),
 		Code: lipgloss.NewStyle().
 			Foreground(charmtone.Smoke).
 			Background(charmtone.Ox),
 	},
 	MissingLine: LineStyle{
 		LineNumber: lipgloss.NewStyle().
-			Background(charmtone.Charcoal).
-			Padding(0, 1),
+			Background(charmtone.Charcoal),
 		Code: lipgloss.NewStyle().
 			Background(charmtone.Charcoal),
 	},
 	EqualLine: LineStyle{
 		LineNumber: lipgloss.NewStyle().
 			Foreground(charmtone.Ash).
-			Background(charmtone.Charcoal).
-			Align(lipgloss.Right).
-			Padding(0, 1),
+			Background(charmtone.Charcoal),
 		Code: lipgloss.NewStyle().
 			Foreground(charmtone.Salt).
 			Background(charmtone.Pepper),
@@ -106,9 +92,7 @@ var DefaultDarkStyle = Style{
 	InsertLine: LineStyle{
 		LineNumber: lipgloss.NewStyle().
 			Foreground(charmtone.Turtle).
-			Background(lipgloss.Color("#293229")).
-			Align(lipgloss.Right).
-			Padding(0, 1),
+			Background(lipgloss.Color("#293229")),
 		Symbol: lipgloss.NewStyle().
 			Foreground(charmtone.Turtle).
 			Background(lipgloss.Color("#303a30")),
@@ -119,9 +103,7 @@ var DefaultDarkStyle = Style{
 	DeleteLine: LineStyle{
 		LineNumber: lipgloss.NewStyle().
 			Foreground(charmtone.Cherry).
-			Background(lipgloss.Color("#332929")).
-			Align(lipgloss.Left).
-			Padding(0, 1),
+			Background(lipgloss.Color("#332929")),
 		Symbol: lipgloss.NewStyle().
 			Foreground(charmtone.Cherry).
 			Background(lipgloss.Color("#3a3030")),