on main help sign in
refactor(diffview): move padding out of `styles.go`
commitbbc3122d841a97fc3c6a69ac79889946d9f231a6 authorAndrey Nering <andreynering@users.noreply.github.com> dateMon 9 Jun 2025, 17:13 (1 year ago) parent864735a
2 files changed+27−29
internal/exp/diffview/diffview.go+17−1 internal/exp/diffview/style.go+10−28
internal/exp/diffview/diffview.go+17−1
@@ -12,7 +12,10 @@ import (
1212 "github.com/charmbracelet/x/ansi"
1313)
1414
15−const leadingSymbolsSize = 2
15+const (
16+ leadingSymbolsSize = 2
17+ lineNumPadding = 1
18+)
1619
1720type file struct {
1821 path string
@@ -132,6 +135,7 @@ func (dv *DiffView) String() string {
132135 return err.Error()
133136 }
134137 dv.convertDiffToSplit()
138+ dv.adjustStyles()
135139 dv.detectNumDigits()
136140 dv.detectCodeWidth()
137141
@@ -145,6 +149,18 @@ func (dv *DiffView) String() string {
145149 }
146150}
147151
152+func (dv *DiffView) adjustStyles() {
153+ dv.style.MissingLine.LineNumber = setPadding(dv.style.MissingLine.LineNumber)
154+ dv.style.DividerLine.LineNumber = setPadding(dv.style.DividerLine.LineNumber)
155+ dv.style.EqualLine.LineNumber = setPadding(dv.style.EqualLine.LineNumber)
156+ dv.style.InsertLine.LineNumber = setPadding(dv.style.InsertLine.LineNumber)
157+ dv.style.DeleteLine.LineNumber = setPadding(dv.style.DeleteLine.LineNumber)
158+}
159+
160+func setPadding(s lipgloss.Style) lipgloss.Style {
161+ return s.Padding(0, lineNumPadding).Align(lipgloss.Right)
162+}
163+
148164// renderUnified renders the unified diff view as a string.
149165func (dv *DiffView) renderUnified() string {
150166 var b strings.Builder
internal/exp/diffview/style.go+10−28
@@ -23,26 +23,21 @@ var DefaultLightStyle = Style{
2323 DividerLine: LineStyle{
2424 LineNumber: lipgloss.NewStyle().
2525 Foreground(charmtone.Iron).
26− Background(charmtone.Thunder).
27− Align(lipgloss.Right).
28− Padding(0, 1),
26+ Background(charmtone.Thunder),
2927 Code: lipgloss.NewStyle().
3028 Foreground(charmtone.Oyster).
3129 Background(charmtone.Anchovy),
3230 },
3331 MissingLine: LineStyle{
3432 LineNumber: lipgloss.NewStyle().
35− Background(charmtone.Ash).
36− Padding(0, 1),
33+ Background(charmtone.Ash),
3734 Code: lipgloss.NewStyle().
3835 Background(charmtone.Ash),
3936 },
4037 EqualLine: LineStyle{
4138 LineNumber: lipgloss.NewStyle().
4239 Foreground(charmtone.Charcoal).
43− Background(charmtone.Ash).
44− Align(lipgloss.Right).
45− Padding(0, 1),
40+ Background(charmtone.Ash),
4641 Code: lipgloss.NewStyle().
4742 Foreground(charmtone.Pepper).
4843 Background(charmtone.Salt),
@@ -50,9 +45,7 @@ var DefaultLightStyle = Style{
5045 InsertLine: LineStyle{
5146 LineNumber: lipgloss.NewStyle().
5247 Foreground(charmtone.Turtle).
53− Background(lipgloss.Color("#c8e6c9")).
54− Align(lipgloss.Right).
55− Padding(0, 1),
48+ Background(lipgloss.Color("#c8e6c9")),
5649 Symbol: lipgloss.NewStyle().
5750 Foreground(charmtone.Turtle).
5851 Background(lipgloss.Color("#e8f5e9")),
@@ -63,9 +56,7 @@ var DefaultLightStyle = Style{
6356 DeleteLine: LineStyle{
6457 LineNumber: lipgloss.NewStyle().
6558 Foreground(charmtone.Cherry).
66− Background(lipgloss.Color("#ffcdd2")).
67− Align(lipgloss.Left).
68− Padding(0, 1),
59+ Background(lipgloss.Color("#ffcdd2")),
6960 Symbol: lipgloss.NewStyle().
7061 Foreground(charmtone.Cherry).
7162 Background(lipgloss.Color("#ffebee")),
@@ -79,26 +70,21 @@ var DefaultDarkStyle = Style{
7970 DividerLine: LineStyle{
8071 LineNumber: lipgloss.NewStyle().
8172 Foreground(charmtone.Smoke).
82− Background(charmtone.Sapphire).
83− Align(lipgloss.Right).
84− Padding(0, 1),
73+ Background(charmtone.Sapphire),
8574 Code: lipgloss.NewStyle().
8675 Foreground(charmtone.Smoke).
8776 Background(charmtone.Ox),
8877 },
8978 MissingLine: LineStyle{
9079 LineNumber: lipgloss.NewStyle().
91− Background(charmtone.Charcoal).
92− Padding(0, 1),
80+ Background(charmtone.Charcoal),
9381 Code: lipgloss.NewStyle().
9482 Background(charmtone.Charcoal),
9583 },
9684 EqualLine: LineStyle{
9785 LineNumber: lipgloss.NewStyle().
9886 Foreground(charmtone.Ash).
99− Background(charmtone.Charcoal).
100− Align(lipgloss.Right).
101− Padding(0, 1),
87+ Background(charmtone.Charcoal),
10288 Code: lipgloss.NewStyle().
10389 Foreground(charmtone.Salt).
10490 Background(charmtone.Pepper),
@@ -106,9 +92,7 @@ var DefaultDarkStyle = Style{
10692 InsertLine: LineStyle{
10793 LineNumber: lipgloss.NewStyle().
10894 Foreground(charmtone.Turtle).
109− Background(lipgloss.Color("#293229")).
110− Align(lipgloss.Right).
111− Padding(0, 1),
95+ Background(lipgloss.Color("#293229")),
11296 Symbol: lipgloss.NewStyle().
11397 Foreground(charmtone.Turtle).
11498 Background(lipgloss.Color("#303a30")),
@@ -119,9 +103,7 @@ var DefaultDarkStyle = Style{
119103 DeleteLine: LineStyle{
120104 LineNumber: lipgloss.NewStyle().
121105 Foreground(charmtone.Cherry).
122− Background(lipgloss.Color("#332929")).
123− Align(lipgloss.Left).
124− Padding(0, 1),
106+ Background(lipgloss.Color("#332929")),
125107 Symbol: lipgloss.NewStyle().
126108 Foreground(charmtone.Cherry).
127109 Background(lipgloss.Color("#3a3030")),