on main help sign in
refactor(diffview): convert styles into a reusable `LineStyle` type
commitf8fc11edaccfcbde37bce5a9cd259b156f18dffb authorAndrey Nering <andreynering@users.noreply.github.com> dateWed 4 Jun 2025, 16:35 (1 year ago) parentb7ab1a8
1 file changed+55−40
internal/exp/diffview/diffview.go+55−40
internal/exp/diffview/diffview.go+55−40
@@ -25,48 +25,63 @@ const (
2525 layoutSplit
2626)
2727
28+type LineStyle struct {
29+ Symbol lipgloss.Style
30+ Code lipgloss.Style
31+}
32+
2833type Style struct {
29− Base lipgloss.Style
30− InsertLine lipgloss.Style
31− InsertSymbols lipgloss.Style
32− DeleteLine lipgloss.Style
33− DeleteSymbols lipgloss.Style
34+ EqualLine LineStyle
35+ InsertLine LineStyle
36+ DeleteLine LineStyle
3437}
3538
3639var DefaultLightStyle = Style{
37− Base: lipgloss.NewStyle().
38− Foreground(charmtone.Pepper).
39− Background(charmtone.Salt),
40− InsertLine: lipgloss.NewStyle().
41− Foreground(charmtone.Pepper).
42− Background(lipgloss.Color("#e8f5e9")),
43− InsertSymbols: lipgloss.NewStyle().
44− Foreground(charmtone.Turtle).
45− Background(lipgloss.Color("#e8f5e9")),
46− DeleteLine: lipgloss.NewStyle().
47− Foreground(charmtone.Pepper).
48− Background(lipgloss.Color("#ffebee")),
49− DeleteSymbols: lipgloss.NewStyle().
50− Foreground(charmtone.Cherry).
51− Background(lipgloss.Color("#ffebee")),
40+ EqualLine: LineStyle{
41+ Code: lipgloss.NewStyle().
42+ Foreground(charmtone.Pepper).
43+ Background(charmtone.Salt),
44+ },
45+ InsertLine: LineStyle{
46+ Symbol: lipgloss.NewStyle().
47+ Foreground(charmtone.Turtle).
48+ Background(lipgloss.Color("#e8f5e9")),
49+ Code: lipgloss.NewStyle().
50+ Foreground(charmtone.Pepper).
51+ Background(lipgloss.Color("#e8f5e9")),
52+ },
53+ DeleteLine: LineStyle{
54+ Symbol: lipgloss.NewStyle().
55+ Foreground(charmtone.Cherry).
56+ Background(lipgloss.Color("#ffebee")),
57+ Code: lipgloss.NewStyle().
58+ Foreground(charmtone.Pepper).
59+ Background(lipgloss.Color("#ffebee")),
60+ },
5261}
5362
5463var DefaultDarkStyle = Style{
55− Base: lipgloss.NewStyle().
56− Foreground(charmtone.Salt).
57− Background(charmtone.Pepper),
58− InsertLine: lipgloss.NewStyle().
59− Foreground(charmtone.Salt).
60− Background(lipgloss.Color("#303a30")),
61− InsertSymbols: lipgloss.NewStyle().
62− Foreground(charmtone.Turtle).
63− Background(lipgloss.Color("#303a30")),
64− DeleteLine: lipgloss.NewStyle().
65− Foreground(charmtone.Salt).
66− Background(lipgloss.Color("#3a3030")),
67− DeleteSymbols: lipgloss.NewStyle().
68− Foreground(charmtone.Cherry).
69− Background(lipgloss.Color("#3a3030")),
64+ EqualLine: LineStyle{
65+ Code: lipgloss.NewStyle().
66+ Foreground(charmtone.Salt).
67+ Background(charmtone.Pepper),
68+ },
69+ InsertLine: LineStyle{
70+ Symbol: lipgloss.NewStyle().
71+ Foreground(charmtone.Turtle).
72+ Background(lipgloss.Color("#303a30")),
73+ Code: lipgloss.NewStyle().
74+ Foreground(charmtone.Salt).
75+ Background(lipgloss.Color("#303a30")),
76+ },
77+ DeleteLine: LineStyle{
78+ Symbol: lipgloss.NewStyle().
79+ Foreground(charmtone.Cherry).
80+ Background(lipgloss.Color("#3a3030")),
81+ Code: lipgloss.NewStyle().
82+ Foreground(charmtone.Salt).
83+ Background(lipgloss.Color("#3a3030")),
84+ },
7085}
7186
7287// DiffView represents a view for displaying differences between two files.
@@ -170,13 +185,13 @@ func (dv *DiffView) String() string {
170185
171186 switch l.Kind {
172187 case udiff.Insert:
173− b.WriteString(dv.style.InsertSymbols.Render("+ "))
174− b.WriteString(dv.style.InsertLine.Width(width).Render(content))
188+ b.WriteString(dv.style.InsertLine.Symbol.Render("+ "))
189+ b.WriteString(dv.style.InsertLine.Code.Width(width).Render(content))
175190 case udiff.Delete:
176− b.WriteString(dv.style.DeleteSymbols.Render("- "))
177− b.WriteString(dv.style.DeleteLine.Width(width).Render(content))
191+ b.WriteString(dv.style.DeleteLine.Symbol.Render("- "))
192+ b.WriteString(dv.style.DeleteLine.Code.Width(width).Render(content))
178193 case udiff.Equal:
179− b.WriteString(dv.style.Base.Width(width + leadingSymbolsSize).Render(" " + content))
194+ b.WriteString(dv.style.EqualLine.Code.Width(width + leadingSymbolsSize).Render(" " + content))
180195 }
181196 b.WriteRune('\n')
182197 }