@@ -7,39 +7,76 @@ import (
77
88 "github.com/RoseSecurity/terrafetch/internal"
99 "github.com/charmbracelet/lipgloss"
10+ "github.com/mattn/go-runewidth"
1011)
1112
1213var (
1314 borderColor = lipgloss .Color ("99" ) // purple
1415 container = lipgloss .NewStyle ().Border (lipgloss .RoundedBorder ()).BorderForeground (borderColor )
1516
1617 headerStyle = lipgloss .NewStyle ().Bold (true ).Foreground (lipgloss .Color ("63" )) // magenta header
17- keyStyle = lipgloss .NewStyle ().Foreground (lipgloss .Color ("111" )) // cyan keys
18+ keyBase = lipgloss .NewStyle ().Foreground (lipgloss .Color ("111" )) // cyan keys (base style)
1819 valStyle = lipgloss .NewStyle ().Foreground (lipgloss .Color ("15" )) // white values
1920 logoStyle = lipgloss .NewStyle ().Foreground (lipgloss .Color ("99" )) // purple logo
2021)
2122
22- func padKey (k string ) string {
23- return keyStyle .Render (fmt .Sprintf ("%-10s:" , k ))
23+ // padKey dynamically pads and styles a key string based on the provided style
24+ // (whose width is already set to the widest key + 1 for the trailing colon).
25+ func padKey (k string , style lipgloss.Style ) string {
26+ return style .Render (k + ":" )
2427}
2528
2629func RenderInfo (dir string , a internal.Analytics ) string {
30+ // All label strings in the order they'll be rendered.
31+ labels := []string {
32+ "Terraform Files" ,
33+ "Documentation" ,
34+ "Providers" ,
35+ "Module Calls" ,
36+ "Resources" ,
37+ "Data Sources" ,
38+ "Variables" ,
39+ "Sensitive Variables" ,
40+ "Outputs" ,
41+ "Sensitive Outputs" ,
42+ }
43+
44+ // Find the widest label using runewidth (handles double‑width runes correctly).
45+ max := 0
46+ for _ , l := range labels {
47+ if w := runewidth .StringWidth (l ); w > max {
48+ max = w
49+ }
50+ }
51+
52+ // Clone the base key style and set its width dynamically.
53+ keyStyle := keyBase .Width (max + 1 ) // +1 for the trailing colon
54+
2755 tfDir := filepath .Base (dir )
56+
57+ // Helper closure so we don't repeat ourselves.
58+ pk := func (k string ) string { return padKey (k , keyStyle ) }
59+
2860 lines := []string {
2961 headerStyle .Render (tfDir ),
30- headerStyle .Render (strings .Repeat ("-" , len (tfDir ))),
31- fmt .Sprintf ("%s %s" , padKey ("Files" ), valStyle .Render (fmt .Sprint (a .FileCount ))),
32- fmt .Sprintf ("%s %s" , padKey ("Docs" ), valStyle .Render (fmt .Sprint (a .DocCount ))),
33- fmt .Sprintf ("%s %s" , padKey ("Resources" ), valStyle .Render (fmt .Sprint (a .ResourceCount ))),
34- fmt .Sprintf ("%s %s" , padKey ("Modules" ), valStyle .Render (fmt .Sprint (a .ModuleCount ))),
35- fmt .Sprintf ("%s %s" , padKey ("Variables" ), valStyle .Render (fmt .Sprint (a .VariableCount ))),
36- fmt .Sprintf ("%s %s" , padKey ("Outputs" ), valStyle .Render (fmt .Sprint (a .OutputCount ))),
37- fmt .Sprintf ("%s %s" , padKey ("Providers" ), valStyle .Render (fmt .Sprint (a .ProviderCount ))),
62+ headerStyle .Render (strings .Repeat ("-" , runewidth .StringWidth (tfDir ))),
63+ fmt .Sprintf ("%s %s" , pk ("Terraform Files" ), valStyle .Render (fmt .Sprint (a .FileCount ))),
64+ fmt .Sprintf ("%s %s" , pk ("Documentation" ), valStyle .Render (fmt .Sprint (a .DocCount ))),
65+ fmt .Sprintf ("%s %s" , pk ("Providers" ), valStyle .Render (fmt .Sprint (a .ProviderCount ))),
66+ fmt .Sprintf ("%s %s" , pk ("Module Calls" ), valStyle .Render (fmt .Sprint (a .ModuleCount ))),
67+ fmt .Sprintf ("%s %s" , pk ("Resources" ), valStyle .Render (fmt .Sprint (a .ResourceCount ))),
68+ fmt .Sprintf ("%s %s" , pk ("Data Sources" ), valStyle .Render (fmt .Sprint (a .DataSourceCount ))),
69+ fmt .Sprintf ("%s %s" , pk ("Variables" ), valStyle .Render (fmt .Sprint (a .VariableCount ))),
70+ fmt .Sprintf ("%s %s" , pk ("Sensitive Variables" ), valStyle .Render (fmt .Sprint (a .SensitiveVariableCount ))),
71+ fmt .Sprintf ("%s %s" , pk ("Outputs" ), valStyle .Render (fmt .Sprint (a .OutputCount ))),
72+ fmt .Sprintf ("%s %s" , pk ("Sensitive Outputs" ), valStyle .Render (fmt .Sprint (a .SensitiveOutputCount ))),
3873 }
3974
4075 rightColumn := lipgloss .NewStyle ().
4176 PaddingLeft (4 ).
77+ PaddingRight (4 ).
4278 Render (strings .Join (lines , "\n " ))
79+
4380 logoColumn := logoStyle .Render (logo )
4481
4582 return container .Render (
0 commit comments