1- // Package tui contains the logic for the terminal user interface of the application.
21package tui
32
43import (
54 "fmt"
5+ "regexp"
66 "strings"
77
88 "github.com/charmbracelet/bubbles/key"
@@ -11,6 +11,14 @@ import (
1111 zone "github.com/lrstanley/bubblezone"
1212)
1313
14+ // ansiRegex is used to strip ANSI escape codes from strings.
15+ var ansiRegex = regexp .MustCompile (`\x1b\[[0-9;]*m` )
16+
17+ // stripAnsi removes ANSI escape codes from a string.
18+ func stripAnsi (str string ) string {
19+ return ansiRegex .ReplaceAllString (str , "" )
20+ }
21+
1422// View is the main render function for the application.
1523func (m Model ) View () string {
1624 if m .showHelp {
@@ -89,7 +97,21 @@ func (m Model) renderPanel(title string, width, height int, panel Panel) string
8997 var finalLine string
9098
9199 if i == p .cursor && isFocused {
92- cleanLine := strings .ReplaceAll (line , "\t " , " " )
100+ var cleanLine string
101+ // For the selected line, strip any existing ANSI codes before applying selection style.
102+ if panel == FilesPanel {
103+ // For files panel, don't show the hidden path in the selection.
104+ parts := strings .Split (line , "\t " )
105+ if len (parts ) >= 3 {
106+ cleanLine = fmt .Sprintf ("%s %s %s" , parts [0 ], parts [1 ], parts [2 ])
107+ } else {
108+ cleanLine = line
109+ }
110+ } else {
111+ cleanLine = stripAnsi (line )
112+ }
113+
114+ cleanLine = strings .ReplaceAll (cleanLine , "\t " , " " ) // Also replace tabs
93115 selectionStyle := m .theme .SelectedLine .Width (contentWidth )
94116 finalLine = selectionStyle .Render (cleanLine )
95117 } else {
@@ -259,23 +281,22 @@ func styleUnselectedLine(line string, panel Panel, theme Theme) string {
259281 case CommitsPanel :
260282 parts := strings .SplitN (line , "\t " , 4 )
261283 if len (parts ) != 4 {
262- return styleGraph (line , theme ) // Render graph-only lines.
284+ // This is a graph-only line, already colored by git.
285+ // We just replace the placeholder node with a styled one.
286+ return strings .ReplaceAll (line , "○" , theme .GraphNode .Render ("○" ))
263287 }
264288 graph , sha , author , subject := parts [0 ], parts [1 ], parts [2 ], parts [3 ]
265- styledGraph := styleGraph (graph , theme )
266- styledSHA := theme .CommitSHA .Render (sha )
267289
268- styledAuthor := theme .CommitAuthor .Render (author )
269-
270- commitNodeIndex := strings .Index (graph , graphNodeChar )
271- if commitNodeIndex != - 1 {
272- authorColorStyle := theme .GraphColors [commitNodeIndex % len (theme .GraphColors )]
273- styledAuthor = authorColorStyle .Render (author )
274- }
290+ // The graph string is already colored by git, but we style the node.
291+ styledGraph := strings .ReplaceAll (graph , "○" , theme .GraphNode .Render ("○" ))
275292
293+ // Apply our theme's styles to the other parts.
294+ styledSHA := theme .CommitSHA .Render (sha )
295+ styledAuthor := theme .CommitAuthor .Render (author )
276296 if strings .HasPrefix (strings .ToLower (subject ), "merge" ) {
277297 styledAuthor = theme .CommitMerge .Render (author )
278298 }
299+
279300 final := lipgloss .JoinHorizontal (lipgloss .Left , styledSHA , " " , styledAuthor , " " , subject )
280301 return fmt .Sprintf ("%s %s" , styledGraph , final )
281302 case StashPanel :
@@ -316,20 +337,3 @@ func styleChar(char byte, style lipgloss.Style) string {
316337 }
317338 return style .Render (string (char ))
318339}
319-
320- // styleGraph applies colors to the git log graph characters.
321- func styleGraph (graph string , theme Theme ) string {
322- var styled strings.Builder
323- for i , char := range graph {
324- switch char {
325- case '|' , '\\' , '/' :
326- color := theme .GraphColors [i % len (theme .GraphColors )]
327- styled .WriteString (color .Render (string (char )))
328- case '○' :
329- styled .WriteString (theme .GraphNode .Render ("○" ))
330- default :
331- styled .WriteString (string (char ))
332- }
333- }
334- return styled .String ()
335- }
0 commit comments