refactor(diffview): wrap default styles into functions

Andrey Nering and Christian Rocha created

Co-authored-by: Christian Rocha <christian@rocha.is>

Change summary

internal/exp/diffview/diffview.go      |   2 
internal/exp/diffview/diffview_test.go |  14 +-
internal/exp/diffview/style.go         | 184 ++++++++++++++-------------
3 files changed, 102 insertions(+), 98 deletions(-)

Detailed changes

internal/exp/diffview/diffview.go 🔗

@@ -69,7 +69,7 @@ func New() *DiffView {
 		lineNumbers:  true,
 		tabWidth:     8,
 	}
-	dv.style = DefaultDarkStyle
+	dv.style = DefaultDarkStyle()
 	return dv
 }
 

internal/exp/diffview/diffview_test.go 🔗

@@ -97,12 +97,12 @@ var (
 
 	LightModeFunc = func(dv *diffview.DiffView) *diffview.DiffView {
 		return dv.
-			Style(diffview.DefaultLightStyle).
+			Style(diffview.DefaultLightStyle()).
 			ChromaStyle(styles.Get("catppuccin-latte"))
 	}
 	DarkModeFunc = func(dv *diffview.DiffView) *diffview.DiffView {
 		return dv.
-			Style(diffview.DefaultDarkStyle).
+			Style(diffview.DefaultDarkStyle()).
 			ChromaStyle(styles.Get("catppuccin-macchiato"))
 	}
 
@@ -165,7 +165,7 @@ func TestDiffViewTabs(t *testing.T) {
 			dv := diffview.New().
 				Before("main.go", TestTabsBefore).
 				After("main.go", TestTabsAfter).
-				Style(diffview.DefaultLightStyle).
+				Style(diffview.DefaultLightStyle()).
 				ChromaStyle(styles.Get("catppuccin-latte"))
 			dv = layoutFunc(dv)
 
@@ -190,7 +190,7 @@ func TestDiffViewWidth(t *testing.T) {
 						Before("main.go", TestMultipleHunksBefore).
 						After("main.go", TestMultipleHunksAfter).
 						Width(width).
-						Style(diffview.DefaultLightStyle).
+						Style(diffview.DefaultLightStyle()).
 						ChromaStyle(styles.Get("catppuccin-latte"))
 					dv = layoutFunc(dv)
 
@@ -215,7 +215,7 @@ func TestDiffViewHeight(t *testing.T) {
 						Before("main.go", TestMultipleHunksBefore).
 						After("main.go", TestMultipleHunksAfter).
 						Height(height).
-						Style(diffview.DefaultLightStyle).
+						Style(diffview.DefaultLightStyle()).
 						ChromaStyle(styles.Get("catppuccin-latte"))
 					dv = layoutFunc(dv)
 
@@ -239,7 +239,7 @@ func TestDiffViewXOffset(t *testing.T) {
 					dv := diffview.New().
 						Before("main.go", TestDefaultBefore).
 						After("main.go", TestDefaultAfter).
-						Style(diffview.DefaultLightStyle).
+						Style(diffview.DefaultLightStyle()).
 						ChromaStyle(styles.Get("catppuccin-latte")).
 						Width(60).
 						XOffset(xOffset)
@@ -265,7 +265,7 @@ func TestDiffViewYOffset(t *testing.T) {
 					dv := diffview.New().
 						Before("main.go", TestMultipleHunksBefore).
 						After("main.go", TestMultipleHunksAfter).
-						Style(diffview.DefaultLightStyle).
+						Style(diffview.DefaultLightStyle()).
 						ChromaStyle(styles.Get("catppuccin-latte")).
 						Height(5).
 						YOffset(yOffset)

internal/exp/diffview/style.go 🔗

@@ -24,97 +24,101 @@ type Style struct {
 }
 
 // DefaultLightStyle provides a default light theme style for the diff view.
-var DefaultLightStyle = Style{
-	DividerLine: LineStyle{
-		LineNumber: lipgloss.NewStyle().
-			Foreground(charmtone.Iron).
-			Background(charmtone.Thunder),
-		Code: lipgloss.NewStyle().
-			Foreground(charmtone.Oyster).
-			Background(charmtone.Anchovy),
-	},
-	MissingLine: LineStyle{
-		LineNumber: lipgloss.NewStyle().
-			Background(charmtone.Ash),
-		Code: lipgloss.NewStyle().
-			Background(charmtone.Ash),
-	},
-	EqualLine: LineStyle{
-		LineNumber: lipgloss.NewStyle().
-			Foreground(charmtone.Charcoal).
-			Background(charmtone.Ash),
-		Code: lipgloss.NewStyle().
-			Foreground(charmtone.Pepper).
-			Background(charmtone.Salt),
-	},
-	InsertLine: LineStyle{
-		LineNumber: lipgloss.NewStyle().
-			Foreground(charmtone.Turtle).
-			Background(lipgloss.Color("#c8e6c9")),
-		Symbol: lipgloss.NewStyle().
-			Foreground(charmtone.Turtle).
-			Background(lipgloss.Color("#e8f5e9")),
-		Code: lipgloss.NewStyle().
-			Foreground(charmtone.Pepper).
-			Background(lipgloss.Color("#e8f5e9")),
-	},
-	DeleteLine: LineStyle{
-		LineNumber: lipgloss.NewStyle().
-			Foreground(charmtone.Cherry).
-			Background(lipgloss.Color("#ffcdd2")),
-		Symbol: lipgloss.NewStyle().
-			Foreground(charmtone.Cherry).
-			Background(lipgloss.Color("#ffebee")),
-		Code: lipgloss.NewStyle().
-			Foreground(charmtone.Pepper).
-			Background(lipgloss.Color("#ffebee")),
-	},
+func DefaultLightStyle() Style {
+	return Style{
+		DividerLine: LineStyle{
+			LineNumber: lipgloss.NewStyle().
+				Foreground(charmtone.Iron).
+				Background(charmtone.Thunder),
+			Code: lipgloss.NewStyle().
+				Foreground(charmtone.Oyster).
+				Background(charmtone.Anchovy),
+		},
+		MissingLine: LineStyle{
+			LineNumber: lipgloss.NewStyle().
+				Background(charmtone.Ash),
+			Code: lipgloss.NewStyle().
+				Background(charmtone.Ash),
+		},
+		EqualLine: LineStyle{
+			LineNumber: lipgloss.NewStyle().
+				Foreground(charmtone.Charcoal).
+				Background(charmtone.Ash),
+			Code: lipgloss.NewStyle().
+				Foreground(charmtone.Pepper).
+				Background(charmtone.Salt),
+		},
+		InsertLine: LineStyle{
+			LineNumber: lipgloss.NewStyle().
+				Foreground(charmtone.Turtle).
+				Background(lipgloss.Color("#c8e6c9")),
+			Symbol: lipgloss.NewStyle().
+				Foreground(charmtone.Turtle).
+				Background(lipgloss.Color("#e8f5e9")),
+			Code: lipgloss.NewStyle().
+				Foreground(charmtone.Pepper).
+				Background(lipgloss.Color("#e8f5e9")),
+		},
+		DeleteLine: LineStyle{
+			LineNumber: lipgloss.NewStyle().
+				Foreground(charmtone.Cherry).
+				Background(lipgloss.Color("#ffcdd2")),
+			Symbol: lipgloss.NewStyle().
+				Foreground(charmtone.Cherry).
+				Background(lipgloss.Color("#ffebee")),
+			Code: lipgloss.NewStyle().
+				Foreground(charmtone.Pepper).
+				Background(lipgloss.Color("#ffebee")),
+		},
+	}
 }
 
 // DefaultDarkStyle provides a default dark theme style for the diff view.
-var DefaultDarkStyle = Style{
-	DividerLine: LineStyle{
-		LineNumber: lipgloss.NewStyle().
-			Foreground(charmtone.Smoke).
-			Background(charmtone.Sapphire),
-		Code: lipgloss.NewStyle().
-			Foreground(charmtone.Smoke).
-			Background(charmtone.Ox),
-	},
-	MissingLine: LineStyle{
-		LineNumber: lipgloss.NewStyle().
-			Background(charmtone.Charcoal),
-		Code: lipgloss.NewStyle().
-			Background(charmtone.Charcoal),
-	},
-	EqualLine: LineStyle{
-		LineNumber: lipgloss.NewStyle().
-			Foreground(charmtone.Ash).
-			Background(charmtone.Charcoal),
-		Code: lipgloss.NewStyle().
-			Foreground(charmtone.Salt).
-			Background(charmtone.Pepper),
-	},
-	InsertLine: LineStyle{
-		LineNumber: lipgloss.NewStyle().
-			Foreground(charmtone.Turtle).
-			Background(lipgloss.Color("#293229")),
-		Symbol: lipgloss.NewStyle().
-			Foreground(charmtone.Turtle).
-			Background(lipgloss.Color("#303a30")),
-		Code: lipgloss.NewStyle().
-			Foreground(charmtone.Salt).
-			Background(lipgloss.Color("#303a30")),
-	},
-	DeleteLine: LineStyle{
-		LineNumber: lipgloss.NewStyle().
-			Foreground(charmtone.Cherry).
-			Background(lipgloss.Color("#332929")),
-		Symbol: lipgloss.NewStyle().
-			Foreground(charmtone.Cherry).
-			Background(lipgloss.Color("#3a3030")),
-		Code: lipgloss.NewStyle().
-			Foreground(charmtone.Salt).
-			Background(lipgloss.Color("#3a3030")),
-	},
+func DefaultDarkStyle() Style {
+	return Style{
+		DividerLine: LineStyle{
+			LineNumber: lipgloss.NewStyle().
+				Foreground(charmtone.Smoke).
+				Background(charmtone.Sapphire),
+			Code: lipgloss.NewStyle().
+				Foreground(charmtone.Smoke).
+				Background(charmtone.Ox),
+		},
+		MissingLine: LineStyle{
+			LineNumber: lipgloss.NewStyle().
+				Background(charmtone.Charcoal),
+			Code: lipgloss.NewStyle().
+				Background(charmtone.Charcoal),
+		},
+		EqualLine: LineStyle{
+			LineNumber: lipgloss.NewStyle().
+				Foreground(charmtone.Ash).
+				Background(charmtone.Charcoal),
+			Code: lipgloss.NewStyle().
+				Foreground(charmtone.Salt).
+				Background(charmtone.Pepper),
+		},
+		InsertLine: LineStyle{
+			LineNumber: lipgloss.NewStyle().
+				Foreground(charmtone.Turtle).
+				Background(lipgloss.Color("#293229")),
+			Symbol: lipgloss.NewStyle().
+				Foreground(charmtone.Turtle).
+				Background(lipgloss.Color("#303a30")),
+			Code: lipgloss.NewStyle().
+				Foreground(charmtone.Salt).
+				Background(lipgloss.Color("#303a30")),
+		},
+		DeleteLine: LineStyle{
+			LineNumber: lipgloss.NewStyle().
+				Foreground(charmtone.Cherry).
+				Background(lipgloss.Color("#332929")),
+			Symbol: lipgloss.NewStyle().
+				Foreground(charmtone.Cherry).
+				Background(lipgloss.Color("#3a3030")),
+			Code: lipgloss.NewStyle().
+				Foreground(charmtone.Salt).
+				Background(lipgloss.Color("#3a3030")),
+		},
+	}
 }