package tui import ( "fmt" "strings" "git.a-wan.net/alan/pipelines/internal/protocol" "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/ansi" ) func fileChangesSummary(step protocol.RunStep) string { if step.FileChanges == nil { if step.Status == "running" { return "File changes · capture pending" } return "File changes · not recorded for this visit" } c := step.FileChanges if c.Status == "unavailable" { return "File changes · capture unavailable" } label := fmt.Sprintf("File changes · %d observed files", len(c.Files)) if len(c.Files) == 0 { label = "File changes · no changes observed" } if c.Status == "partial" { label += " · partial capture" } return label + " · d view diff" } func diffHighlight(line string) string { line = ansi.Strip(line) line = strings.ReplaceAll(strings.ReplaceAll(line, "\t", " "), "\r", "␍") line = strings.Map(func(r rune) rune { if r < 32 || r == 127 { return '�' } return r }, line) style := lipgloss.NewStyle() switch { case strings.HasPrefix(line, "--- "), strings.HasPrefix(line, "+++ "): style = style.Foreground(themeMuted).Bold(true) case strings.HasPrefix(line, "@@"): style = style.Foreground(themeAccent).Bold(true) case strings.HasPrefix(line, "+"): style = style.Foreground(lipgloss.Color("2")) case strings.HasPrefix(line, "-"): style = style.Foreground(lipgloss.Color("1")) case strings.HasPrefix(line, "\\ "): style = style.Foreground(themeMuted) } return style.Render(line) } // HighlightDiff also sanitizes untrusted source before terminal rendering. func HighlightDiff(patch string) string { lines := strings.Split(strings.TrimSuffix(patch, "\n"), "\n") for i, line := range lines { lines[i] = diffHighlight(line) } return strings.Join(lines, "\n") } func stageDiffLines(step protocol.RunStep) []string { c := step.FileChanges if c == nil { return []string{fileChangesSummary(step)} } lines := []string{fileChangesSummary(step), "Observed workspace changes during this visit; not proof of exclusive authorship."} if c.Scope != "" { lines = append(lines, c.Scope) } if c.Diagnostic != "" { lines = append(lines, lipgloss.NewStyle().Foreground(themeError).Render(ansi.Strip(c.Diagnostic))) } if c.Skipped > 0 { lines = append(lines, fmt.Sprintf("%d files excluded from capture.", c.Skipped)) } if c.Omitted > 0 { lines = append(lines, fmt.Sprintf("%d additional changed files omitted by capture limits.", c.Omitted)) } for _, file := range c.Files { lines = append(lines, "", lipgloss.NewStyle().Bold(true).Render(fmt.Sprintf("%s · %q", file.Kind, file.Path))) if file.BeforeMode != file.AfterMode { lines = append(lines, fmt.Sprintf("Mode: %s → %s", file.BeforeMode, file.AfterMode)) } if file.Binary { lines = append(lines, "Binary file changed · contents not displayed.") } if file.Patch != "" { for _, line := range strings.Split(strings.TrimSuffix(file.Patch, "\n"), "\n") { lines = append(lines, diffHighlight(line)) } } if file.Truncated { lines = append(lines, lipgloss.NewStyle().Foreground(themeMuted).Render("Patch omitted or truncated by capture limits.")) } if !file.Binary && file.Patch == "" && !file.Truncated { lines = append(lines, "No text hunk · empty file or file-mode change.") } } return lines }