14175eba70
Add `pipelines retention report` (strictly read-only) and `pipelines retention apply` (explicit, approval-gated removal) with an age and/or count policy, protected-run rules, atomic single-transaction removal and honest database space reclaim. Nothing prunes automatically.
154 lines
4.8 KiB
Go
154 lines
4.8 KiB
Go
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 artifactsSummary(step protocol.RunStep) string {
|
|
if len(step.Artifacts) == 0 {
|
|
if step.Status == "running" {
|
|
return "Artifacts · capture pending"
|
|
}
|
|
return "Artifacts · not recorded for this visit"
|
|
}
|
|
label := fmt.Sprintf("Artifacts · %d recorded", len(step.Artifacts))
|
|
if step.ArtifactsSkipped > 0 || step.ArtifactsTruncated > 0 {
|
|
label += " · partial capture"
|
|
}
|
|
return label
|
|
}
|
|
|
|
// HumanBytes renders a byte size with binary units. It is the one byte
|
|
// formatter shared by the TUI and the CLI, so sizes never diverge.
|
|
func HumanBytes(size int64) string {
|
|
const unit = 1024
|
|
if size < unit {
|
|
return fmt.Sprintf("%d B", size)
|
|
}
|
|
div, exp := int64(unit), 0
|
|
for value := size / unit; value >= unit; value /= unit {
|
|
div *= unit
|
|
exp++
|
|
}
|
|
return fmt.Sprintf("%.1f %ciB", float64(size)/float64(div), "KMGTPE"[exp])
|
|
}
|
|
|
|
// artifactCaptureNotes lists the partial-capture notes for one visit: one line
|
|
// per non-zero excluded/omitted count, and nothing when the capture was
|
|
// complete. A visit with no recorded artifacts yields no notes, mirroring the
|
|
// app's artifactCaptureNotes, so notes never accompany an empty-state summary.
|
|
func artifactCaptureNotes(step protocol.RunStep) []string {
|
|
if len(step.Artifacts) == 0 {
|
|
return nil
|
|
}
|
|
var notes []string
|
|
if step.ArtifactsSkipped > 0 {
|
|
notes = append(notes, fmt.Sprintf("%d files excluded from artifact capture.", step.ArtifactsSkipped))
|
|
}
|
|
if step.ArtifactsTruncated > 0 {
|
|
notes = append(notes, fmt.Sprintf("%d files omitted by artifact capture limits.", step.ArtifactsTruncated))
|
|
}
|
|
return notes
|
|
}
|
|
|
|
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
|
|
}
|