Files
pipelines/internal/tui/file_changes.go
T
alan 865993b200 Rework omni search onto a typed search contract and add a TUI artifacts view
Protocol: replace the loose Query/Kind/Matches/Limit search fields with a Search request object and a SearchResults response, and have the server answer search from Store.Search over the shared run projections (runFrom/runStamps).

CLI: add the read-only 'pipelines search QUERY [--type KIND] [--limit N] [--json]' command with usage/help, and document the typed targets, ranking and bounds in docs/cli.md.

TUI: the '/' popup keeps type-to-search, up/down, Enter and Esc, renders one ranked list with kind badges and a bracketed (non-colour) match snippet, and Enter opens the matched pipeline, run, step, artifact or field; footer text updated to match.

TUI: add a dedicated run artifacts view ('A') with open/copy of recorded files, and move artifacts out of the run-detail File changes section (artifactCaptureNotes).

Tests: add Store.OpenReadOnly for read-only inspection of a live database plus pipeline/server/TUI search tests and artifacts tests, including env-gated live checks.
2026-10-16 23:48:38 +01:00

152 lines
4.7 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
}
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
}