Files

168 lines
5.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package tui
import (
"fmt"
"strings"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/stage"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/ansi"
)
// lastWaitingStep returns the most recent step a run is waiting on (the last
// waiting step by sequence), or false when no step is waiting. The decision
// overlay and the run summary both read it, so the scan lives once.
func lastWaitingStep(r *protocol.RunDetail) (protocol.RunStep, bool) {
for i := len(r.Steps) - 1; i >= 0; i-- {
if r.Steps[i].Status == protocol.StepStatusWaiting {
return r.Steps[i], true
}
}
return protocol.RunStep{}, false
}
// waitingStage returns the stage the open run is waiting on plus its recorded
// prompt and selectable options. It prefers the self-contained evidence on the
// waiting step and falls back to the pinned graph's edges when a step recorded
// no options.
func (m Model) waitingStage() (string, []string, string) {
if m.runDetail != nil && m.runDetail.ExportPendingSequence > 0 {
return m.runDetail.CurrentStage, nil, ""
}
if m.runDetail == nil {
return "", nil, ""
}
if step, ok := lastWaitingStep(m.runDetail); ok {
return step.Stage, protocol.WaitingOptions(step.Output), protocol.WaitingPrompt(step.Output)
}
key := m.runDetail.CurrentStage
for _, declared := range m.runDetail.Graph.Stages {
if declared.Key == key {
return key, stage.HumanOptions(declared.Edges), declared.Prompt
}
}
return key, nil, ""
}
func (m Model) openDecision() (tea.Model, tea.Cmd) {
if m.runDetail == nil || m.runDetail.Status != protocol.RunStatusWaiting || m.runDetail.ExportPendingSequence > 0 {
m.runError = "This run is not waiting for a decision."
return m, nil
}
key, options, _ := m.waitingStage()
if len(options) == 0 {
m.runError = "No selectable options recorded for the waiting stage."
return m, nil
}
m.decisionOpen = true
m.decisionRunID = m.runDetail.ID
m.decisionStage = key
m.decisionOptions = options
m.decisionIndex = 0
m.decisionFeedback = newInput()
m.decisionFeedback.Placeholder = "Optional feedback"
m.decisionFeedback.Width = max(1, min(60, m.width-12))
m.decisionFeedbackFocus = false
return m, nil
}
func (m *Model) closeDecision() {
m.decisionOpen = false
m.decisionRunID = 0
m.decisionStage = ""
m.decisionOptions = nil
m.decisionIndex = 0
m.decisionFeedback = newInput()
m.decisionFeedbackFocus = false
}
func (m Model) updateDecision(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
switch msg.String() {
case "esc":
m.closeDecision()
return m, nil
case "tab":
m.decisionFeedbackFocus = !m.decisionFeedbackFocus
if m.decisionFeedbackFocus {
return m, m.decisionFeedback.Focus()
}
m.decisionFeedback.Blur()
return m, nil
case "f":
if !m.decisionFeedbackFocus {
m.decisionFeedbackFocus = true
return m, m.decisionFeedback.Focus()
}
case "up", "k":
if !m.decisionFeedbackFocus {
m.decisionIndex = max(0, m.decisionIndex-1)
return m, nil
}
case "down", "j":
if !m.decisionFeedbackFocus {
m.decisionIndex = min(m.decisionIndex+1, max(0, len(m.decisionOptions)-1))
return m, nil
}
case "enter":
if len(m.decisionOptions) == 0 {
return m, nil
}
option := m.decisionOptions[min(m.decisionIndex, len(m.decisionOptions)-1)]
feedback := strings.TrimSpace(m.decisionFeedback.Value())
request := protocol.Message{Type: protocol.DecideRun, RunID: m.decisionRunID, Stage: m.decisionStage, Option: option, Feedback: feedback}
m.closeDecision()
return m.runRequest(request)
}
if m.decisionFeedbackFocus {
var cmd tea.Cmd
m.decisionFeedback, cmd = m.decisionFeedback.Update(msg)
return m, cmd
}
return m, nil
}
func (m Model) decisionView(height int) string {
title := fmt.Sprintf("Decide %s · run #%d", m.decisionStage, m.decisionRunID)
intro := "Choose where the run goes next. The choice and feedback are recorded as the stage's evidence."
if m.width < 65 {
intro = "Choose where the run goes next."
}
// Size the option window to the panel body so every shown paragraph fits and
// the selected option is never clipped: each option costs a line plus a
// separator, and the feedback line always fits. The count of shown options
// goes in the controls, so a windowed list still says more exist.
dialogWidth := min(m.width, 76)
inner := max(1, dialogWidth-4)
introLines := len(strings.Split(ansi.Wrap(intro, inner, ""), "\n"))
capacity := max(1, (height-4-introLines)/2-1)
count := len(m.decisionOptions)
start, end := selectionWindow(count, m.decisionIndex, capacity)
paragraphs := []string{intro}
for i := start; i < end; i++ {
marker := " "
if i == m.decisionIndex {
marker = " › "
}
paragraphs = append(paragraphs, marker+m.decisionOptions[i])
}
paragraphs = append(paragraphs, "Feedback: "+m.decisionFeedback.View())
controls := "↑/↓ choose · Tab feedback · Enter decide · Esc cancel"
if end-start < count {
controls += fmt.Sprintf(" · %d/%d", end-start, count)
}
if m.width < 65 {
controls = "↑↓ select · Enter decide · Esc"
if end-start < count {
controls += fmt.Sprintf(" · %d/%d", end-start, count)
}
}
if m.decisionFeedbackFocus {
controls = "type feedback · Tab options · Enter decide · Esc cancel"
if m.width < 65 {
controls = "type feedback · Enter decide · Esc"
}
}
return wrappedDialog(m.width, height, 72, title, paragraphs, controls, 0)
}