865993b200
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.
472 lines
15 KiB
Go
472 lines
15 KiB
Go
package tui
|
||
|
||
import (
|
||
"fmt"
|
||
"strconv"
|
||
"strings"
|
||
|
||
tea "github.com/charmbracelet/bubbletea"
|
||
"github.com/charmbracelet/lipgloss"
|
||
"github.com/charmbracelet/x/ansi"
|
||
"github.com/sahilm/fuzzy"
|
||
|
||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||
)
|
||
|
||
var commands = []string{":status", ":logs", ":new", ":drafts", ":templates", ":export", ":import", ":refresh", ":ping", ":edit", ":submit", ":versions", ":run", ":runs", ":retry", ":debug", ":pipeline", ":home", ":quit"}
|
||
|
||
var commandDescriptions = map[string]string{
|
||
":export": "Select pipelines and save their definitions in a portable ZIP in the current directory.",
|
||
":import": "Select definitions from a ZIP, resolve conflicts, then confirm publication. Schedules may activate.",
|
||
":status": "Server health and phone connection details", ":logs": "Read retained client diagnostics", ":new": "Create a retained pipeline draft", ":drafts": "Continue editing or publish a draft", ":templates": "Choose a reusable workflow starter", ":refresh": "Reload the current screen from the server", ":ping": "Check the server connection", ":edit": "Edit a retained draft of this definition", ":submit": "Publish the edited draft after confirmation", ":versions": "View an immutable published version", ":run": "Describe a task for this pipeline", ":runs": "Inspect recorded runs and their results", ":retry": "Confirm a fresh run; previous side effects may repeat", ":debug": "Open the configured harness with the pinned definition and failure evidence", ":pipeline": "View the definition used by this run", ":home": "Return to recent results and pipelines", ":quit": "Exit the client; server-owned runs continue"}
|
||
|
||
func (m Model) footerContent() string {
|
||
if footer := m.activeFooter(); footer != "" {
|
||
return footer
|
||
}
|
||
if m.width < 80 && m.runOpen {
|
||
text := "↑↓ select · Enter open · / find · Esc"
|
||
if len(m.visibleRuns()) == 0 {
|
||
text = "Esc back · / search · : commands"
|
||
}
|
||
if m.runDetail != nil {
|
||
text = "Tab graph · n/p visit · j/k read · Esc"
|
||
if m.runGraphFocus {
|
||
text = "Tab details · n/p visit · hjkl pan · Esc"
|
||
}
|
||
if m.width < 40 {
|
||
text = "Esc back · Tab pane · j/k read"
|
||
}
|
||
if m.width < 30 {
|
||
text = "Esc · Tab pane"
|
||
}
|
||
}
|
||
return lipgloss.NewStyle().Foreground(themeMuted).Render(ansi.Truncate(text, max(1, m.width), "…"))
|
||
}
|
||
if m.width < 80 && m.keys.detail {
|
||
text := "Tab stage · n/p stage · hjkl pan · Esc"
|
||
if m.stageFocus {
|
||
text = "Tab graph · n/p stage · j/k read · Esc"
|
||
}
|
||
if m.width < 40 {
|
||
text = "Esc home · Tab pane · n/p stage"
|
||
}
|
||
return lipgloss.NewStyle().Foreground(themeMuted).Render(ansi.Truncate(text, max(1, m.width), "…"))
|
||
}
|
||
label := fmt.Sprintf(" LOGS %d · :logs ", len(m.lines))
|
||
style := lipgloss.NewStyle().Foreground(themeMuted)
|
||
if m.unread > 0 {
|
||
label = fmt.Sprintf(" ● %d NEW · LOGS %d · :logs ", m.unread, len(m.lines))
|
||
if m.width < 60 {
|
||
label = fmt.Sprintf(" ● %d NEW · :logs ", m.unread)
|
||
}
|
||
style = lipgloss.NewStyle().Foreground(themeAccent).Background(themeSelection)
|
||
} else if len(m.lines) > 0 {
|
||
style = lipgloss.NewStyle().Foreground(themeAccent).Bold(true)
|
||
}
|
||
if m.homePage() {
|
||
label = strings.ReplaceAll(label, " · :logs", "")
|
||
}
|
||
label = style.Render(ansi.Truncate(label, m.width, ""))
|
||
available := m.width - lipgloss.Width(label) - 1
|
||
left := ""
|
||
if available > 0 {
|
||
keys := m.keys
|
||
keys.runInspect = m.runDetail != nil
|
||
keys.runGraph = m.runGraphFocus
|
||
keys.retryAvailable = m.selectedFailedRun() > 0
|
||
left = ansi.Truncate(m.help.View(keys), available, "…")
|
||
}
|
||
return left + strings.Repeat(" ", max(0, m.width-lipgloss.Width(left)-lipgloss.Width(label))) + label
|
||
}
|
||
|
||
func (m Model) homeLines() []string {
|
||
panelWidth := m.width
|
||
if m.width >= 120 {
|
||
panelWidth = (m.width - 2) * 3 / 5
|
||
}
|
||
runs := []string{bar("Latest runs", "Outcome & result", max(1, panelWidth-4), true), ""}
|
||
if len(m.runs) == 0 {
|
||
if !m.loaded {
|
||
runs = append(runs, m.loading("Loading recent work…"))
|
||
} else {
|
||
runs = append(runs, "No runs yet.", "Results will appear here after you run a pipeline.")
|
||
}
|
||
}
|
||
limit := 4
|
||
if m.width < 120 {
|
||
limit = 2
|
||
}
|
||
for _, r := range m.runs[:min(limit, len(m.runs))] {
|
||
runs = append(runs, bar(fmt.Sprintf("#%d %s", r.ID, r.Label()), m.progress(r.Status), max(1, panelWidth-4), false))
|
||
runs = append(runs, previewRows(resultLabel(r), max(1, panelWidth-4), 3)...)
|
||
runs = append(runs, lipgloss.NewStyle().Foreground(themeMuted).Render(fmt.Sprintf("v%d · %d completed visits · %s", r.Version, r.CompletedSteps, runDuration(r.CreatedAt, r.FinishedAt))))
|
||
if r.CurrentStage != "" && (r.Status == "running" || r.Status == "queued") {
|
||
runs = append(runs, "Working on "+r.CurrentStage)
|
||
}
|
||
if r.Status == protocol.RunStatusWaiting {
|
||
runs = append(runs, "Waiting on a person · stage "+r.CurrentStage)
|
||
}
|
||
runs = append(runs, "")
|
||
}
|
||
if len(m.runs) > limit {
|
||
runs = append(runs, lipgloss.NewStyle().Foreground(themeMuted).Render(fmt.Sprintf("%d more runs in history", len(m.runs)-limit)))
|
||
}
|
||
pipelineWidth := m.width
|
||
if m.width >= 120 {
|
||
pipelineWidth = m.width - panelWidth - 2
|
||
}
|
||
lines := []string{bar("Existing pipelines", fmt.Sprintf("%d", len(m.pipelines)), max(1, pipelineWidth-4), true), ""}
|
||
if !m.loaded {
|
||
lines = append(lines, m.loading("Waiting for pipeline data…"))
|
||
} else if len(m.pipelines) == 0 {
|
||
lines = append(lines, "No pipelines yet. Create one using commands.")
|
||
} else {
|
||
for _, p := range m.pipelines[:min(4, len(m.pipelines))] {
|
||
lines = append(lines,
|
||
lipgloss.NewStyle().Bold(true).Render(p.Name), lipgloss.NewStyle().Foreground(themeMuted).Render(fmt.Sprintf("v%d · %d stages", p.Version, p.Stages)))
|
||
if r := m.latestPipelineRun(p.ID, 0); r != nil {
|
||
lines = append(lines, fmt.Sprintf("Last run #%d · %s · v%d · %s", r.ID, r.Label(), r.Version, m.progress(r.Status)))
|
||
lines = append(lines, previewRows(resultLabel(*r), max(1, pipelineWidth-4), 2)...)
|
||
} else {
|
||
lines = append(lines, "Not run yet · no recorded results")
|
||
}
|
||
if m.usage != nil {
|
||
for _, u := range m.usage.Pipelines {
|
||
if u.PipelineID == p.ID {
|
||
lines = append(lines, fmt.Sprintf(" %d runs · %s · avg %s", u.Total, successLabel(u.RunCounts), averageLabel(u.RunCounts)))
|
||
break
|
||
}
|
||
}
|
||
}
|
||
lines = append(lines, "")
|
||
}
|
||
if len(m.pipelines) > 4 {
|
||
lines = append(lines, fmt.Sprintf("%d more pipelines", len(m.pipelines)-4))
|
||
}
|
||
}
|
||
if m.width < 8 {
|
||
return append(append(runs, ""), lines...)
|
||
}
|
||
widget := func(rows []string, width int) string {
|
||
for i := range rows {
|
||
rows[i] = ansi.Truncate(rows[i], max(1, width-4), "…")
|
||
}
|
||
content := lipgloss.NewStyle().Background(themeSurface).Foreground(themeText).
|
||
Padding(1, 2).Width(width).
|
||
Render(surfaceContent(strings.Join(rows, "\n"), themeSurface))
|
||
return content
|
||
}
|
||
panels := widget(runs, panelWidth) + "\n\n" + widget(lines, panelWidth)
|
||
if m.width >= 120 {
|
||
panels = lipgloss.JoinHorizontal(lipgloss.Top, widget(runs, panelWidth), " ", widget(lines, m.width-panelWidth-2))
|
||
}
|
||
if m.usage != nil {
|
||
panels += "\n\n" + widget(m.usageLines(), m.width)
|
||
}
|
||
return strings.Split(panels, "\n")
|
||
}
|
||
|
||
func previewRows(text string, width, limit int) []string {
|
||
rows := strings.Split(ansi.Wrap(ansi.Strip(text), max(1, width), ""), "\n")
|
||
if len(rows) > limit {
|
||
rows = rows[:limit]
|
||
rows[limit-1] = ansi.Truncate(rows[limit-1], max(1, width-1), "") + "…"
|
||
}
|
||
return rows
|
||
}
|
||
|
||
func (m Model) homePage() bool {
|
||
return !m.keys.detail && !m.runOpen && !m.templateOpen && !m.draftOpen && m.runForm == 0
|
||
}
|
||
|
||
func (m Model) pageCommands() []string {
|
||
available := []string{}
|
||
detailPage := m.keys.detail && !m.runOpen
|
||
for _, command := range commands {
|
||
switch command {
|
||
case ":debug":
|
||
if m.selectedRunID() <= 0 {
|
||
continue
|
||
}
|
||
case ":retry":
|
||
if m.selectedFailedRun() <= 0 {
|
||
continue
|
||
}
|
||
case ":pipeline":
|
||
if id, version := m.selectedRunPipeline(); id <= 0 || version <= 0 {
|
||
continue
|
||
}
|
||
case ":new", ":ping", ":templates":
|
||
if !m.homePage() {
|
||
continue
|
||
}
|
||
case ":edit", ":versions", ":run":
|
||
if !detailPage || m.detail == nil || m.detailLoading {
|
||
continue
|
||
}
|
||
case ":submit":
|
||
if !detailPage || m.pendingDraft == nil {
|
||
continue
|
||
}
|
||
case ":home":
|
||
if m.homePage() {
|
||
continue
|
||
}
|
||
}
|
||
available = append(available, command)
|
||
}
|
||
return available
|
||
}
|
||
|
||
func (m Model) matches() []string {
|
||
available := m.pageCommands()
|
||
query := strings.TrimPrefix(strings.TrimSpace(m.commandInput.Value()), ":")
|
||
if query == "" {
|
||
return available
|
||
}
|
||
result := []string{}
|
||
for _, match := range fuzzy.Find(query, available) {
|
||
result = append(result, match.Str)
|
||
}
|
||
return result
|
||
}
|
||
|
||
func (m Model) updatePalette(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||
if m.searchOpen {
|
||
return m.updateSearch(msg)
|
||
}
|
||
items := m.matches()
|
||
switch msg.String() {
|
||
case "esc":
|
||
m.paletteOpen = false
|
||
m.keys.palette = false
|
||
return m, nil
|
||
case "down", "ctrl+n":
|
||
m.commandIndex = min(m.commandIndex+1, max(0, len(items)-1))
|
||
return m, nil
|
||
case "up", "ctrl+p":
|
||
m.commandIndex = max(0, m.commandIndex-1)
|
||
return m, nil
|
||
case "enter":
|
||
if len(items) == 0 {
|
||
return m, nil
|
||
}
|
||
command := items[min(m.commandIndex, len(items)-1)]
|
||
m.paletteOpen = false
|
||
m.keys.palette = false
|
||
switch command {
|
||
case ":export":
|
||
return m.transferPipelines(false)
|
||
case ":import":
|
||
return m.transferPipelines(true)
|
||
case ":status":
|
||
return m.openStatus()
|
||
case ":debug":
|
||
return m.debugRun()
|
||
case ":retry":
|
||
return m.retrySelectedRun()
|
||
case ":pipeline":
|
||
return m.openRunPipeline()
|
||
case ":drafts":
|
||
return m.openDrafts()
|
||
case ":templates":
|
||
return m.openTemplates()
|
||
case ":run":
|
||
return m.beginRun()
|
||
case ":runs":
|
||
return m.openRuns()
|
||
case ":logs":
|
||
m.logOpen = true
|
||
m.keys.logs = true
|
||
m.logTop = max(0, len(m.lines)-m.logPageSize())
|
||
m.logPan = 0
|
||
m.markVisibleLogs()
|
||
return m, nil
|
||
case ":new":
|
||
return m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'n'}})
|
||
case ":ping":
|
||
return m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'t'}})
|
||
case ":quit":
|
||
return m, tea.Quit
|
||
case ":edit":
|
||
return m.editPipeline()
|
||
case ":submit":
|
||
if m.pendingDraft != nil {
|
||
m.confirmOpen = true
|
||
m.keys.confirm = true
|
||
} else {
|
||
m.appendLine("no edited draft to create/update")
|
||
}
|
||
return m, nil
|
||
case ":versions":
|
||
return m.openVersions()
|
||
case ":home":
|
||
m.closeRuns()
|
||
return m.goHome(), nil
|
||
case ":refresh":
|
||
if m.templateOpen {
|
||
return m.openTemplates()
|
||
}
|
||
if m.runOpen {
|
||
request := protocol.Message{Type: protocol.ListRuns}
|
||
if m.requestedRunID > 0 {
|
||
request.Type = protocol.GetRun
|
||
request.RunID = m.requestedRunID
|
||
}
|
||
return m.runRequest(request)
|
||
}
|
||
if m.detail != nil && m.keys.detail {
|
||
return m.requestPipeline(m.detail.ID, m.detail.Version)
|
||
}
|
||
if m.conn == nil {
|
||
m.appendLine("not connected")
|
||
return m, nil
|
||
}
|
||
m.nextID++
|
||
return m, m.send(protocol.Message{Type: protocol.List, ID: strconv.FormatUint(m.nextID, 10)})
|
||
}
|
||
}
|
||
var cmd tea.Cmd
|
||
m.commandInput, cmd = m.commandInput.Update(msg)
|
||
m.commandIndex = 0
|
||
return m, cmd
|
||
}
|
||
|
||
// The popup reserves two lines for its border and two for title/separation.
|
||
func (m Model) logPageSize() int { return max(0, min(24, m.height-8)) }
|
||
|
||
func (m *Model) markVisibleLogs() {
|
||
for i := m.logTop; i < min(len(m.lines), m.logTop+m.logPageSize()); i++ {
|
||
if !m.logSeen[i] {
|
||
m.logSeen[i] = true
|
||
m.unread--
|
||
}
|
||
}
|
||
}
|
||
|
||
func (m Model) updateLogs(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||
switch msg.String() {
|
||
case "esc", "q":
|
||
m.logOpen = false
|
||
m.keys.logs = false
|
||
return m, nil
|
||
case "j", "down":
|
||
m.logTop++
|
||
case "k", "up":
|
||
m.logTop--
|
||
case "h":
|
||
m.logTop -= max(1, m.logPageSize()/2)
|
||
case "l":
|
||
m.logTop += max(1, m.logPageSize()/2)
|
||
case "left":
|
||
m.logPan = max(0, m.logPan-8)
|
||
case "right":
|
||
m.logPan += 8
|
||
}
|
||
widest := 0
|
||
for _, line := range m.lines {
|
||
widest = max(widest, ansi.StringWidth(line))
|
||
}
|
||
m.logPan = min(m.logPan, max(0, widest-min(100, max(1, m.width-6))))
|
||
m.logTop = max(0, min(m.logTop, max(0, len(m.lines)-m.logPageSize())))
|
||
m.markVisibleLogs()
|
||
return m, nil
|
||
}
|
||
|
||
// popup centres a dialog of at most preferredWidth (clamped to width-6) and
|
||
// truncates every line to that width so a modal cannot break the layout. Below
|
||
// roughly 20 columns the boxed dialog no longer fits, so popup falls back to the
|
||
// bare truncated lines without chrome; the TUI is designed for terminals at
|
||
// least 20 columns wide.
|
||
func popup(width, height, preferredWidth int, lines []string) string {
|
||
inner := min(preferredWidth, max(1, width-6))
|
||
for i := range lines {
|
||
lines[i] = ansi.Truncate(lines[i], inner, "…")
|
||
}
|
||
if len(lines) > 0 {
|
||
lines[0] = bar(lines[0], "", inner, true)
|
||
}
|
||
dialog := lipgloss.NewStyle().Background(themeElevated).Foreground(themeText).
|
||
Padding(1, 2).Width(inner + 4).
|
||
Render(surfaceContent(strings.Join(lines, "\n"), themeElevated))
|
||
if lipgloss.Width(dialog) > width || lipgloss.Height(dialog) > height {
|
||
lines = lines[:min(len(lines), height)]
|
||
for i := range lines {
|
||
lines[i] = ansi.Truncate(lines[i], width, "…")
|
||
}
|
||
dialog = strings.Join(lines, "\n")
|
||
}
|
||
return lipgloss.Place(width, height, lipgloss.Center, lipgloss.Center, dialog)
|
||
}
|
||
|
||
// Safety text wraps and scrolls independently of its pinned title and controls.
|
||
func wrappedDialog(width, height, preferredWidth int, title string, paragraphs []string, controls string, top int) string {
|
||
if width <= 0 || height <= 0 {
|
||
return ""
|
||
}
|
||
dialogWidth := min(width, preferredWidth+4)
|
||
inner := max(1, dialogWidth-4)
|
||
var rows []string
|
||
for i, text := range paragraphs {
|
||
if i > 0 {
|
||
rows = append(rows, "")
|
||
}
|
||
rows = append(rows, strings.Split(ansi.Wrap(ansi.Strip(text), inner, ""), "\n")...)
|
||
}
|
||
dialogHeight := min(height, len(rows)+4)
|
||
page := max(0, dialogHeight-4)
|
||
top = max(0, min(top, max(0, len(rows)-page)))
|
||
if page < len(rows) {
|
||
controls += " · j/k read"
|
||
}
|
||
content := panel(dialogWidth, dialogHeight, []string{bar(title, "", inner, true)}, rows[top:min(len(rows), top+page)], controls, true)
|
||
return lipgloss.Place(width, height, lipgloss.Center, lipgloss.Center, content)
|
||
}
|
||
|
||
func (m Model) logsView(height int) string {
|
||
end := min(len(m.lines), m.logTop+m.logPageSize())
|
||
lines := []string{fmt.Sprintf("Logs · %d–%d / %d", min(m.logTop+1, len(m.lines)), end, len(m.lines)), ""}
|
||
if len(m.lines) == 0 {
|
||
lines[0] = "Logs"
|
||
lines = append(lines, "No logs yet.")
|
||
} else {
|
||
width := min(100, max(1, m.width-6))
|
||
for _, line := range m.lines[m.logTop:end] {
|
||
lines = append(lines, ansi.Cut(line, m.logPan, m.logPan+width))
|
||
}
|
||
}
|
||
return popup(m.width, height, 100, lines)
|
||
}
|
||
|
||
func (m Model) paletteView(height int) string {
|
||
if m.searchOpen {
|
||
return m.searchView(height)
|
||
}
|
||
field := m.commandInput
|
||
field.Width = max(1, min(48, m.width-10))
|
||
lines := []string{"Commands", field.View(), ""}
|
||
items := m.matches()
|
||
if len(items) == 0 {
|
||
lines = append(lines, "No matching commands")
|
||
}
|
||
if len(items) > 0 {
|
||
selected := items[min(m.commandIndex, len(items)-1)]
|
||
lines = append(lines, previewRows(commandDescriptions[selected], min(50, max(1, m.width-10)), 2)...)
|
||
lines = append(lines, "")
|
||
}
|
||
start, end := selectionWindow(len(items), m.commandIndex, max(1, height-8))
|
||
for i := start; i < end; i++ {
|
||
item := items[i]
|
||
prefix := " "
|
||
if i == m.commandIndex {
|
||
prefix = "› "
|
||
}
|
||
lines = append(lines, menuOption(prefix+item, i == m.commandIndex))
|
||
}
|
||
if len(items) > 0 {
|
||
lines = append(lines, lipgloss.NewStyle().Foreground(themeMuted).Render(fmt.Sprintf("%d–%d / %d commands", start+1, end, len(items))))
|
||
}
|
||
return popup(m.width, height, 54, lines)
|
||
}
|