99 "charm.land/lipgloss/v2/tree"
1010 "github.com/bluekeyes/go-gitdiff/gitdiff"
1111
12+ "github.com/dlvhdr/diffnav/pkg/config"
1213 "github.com/dlvhdr/diffnav/pkg/icons"
1314 "github.com/dlvhdr/diffnav/pkg/utils"
1415)
@@ -24,13 +25,12 @@ const (
2425)
2526
2627type FileNode struct {
27- File * gitdiff.File
28- Depth int
29- YOffset int
30- IconStyle string
31- Selected bool
32- ColorFileNames bool
33- PanelWidth int
28+ File * gitdiff.File
29+ Depth int
30+ YOffset int
31+ Selected bool
32+ PanelWidth int
33+ Cfg config.Config
3434}
3535
3636func (f * FileNode ) Path () string {
@@ -41,7 +41,7 @@ func (f *FileNode) Value() string {
4141 name := filepath .Base (f .Path ())
4242
4343 // full has a special layout: [status icon] [filename] [file-type icon]
44- if f .IconStyle == IconsNerdFull {
44+ if f .Cfg . UI . Icons == IconsNerdFull {
4545 return utils .RemoveReset (f .renderFullLayout (name ))
4646 }
4747
@@ -54,7 +54,13 @@ func (f *FileNode) Value() string {
5454func (f * FileNode ) renderStandardLayout (name string ) string {
5555 icon := f .getIcon () + " "
5656 iconWidth := lipgloss .Width (icon ) + 1
57- nameMaxWidth := f .PanelWidth - f .Depth - iconWidth
57+
58+ stats := ""
59+ if f .Cfg .UI .ShowDiffStats {
60+ stats = " " + ViewFileDiffStats (f .File , lipgloss .NewStyle ())
61+ }
62+
63+ nameMaxWidth := f .PanelWidth - f .Depth - iconWidth - lipgloss .Width (stats )
5864 truncatedName := utils .TruncateString (name , nameMaxWidth )
5965 coloredIcon := lipgloss .NewStyle ().Foreground (f .StatusColor ()).Render (icon )
6066
@@ -68,15 +74,15 @@ func (f *FileNode) renderStandardLayout(name string) string {
6874 bgStyle = bgStyle .Width (availableWidth )
6975 }
7076 }
71- return coloredIcon + bgStyle .Render (truncatedName )
77+ return coloredIcon + bgStyle .Render (truncatedName ) + stats
7278 }
7379
74- if f .ColorFileNames {
80+ if f .Cfg . UI . ColorFileNames {
7581 styledName := lipgloss .NewStyle ().Foreground (f .StatusColor ()).Render (truncatedName )
76- return coloredIcon + styledName
82+ return coloredIcon + styledName + stats
7783 }
7884
79- return coloredIcon + truncatedName
85+ return coloredIcon + truncatedName + stats
8086}
8187
8288// renderFullLayout renders: [status icon colored] [file-type icon colored] [filename]
@@ -86,10 +92,15 @@ func (f *FileNode) renderFullLayout(name string) string {
8692 fileIcon := icons .GetIcon (name , false )
8793 style := lipgloss .NewStyle ().Foreground (f .StatusColor ())
8894
95+ stats := ""
96+ if f .Cfg .UI .ShowDiffStats {
97+ stats = " " + ViewFileDiffStats (f .File , lipgloss .NewStyle ())
98+ }
99+
89100 iconsPrefix := style .Render (statusIcon ) + " " + style .Render (fileIcon ) + " "
90101 iconsWidth := lipgloss .Width (statusIcon ) + 1 + lipgloss .Width (fileIcon ) + 1
91102
92- nameMaxWidth := f .PanelWidth - f .Depth - iconsWidth
103+ nameMaxWidth := f .PanelWidth - f .Depth - iconsWidth - lipgloss . Width ( stats )
93104 truncatedName := utils .TruncateString (name , nameMaxWidth )
94105
95106 if f .Selected {
@@ -99,19 +110,19 @@ func (f *FileNode) renderFullLayout(name string) string {
99110 bgStyle = bgStyle .Width (w )
100111 }
101112 }
102- return iconsPrefix + bgStyle .Render (truncatedName )
113+ return iconsPrefix + bgStyle .Render (truncatedName ) + stats
103114 }
104115
105- if f .ColorFileNames {
106- return iconsPrefix + style .Render (truncatedName )
116+ if f .Cfg . UI . ColorFileNames {
117+ return iconsPrefix + style .Render (truncatedName ) + stats
107118 }
108- return iconsPrefix + lipgloss .NewStyle ().Foreground (lipgloss .Color ("15" )).Render (truncatedName )
119+ return iconsPrefix + lipgloss .NewStyle ().Foreground (lipgloss .Color ("15" )).Render (truncatedName ) + stats
109120}
110121
111122// getIcon returns the left icon based on the icon style.
112123func (f * FileNode ) getIcon () string {
113124 name := filepath .Base (f .Path ())
114- switch f .IconStyle {
125+ switch f .Cfg . UI . Icons {
115126 case IconsNerdStatus :
116127 if f .File .IsNew {
117128 return ""
@@ -177,7 +188,7 @@ func (f *FileNode) SetHidden(bool) {}
177188
178189func (f * FileNode ) SetValue (any ) {}
179190
180- func LinesCounts (file * gitdiff.File ) (int64 , int64 ) {
191+ func DiffStats (file * gitdiff.File ) (int64 , int64 ) {
181192 if file == nil {
182193 return 0 , 0
183194 }
@@ -191,18 +202,29 @@ func LinesCounts(file *gitdiff.File) (int64, int64) {
191202 return added , deleted
192203}
193204
194- func ViewLinesCounts (added , deleted int64 , base lipgloss.Style ) string {
195- return lipgloss .JoinHorizontal (
196- lipgloss .Top ,
197- base .Foreground (lipgloss .Green ).Render (fmt .Sprintf ("+%d " , added )),
198- base .Foreground (lipgloss .Red ).Render (fmt .Sprintf ("-%d" , deleted )),
199- )
205+ func ViewDiffStats (added , deleted int64 , base lipgloss.Style ) string {
206+ addedView := ""
207+ deletedView := ""
208+
209+ if added > 0 {
210+ addedView = base .Foreground (lipgloss .Green ).Render (fmt .Sprintf ("+%d" , added ))
211+ }
212+
213+ if added > 0 && deleted > 0 {
214+ addedView += " "
215+ }
216+
217+ if deleted > 0 {
218+ deletedView = base .Foreground (lipgloss .Red ).Render (fmt .Sprintf ("-%d" , deleted ))
219+ }
220+
221+ return lipgloss .JoinHorizontal (lipgloss .Top , addedView , deletedView )
200222}
201223
202- func ViewFileLinesCounts (file * gitdiff.File , base lipgloss.Style ) string {
203- added , deleted := LinesCounts (file )
224+ func ViewFileDiffStats (file * gitdiff.File , base lipgloss.Style ) string {
225+ added , deleted := DiffStats (file )
204226
205- return ViewLinesCounts (added , deleted , base )
227+ return ViewDiffStats (added , deleted , base )
206228}
207229
208230func GetFileName (file * gitdiff.File ) string {
0 commit comments