on main help sign in
refactor(diffview): move padding out of `styles.go`
bbc3122Andrey Nering
internal/exp/diffview/diffview.go+17−1
@@ -12,7 +12,10 @@ import (
12 "github.com/charmbracelet/x/ansi"
12 "github.com/charmbracelet/x/ansi"
13)
13)
14
14
15+const (
15−const leadingSymbolsSize = 2
16+ leadingSymbolsSize = 2
17+ lineNumPadding = 1
18+)
16
19
17type file struct {
20type file struct {
18 path string
21 path string
@@ -132,6 +135,7 @@ func (dv *DiffView) String() string {
132 return err.Error()
135 return err.Error()
133 }
136 }
134 dv.convertDiffToSplit()
137 dv.convertDiffToSplit()
138+ dv.adjustStyles()
135 dv.detectNumDigits()
139 dv.detectNumDigits()
136 dv.detectCodeWidth()
140 dv.detectCodeWidth()
137
141
@@ -145,6 +149,18 @@ func (dv *DiffView) String() string {
145 }
149 }
146}
150}
147
151
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+
148// renderUnified renders the unified diff view as a string.
164// renderUnified renders the unified diff view as a string.
149func (dv *DiffView) renderUnified() string {
165func (dv *DiffView) renderUnified() string {
150 var b strings.Builder
166 var b strings.Builder
internal/exp/diffview/style.go+10−28
@@ -23,26 +23,21 @@ var DefaultLightStyle = Style{
23 DividerLine: LineStyle{
23 DividerLine: LineStyle{
24 LineNumber: lipgloss.NewStyle().
24 LineNumber: lipgloss.NewStyle().
25 Foreground(charmtone.Iron).
25 Foreground(charmtone.Iron).
26− Background(charmtone.Thunder).
26+ Background(charmtone.Thunder),
27− Align(lipgloss.Right).
28− Padding(0, 1),
29 Code: lipgloss.NewStyle().
27 Code: lipgloss.NewStyle().
30 Foreground(charmtone.Oyster).
28 Foreground(charmtone.Oyster).
31 Background(charmtone.Anchovy),
29 Background(charmtone.Anchovy),
32 },
30 },
33 MissingLine: LineStyle{
31 MissingLine: LineStyle{
34 LineNumber: lipgloss.NewStyle().
32 LineNumber: lipgloss.NewStyle().
35− Background(charmtone.Ash).
33+ Background(charmtone.Ash),
36− Padding(0, 1),
37 Code: lipgloss.NewStyle().
34 Code: lipgloss.NewStyle().
38 Background(charmtone.Ash),
35 Background(charmtone.Ash),
39 },
36 },
40 EqualLine: LineStyle{
37 EqualLine: LineStyle{
41 LineNumber: lipgloss.NewStyle().
38 LineNumber: lipgloss.NewStyle().
42 Foreground(charmtone.Charcoal).
39 Foreground(charmtone.Charcoal).
43− Background(charmtone.Ash).
40+ Background(charmtone.Ash),
44− Align(lipgloss.Right).
45− Padding(0, 1),
46 Code: lipgloss.NewStyle().
41 Code: lipgloss.NewStyle().
47 Foreground(charmtone.Pepper).
42 Foreground(charmtone.Pepper).
48 Background(charmtone.Salt),
43 Background(charmtone.Salt),
@@ -50,9 +45,7 @@ var DefaultLightStyle = Style{
50 InsertLine: LineStyle{
45 InsertLine: LineStyle{
51 LineNumber: lipgloss.NewStyle().
46 LineNumber: lipgloss.NewStyle().
52 Foreground(charmtone.Turtle).
47 Foreground(charmtone.Turtle).
53− Background(lipgloss.Color("#c8e6c9")).
48+ Background(lipgloss.Color("#c8e6c9")),
54− Align(lipgloss.Right).
55− Padding(0, 1),
56 Symbol: lipgloss.NewStyle().
49 Symbol: lipgloss.NewStyle().
57 Foreground(charmtone.Turtle).
50 Foreground(charmtone.Turtle).
58 Background(lipgloss.Color("#e8f5e9")),
51 Background(lipgloss.Color("#e8f5e9")),
@@ -63,9 +56,7 @@ var DefaultLightStyle = Style{
63 DeleteLine: LineStyle{
56 DeleteLine: LineStyle{
64 LineNumber: lipgloss.NewStyle().
57 LineNumber: lipgloss.NewStyle().
65 Foreground(charmtone.Cherry).
58 Foreground(charmtone.Cherry).
66− Background(lipgloss.Color("#ffcdd2")).
59+ Background(lipgloss.Color("#ffcdd2")),
67− Align(lipgloss.Left).
68− Padding(0, 1),
69 Symbol: lipgloss.NewStyle().
60 Symbol: lipgloss.NewStyle().
70 Foreground(charmtone.Cherry).
61 Foreground(charmtone.Cherry).
71 Background(lipgloss.Color("#ffebee")),
62 Background(lipgloss.Color("#ffebee")),
@@ -79,26 +70,21 @@ var DefaultDarkStyle = Style{
79 DividerLine: LineStyle{
70 DividerLine: LineStyle{
80 LineNumber: lipgloss.NewStyle().
71 LineNumber: lipgloss.NewStyle().
81 Foreground(charmtone.Smoke).
72 Foreground(charmtone.Smoke).
82− Background(charmtone.Sapphire).
73+ Background(charmtone.Sapphire),
83− Align(lipgloss.Right).
84− Padding(0, 1),
85 Code: lipgloss.NewStyle().
74 Code: lipgloss.NewStyle().
86 Foreground(charmtone.Smoke).
75 Foreground(charmtone.Smoke).
87 Background(charmtone.Ox),
76 Background(charmtone.Ox),
88 },
77 },
89 MissingLine: LineStyle{
78 MissingLine: LineStyle{
90 LineNumber: lipgloss.NewStyle().
79 LineNumber: lipgloss.NewStyle().
91− Background(charmtone.Charcoal).
80+ Background(charmtone.Charcoal),
92− Padding(0, 1),
93 Code: lipgloss.NewStyle().
81 Code: lipgloss.NewStyle().
94 Background(charmtone.Charcoal),
82 Background(charmtone.Charcoal),
95 },
83 },
96 EqualLine: LineStyle{
84 EqualLine: LineStyle{
97 LineNumber: lipgloss.NewStyle().
85 LineNumber: lipgloss.NewStyle().
98 Foreground(charmtone.Ash).
86 Foreground(charmtone.Ash).
99− Background(charmtone.Charcoal).
87+ Background(charmtone.Charcoal),
100− Align(lipgloss.Right).
101− Padding(0, 1),
102 Code: lipgloss.NewStyle().
88 Code: lipgloss.NewStyle().
103 Foreground(charmtone.Salt).
89 Foreground(charmtone.Salt).
104 Background(charmtone.Pepper),
90 Background(charmtone.Pepper),
@@ -106,9 +92,7 @@ var DefaultDarkStyle = Style{
106 InsertLine: LineStyle{
92 InsertLine: LineStyle{
107 LineNumber: lipgloss.NewStyle().
93 LineNumber: lipgloss.NewStyle().
108 Foreground(charmtone.Turtle).
94 Foreground(charmtone.Turtle).
109− Background(lipgloss.Color("#293229")).
95+ Background(lipgloss.Color("#293229")),
110− Align(lipgloss.Right).
111− Padding(0, 1),
112 Symbol: lipgloss.NewStyle().
96 Symbol: lipgloss.NewStyle().
113 Foreground(charmtone.Turtle).
97 Foreground(charmtone.Turtle).
114 Background(lipgloss.Color("#303a30")),
98 Background(lipgloss.Color("#303a30")),
@@ -119,9 +103,7 @@ var DefaultDarkStyle = Style{
119 DeleteLine: LineStyle{
103 DeleteLine: LineStyle{
120 LineNumber: lipgloss.NewStyle().
104 LineNumber: lipgloss.NewStyle().
121 Foreground(charmtone.Cherry).
105 Foreground(charmtone.Cherry).
122− Background(lipgloss.Color("#332929")).
106+ Background(lipgloss.Color("#332929")),
123− Align(lipgloss.Left).
124− Padding(0, 1),
125 Symbol: lipgloss.NewStyle().
107 Symbol: lipgloss.NewStyle().
126 Foreground(charmtone.Cherry).
108 Foreground(charmtone.Cherry).
127 Background(lipgloss.Color("#3a3030")),
109 Background(lipgloss.Color("#3a3030")),