package tui import ( "context" "fmt" "net/url" "strconv" "strings" "time" "github.com/charmbracelet/bubbles/help" "github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/spinner" "github.com/charmbracelet/bubbles/textarea" "github.com/charmbracelet/bubbles/textinput" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/ansi" "github.com/coder/websocket" "git.a-wan.net/alan/pipelines/internal/client" "git.a-wan.net/alan/pipelines/internal/draft" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" ) type connectedMsg struct { conn *websocket.Conn watch *client.Watch } type receivedMsg protocol.Message type sentMsg struct{ id string } type errorMsg struct { err error conn *websocket.Conn } type socketMsg struct { conn *websocket.Conn message protocol.Message notices []string } type reconnectMsg struct{} type keyMap struct { ping key.Binding quit key.Binding create key.Binding enter key.Binding cancel key.Binding commands key.Binding logs bool palette bool search key.Binding detail bool versions bool confirm bool runs bool runInspect bool runGraph bool retryAvailable bool templates bool drafts bool } func (k keyMap) ShortHelp() []key.Binding { if k.confirm { return []key.Binding{key.NewBinding(key.WithKeys("s"), key.WithHelp("s", "create/update pipeline")), key.NewBinding(key.WithKeys("e"), key.WithHelp("e", "edit")), k.cancel} } if k.drafts { return []key.Binding{key.NewBinding(key.WithKeys("n", "p"), key.WithHelp("n/p", "draft")), key.NewBinding(key.WithKeys("e"), key.WithHelp("e", "edit")), key.NewBinding(key.WithKeys("v"), key.WithHelp("v", "validate")), key.NewBinding(key.WithKeys("s"), key.WithHelp("s", "create/update pipeline")), k.cancel} } if k.versions { return []key.Binding{key.NewBinding(key.WithKeys("j", "k"), key.WithHelp("j/k", "select")), k.enter, k.cancel} } if k.logs { return []key.Binding{key.NewBinding(key.WithKeys("j", "k"), key.WithHelp("j/k", "line")), key.NewBinding(key.WithKeys("h", "l"), key.WithHelp("h/l", "half page")), k.cancel} } if k.palette { return []key.Binding{k.enter, k.cancel} } if k.templates { return []key.Binding{key.NewBinding(key.WithKeys("s", "enter"), key.WithHelp("s/Enter", "create pipeline")), key.NewBinding(key.WithKeys("tab"), key.WithHelp("Tab", "pane")), key.NewBinding(key.WithKeys("e"), key.WithHelp("e", "customize")), key.NewBinding(key.WithKeys("n", "p"), key.WithHelp("n/p", "template")), k.cancel} } if k.runs { if !k.runInspect { return []key.Binding{k.commands, k.search, key.NewBinding(key.WithKeys("j", "k"), key.WithHelp("j/k", "select")), k.enter, key.NewBinding(key.WithKeys("esc"), key.WithHelp("Esc", "back"))} } target, keys, action := "graph", "j/k", "scroll" if k.runGraph { target, keys, action = "details", "h/j/k/l", "pan" } return []key.Binding{k.commands, key.NewBinding(key.WithKeys("tab"), key.WithHelp("Tab", target)), key.NewBinding(key.WithKeys("n", "p"), key.WithHelp("n/p", "stage")), key.NewBinding(key.WithKeys(keys), key.WithHelp(keys, action)), key.NewBinding(key.WithKeys("esc"), key.WithHelp("Esc", "back"))} } if k.detail { return []key.Binding{k.commands, k.search, key.NewBinding(key.WithKeys("n", "p"), key.WithHelp("n/p", "next/prev stage")), key.NewBinding(key.WithKeys("tab"), key.WithHelp("Tab", "pane")), key.NewBinding(key.WithKeys("r"), key.WithHelp("r", "runs")), key.NewBinding(key.WithKeys("j", "k"), key.WithHelp("j/k", "scroll")), key.NewBinding(key.WithKeys("esc"), key.WithHelp("esc", "home"))} } return []key.Binding{k.search, k.commands} } func (k keyMap) FullHelp() [][]key.Binding { return [][]key.Binding{k.ShortHelp()} } type Model struct { editorCheckGeneration uint64 editorChecking bool statusOpen bool statusTop int statusRequest, statusError string systemStatus *protocol.SystemStatus usage *protocol.UsageStats draftOpen bool draftItems []draft.Draft draftIndex, draftTop int draftError, draftSource, draftNotice string ctx context.Context url string conn *websocket.Conn watch *client.Watch status string nextID uint64 lines []string width int height int help help.Model keys keyMap pipelines []protocol.PipelineSummary loaded bool notificationEndpoints []protocol.NotificationEndpoint logSeen []bool unread int logOpen bool logTop int logPan int paletteOpen bool commandInput textinput.Model commandIndex int homeTop int searchOpen bool searchResults []protocol.SearchResult searchText string searchRequest string searchInFlight bool searchQueued bool searchError string pendingRunNav *searchNav detail *protocol.PipelineDetail detailRequest string detailLoading bool detailError string pipelineTop int graphPan int versionsOpen bool versionIndex int confirmOpen bool actionConfirm *protocol.Message actionConfirmTop int actionApproved bool defaultModel string defaultHarness string editorDebugRunID int64 mutationTitle string mutationCommand func() tea.Cmd mutationTop int pendingDraft *editedMsg draftNameInput textinput.Model submitID string submitNotice string stageIndex int stageTop int stageFocus bool stageConfigExpanded bool reconnecting bool requestedPipelineID int64 requestedVersion int runs []protocol.RunSummary runDetail *protocol.RunDetail runOpen bool runForm int runInput textinput.Model runPrompt textarea.Model runWorkspace string cliRun, cliStarted, runPicker bool cliOptions RunOptions runPickerIndex int runPickerInput textinput.Model runTask string runPaths []string runIndex, runTop int requestedRunID int64 runStartRequest string runStepIndex int runDetailFocus bool runCollapsed map[string]bool runError string runPipelineID int64 runFailedOnly bool runSearch textinput.Model runSearchOpen bool runGraphFocus bool runGraphTop int runGraphPan int decisionOpen bool decisionRunID int64 decisionStage string decisionOptions []string decisionIndex int decisionFeedback textinput.Model decisionFeedbackFocus bool artifactsOpen bool artifactIndex int artifactTop int editorOpen bool editorInput textinput.Model editorIndex int editorDraft *editedMsg draftDeletingPath string draftDeletingTop int runSpinner spinner.Model templates []protocol.Template templateOpen, templateRaw bool templateGraphFocus, templateConfirm bool templateGraphTop, templateGraphPan int templateIndex, templateTop int templateLines []string templateRequest, templateError string } func New(ctx context.Context, url string) Model { return Model{ ctx: ctx, url: url, status: "Connecting…", width: 80, height: 24, help: newHelp(), runPrompt: textarea.New(), runSpinner: spinner.New(spinner.WithSpinner(spinner.Spinner{Frames: []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}, FPS: time.Second / 12}), spinner.WithStyle(lipgloss.NewStyle().Foreground(themeAccent))), keys: keyMap{ commands: key.NewBinding(key.WithKeys(":"), key.WithHelp(":", "commands")), search: key.NewBinding(key.WithKeys("/"), key.WithHelp("/", "omni search")), create: key.NewBinding(key.WithKeys("n"), key.WithHelp("n", "new pipeline")), enter: key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "continue")), cancel: key.NewBinding(key.WithKeys("esc"), key.WithHelp("esc", "cancel")), ping: key.NewBinding(key.WithKeys("t"), key.WithHelp("t", "test ping")), quit: key.NewBinding(key.WithKeys(":quit"), key.WithHelp(":quit", "quit")), }, } } func (m Model) Init() tea.Cmd { return tea.Batch(m.connect(), runPoll(), m.runSpinner.Tick) } func (m Model) connect() tea.Cmd { return func() tea.Msg { ctx, cancel := context.WithTimeout(m.ctx, 5*time.Second) defer cancel() conn, err := client.Dial(ctx, m.url) if err != nil { return errorMsg{err: err} } // Ensure quitting also closes an idle connection. go func() { <-m.ctx.Done() conn.CloseNow() }() return connectedMsg{conn: conn, watch: &client.Watch{}} } } func (m Model) read() tea.Cmd { return func() tea.Msg { response, notices, err := client.ReceiveNotice(m.ctx, m.conn, m.watch) if err != nil { return errorMsg{err: err, conn: m.conn} } return socketMsg{conn: m.conn, message: response, notices: notices} } } func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case debugClosedMsg: if msg.err != nil { m.appendLine("Harness debug: " + msg.err.Error()) } return m, nil case artifactActionMsg: if msg.err != nil { m.appendLine("Artifact " + msg.verb + ": " + msg.err.Error()) return m, nil } if msg.verb == "copy" { m.appendLine("Copied artifact URL: " + msg.url) } else { m.appendLine("Opened artifact URL: " + msg.url) } return m, nil case spinner.TickMsg: var cmd tea.Cmd m.runSpinner, cmd = m.runSpinner.Update(msg) return m, cmd case runTick: if m.conn == nil { return m, runPoll() } request := protocol.Message{Type: protocol.ListRuns} if m.runOpen && m.requestedRunID > 0 { request.Type = protocol.GetRun request.RunID = m.requestedRunID } if m.runOpen && m.runDetail != nil { request.Type = protocol.GetRun request.RunID = m.runDetail.ID } updated, cmd := m.runRequest(request) return updated, tea.Batch(cmd, runPoll()) case tea.WindowSizeMsg: m.width = max(1, msg.Width) m.height = max(1, msg.Height) m.help.Width = m.width if m.runForm == 1 { m.runPrompt.SetWidth(max(1, min(96, m.width-12))) m.runPrompt.SetHeight(max(1, min(14, m.height-10))) } if m.templateOpen { oldTop := m.templateTop graphTop, graphPan := m.templateGraphTop, m.templateGraphPan m.renderTemplate() m.templateTop = oldTop m.templateGraphTop, m.templateGraphPan = graphTop, graphPan m = m.clampTemplateGraph() } if m.keys.detail { m.clampPipelineScroll() } if m.logOpen { m.logTop = min(m.logTop, max(0, len(m.lines)-m.logPageSize())) m.markVisibleLogs() } case connectedMsg: m.notificationEndpoints = nil if m.conn != nil && m.conn != msg.conn { m.conn.CloseNow() } m.conn = msg.conn m.watch = msg.watch m.reconnecting = false m.status = m.url if endpoint, err := url.Parse(m.url); err == nil { m.status = endpoint.Host } if m.keys.detail && m.requestedPipelineID > 0 { updated, send := m.requestPipeline(m.requestedPipelineID, m.requestedVersion) m = updated.(Model) return m, tea.Batch(m.read(), send) } return m, m.read() case reconnectMsg: if m.conn != nil || m.ctx.Err() != nil { return m, nil } return m, m.connect() case socketMsg: if msg.conn != m.conn { return m, nil } // Surface the once-only identity/unrecognized-field notices on the same // connection without corrupting the TUI layout. m.appendNotices(msg.notices) return m.Update(receivedMsg(msg.message)) case receivedMsg: if msg.Type == protocol.Status && msg.ID == m.statusRequest { m.systemStatus = msg.Status m.statusError = "" m.statusRequest = "" return m, m.read() } if msg.Type == protocol.Error && msg.ID != "" && msg.ID == m.statusRequest { m.statusError = msg.Error m.statusRequest = "" } if msg.Type == protocol.SearchResults { if msg.ID == m.searchRequest && msg.SearchResults != nil { m.searchInFlight = false m.searchResults = msg.SearchResults.Results m.searchText = msg.SearchResults.Query m.searchError = "" m.commandIndex = min(m.commandIndex, max(0, len(msg.SearchResults.Results)-1)) if m.searchQueued { m.searchQueued = false updated, cmd := m.requestSearch() m = updated.(Model) return m, tea.Batch(m.read(), cmd) } } return m, m.read() } if msg.Type == protocol.PipelineList || msg.Type == protocol.Submitted { m.notificationEndpoints = msg.NotificationEndpoints if msg.DefaultModel != "" { m.defaultModel = msg.DefaultModel } if msg.Type == protocol.PipelineList { m.defaultHarness = msg.DefaultHarness } } if msg.Type == protocol.Templates && msg.ID == m.templateRequest { m.templates = msg.Templates m.templateIndex = 0 m.renderTemplate() } else if msg.Type == protocol.Runs { m.usage = msg.Usage m.runs = msg.Runs m.runIndex = min(m.runIndex, max(0, len(m.visibleRuns())-1)) } else if msg.Type == protocol.Run || msg.Type == protocol.RunStarted { accepted := m.runOpen && msg.RunDetail != nil && ((msg.Type == protocol.Run && msg.RunDetail.ID == m.requestedRunID) || (msg.Type == protocol.RunStarted && msg.ID == m.runStartRequest)) if accepted { if m.runDetail == nil { m.runGraphFocus = false m.runDetailFocus = true m.runGraphTop, m.runGraphPan = 0, 0 } if msg.Type == protocol.RunStarted { m.runStartRequest = "" } m.keys.runs = true m.runDetail = msg.RunDetail m.requestedRunID = msg.RunDetail.ID m.runError = "" m.runStepIndex = min(m.runStepIndex, len(msg.RunDetail.Steps)) if m.pendingRunNav != nil && m.pendingRunNav.runID == msg.RunDetail.ID { nav := *m.pendingRunNav m.pendingRunNav = nil m.applyRunNav(nav) } } } else if msg.Type == protocol.Pong { m.appendLine(fmt.Sprintf("pong #%s: %s", msg.ID, msg.Time)) } else if msg.Type == protocol.Error { m.appendLine(fmt.Sprintf("server error #%s: %s", msg.ID, msg.Error)) if msg.ID == m.templateRequest { m.templateError = msg.Error } if strings.HasPrefix(msg.ID, "run-") && m.runOpen { m.runError = msg.Error if msg.ID == m.runStartRequest { m.runStartRequest = "" } } if msg.ID != "" && msg.ID == m.detailRequest { m.detailLoading = false m.detailError = msg.Error } if msg.ID != "" && msg.ID == m.submitID { m.submitID = "" m.submitNotice = msg.Error if m.pendingDraft != nil { m.confirmOpen = true m.keys.confirm = true } } if msg.ID != "" && msg.ID == m.searchRequest { m.searchInFlight = false m.searchQueued = false m.searchResults = nil m.searchText = "" m.searchError = msg.Error } } else if msg.Type == protocol.Submitted { m.templateConfirm = false m.draftOpen = false m.keys.drafts = false m.templateOpen = false m.keys.templates = false verb := "Updated" if msg.Version == 1 { verb = "Created" } m.appendLine(fmt.Sprintf("%s pipeline %s: version %d", verb, msg.Name, msg.Version)) m.pipelines = msg.Pipelines m.loaded = true if msg.ID == m.submitID { m.submitID = "" m.pendingDraft = nil m.confirmOpen = false m.keys.confirm = false m.submitNotice = fmt.Sprintf("%s pipeline · version %d", verb, msg.Version) } if msg.Detail != nil { m.detail = msg.Detail m.keys.detail = true m.detailLoading = false m.detailRequest = "" m.detailError = "" m.resetPipelineView() m.requestedPipelineID = msg.Detail.ID m.requestedVersion = msg.Detail.Version } } else if msg.Type == protocol.Pipeline { if msg.ID == m.detailRequest { if msg.Detail == nil || (m.requestedPipelineID > 0 && msg.Detail.ID != m.requestedPipelineID) || (m.requestedVersion > 0 && msg.Detail.Version != m.requestedVersion) { m.detailLoading = false m.detailError = "Server returned a different pipeline or version than requested" return m, m.read() } m.detail = msg.Detail m.keys.detail = true m.detailLoading = false m.detailError = "" m.resetPipelineView() if m.cliRun && !m.cliStarted { updated, cmd := m.configureCLIRun() return updated, tea.Batch(m.read(), cmd) } } } else if msg.Type == protocol.PipelineList { m.pipelines = msg.Pipelines m.loaded = true if m.cliRun && !m.cliStarted && m.cliOptions.Pipeline != "" { for _, p := range m.pipelines { if p.Name == m.cliOptions.Pipeline { m.runPicker = false updated, send := m.requestPipeline(p.ID, p.Version) return updated, tea.Batch(m.read(), send) } } } } else { m.appendLine("unexpected response: " + msg.Type) } return m, m.read() case draftValidationMsg: m.appendNotices(msg.notices) if len(m.draftItems) > 0 && m.draftItems[m.draftIndex].Path == msg.path { m.draftError = "Validation passed (structure only)." if msg.err != nil { m.draftError = "Validation: " + msg.err.Error() } } return m, nil case sentMsg: // Responses are displayed by the independent read loop. case transferFinishedMsg: m.transferResult(msg) if msg.imported && m.conn != nil { return m, m.send(protocol.Message{Type: protocol.List}) } return m, nil case editedMsg: m.appendLine("Draft: " + msg.path) if msg.err != nil { m.appendLine("editor: " + msg.err.Error()) return m, nil } if m.conn != nil { m.editorCheckGeneration++ m.editorChecking = true m.confirmOpen = false m.keys.confirm = false m.submitNotice = "Checking whether edits were published…" return m, checkEditorPublication(m.ctx, m.url, msg, m.editorCheckGeneration) } if msg.unchanged { m.confirmOpen = false m.keys.confirm = false m.submitNotice = "No changes." m.appendLine("No changes; nothing to update.") return m, nil } // Editor completion is never approval to publish, for either new or existing drafts. m.pendingDraft = &msg m.confirmOpen = true m.keys.confirm = true m.submitNotice = "" return m, nil case editorPublicationMsg: return m.finishEditorPublication(msg) case errorMsg: if msg.conn != nil && msg.conn != m.conn { return m, nil } if msg.conn == nil && m.conn != nil { return m, nil } m.status = "Reconnecting…" if m.statusOpen { m.statusError = "Connection lost; reconnecting. Press r to refresh after reconnect." m.statusRequest = "" } m.appendLine("error: " + msg.err.Error()) m.detailRequest = "" m.detailLoading = m.keys.detail && m.requestedPipelineID > 0 m.submitID = "" if m.pendingDraft != nil { m.submitNotice = "Connection lost; submission outcome unknown. Draft retained." } if m.conn != nil { m.conn.CloseNow() m.conn = nil } m.reconnecting = true return m, tea.Tick(time.Second, func(time.Time) tea.Msg { return reconnectMsg{} }) case tea.KeyMsg: if msg.String() == "ctrl+c" { return m, tea.Quit } if m.actionConfirm != nil { return m.updateActionConfirm(msg) } if m.mutationCommand != nil { if msg.String() == "y" { command := m.mutationCommand m.mutationCommand = nil m.mutationTitle = "" return m, command() } if msg.String() == "n" || msg.String() == "esc" { m.mutationCommand = nil m.mutationTitle = "" } if msg.String() == "j" || msg.String() == "down" || msg.String() == "pgdown" { m.mutationTop++ } if msg.String() == "k" || msg.String() == "up" || msg.String() == "pgup" { m.mutationTop = max(0, m.mutationTop-1) } return m, nil } if m.editorChecking { return m, nil } if m.editorOpen { return m.updateEditorPicker(msg) } if m.statusOpen { return m.updateStatus(msg) } if m.logOpen { return m.updateLogs(msg) } if m.paletteOpen { return m.updatePalette(msg) } if m.confirmOpen { return m.updateConfirm(msg) } if m.draftOpen { return m.updateDrafts(msg) } if m.runPicker { return m.updateRunPicker(msg) } if m.runForm != 0 { return m.updateRunForm(msg) } if m.decisionOpen { return m.updateDecision(msg) } if m.artifactsOpen { return m.updateArtifacts(msg) } if m.runOpen && (msg.String() != ":" || m.runSearchOpen) { return m.updateRuns(msg) } if m.versionsOpen { return m.updateVersions(msg) } if m.templateOpen { return m.updateTemplates(msg) } switch { case m.homePage() && msg.String() == "r": m.closeRuns() m.runFailedOnly = false m.runOpen = true m.keys.runs = true m.runIndex = 0 return m.runRequest(protocol.Message{Type: protocol.ListRuns}) case m.homePage() && msg.String() == "d": return m.openDrafts() case m.homePage() && msg.String() == "b": return m.openTemplates() case key.Matches(msg, m.keys.commands), key.Matches(msg, m.keys.search): m.paletteOpen = true m.searchOpen = key.Matches(msg, m.keys.search) m.keys.palette = true m.commandIndex = 0 m.commandInput = newInput() if m.searchOpen { m.searchResults = nil m.searchText = "" m.searchError = "" m.commandInput.Placeholder = "Type to search…" return m, m.commandInput.Focus() } m.commandInput.Placeholder = ":logs" m.commandInput.SetValue(":") return m, m.commandInput.Focus() case m.homePage() && key.Matches(msg, m.keys.create): if m.conn == nil { m.appendLine("not connected") return m, nil } return m.newPipeline(pipeline.Template([]string{"worker"}, true)) case m.homePage() && key.Matches(msg, m.keys.ping): if m.conn == nil { m.appendLine("not connected") return m, nil } m.nextID++ id := strconv.FormatUint(m.nextID, 10) m.appendLine("ping #" + id) return m, func() tea.Msg { ctx, cancel := context.WithTimeout(m.ctx, 5*time.Second) defer cancel() if err := client.Send(ctx, m.conn, protocol.Message{Type: protocol.Ping, ID: id}); err != nil { return errorMsg{err: err, conn: m.conn} } return sentMsg{id} } case m.keys.detail: return m.updatePipeline(msg) case msg.String() == "j" || msg.String() == "down": m.homeTop = min(m.homeTop+1, max(0, len(m.homeLines())-max(1, m.height-4))) case msg.String() == "k" || msg.String() == "up": m.homeTop = max(0, m.homeTop-1) } } if m.runForm != 0 { var cmd tea.Cmd if m.runForm == 1 { m.runPrompt, cmd = m.runPrompt.Update(msg) return m, cmd } m.runInput, cmd = m.runInput.Update(msg) return m, cmd } if m.paletteOpen { var cmd tea.Cmd m.commandInput, cmd = m.commandInput.Update(msg) return m, cmd } if m.runPicker { var cmd tea.Cmd m.runPickerInput, cmd = m.runPickerInput.Update(msg) return m, cmd } return m, nil } func (m *Model) appendLine(line string) { follow := m.logOpen && m.logTop >= max(0, len(m.lines)-m.logPageSize()) for _, part := range strings.Split(line, "\n") { m.lines = append(m.lines, ansi.Strip(part)) m.logSeen = append(m.logSeen, false) m.unread++ } if m.logOpen { if follow { m.logTop = max(0, len(m.lines)-m.logPageSize()) } m.markVisibleLogs() } } func (m Model) View() string { connection := m.status if m.conn != nil { connection = "Connected · :status" if m.homePage() { connection = "Connected" } } if m.conn == nil { connection = m.loading(connection) } status := ansi.Truncate(connection, max(0, m.width-min(24, m.width/4)), "…") title := "" if titleWidth := m.width - lipgloss.Width(status) - 1; titleWidth > 0 { name := "Pipelines" if m.keys.detail && m.detail != nil { name = fmt.Sprintf("%s · v%d", m.detail.Name, m.detail.Version) } if m.detailLoading { name = "Loading pipeline" if m.requestedVersion > 0 { name = fmt.Sprintf("Loading v%d", m.requestedVersion) } } if m.runOpen { name = "Runs" if m.requestedRunID > 0 { name = fmt.Sprintf("Run #%d", m.requestedRunID) } } if m.templateOpen { name = "Templates" } if m.draftOpen { name = "Drafts" } if m.statusOpen { name = "Status" } title = ansi.Truncate(name, titleWidth, "…") } gap := max(0, m.width-lipgloss.Width(title)-lipgloss.Width(status)) headerLine := lipgloss.NewStyle().Bold(true).Foreground(themeAccent).Render(title) + strings.Repeat(" ", gap) + lipgloss.NewStyle().Foreground(themeMuted).Render(status) header := lipgloss.NewStyle().Background(themeSurface).Foreground(themeText). Width(m.width).Render(surfaceContent(headerLine, themeSurface)) + "\n" footer := "\n" + lipgloss.NewStyle().Width(m.width).Render(m.footerContent()) if m.height < 4 { return lipgloss.NewStyle().MaxHeight(m.height).Render(header + "\n" + footer) } bodyHeight := max(0, m.height-lipgloss.Height(header)-lipgloss.Height(footer)) lines := m.homeLines() homeLength := len(lines) top := m.homeTop top = min(top, max(0, len(lines)-bodyHeight)) lines = lines[top:min(len(lines), top+bodyHeight)] body := make([]string, bodyHeight) for i, line := range lines { body[i] = ansi.Truncate(line, m.width, "…") } if m.homePage() && len(body) > 0 && top+bodyHeight < homeLength { body[len(body)-1] = bar("↓ More below", fmt.Sprintf("%d–%d / %d", top+1, top+bodyHeight, homeLength), m.width, false) } parts := []string{header} if bodyHeight > 0 { content := strings.Join(body, "\n") if m.keys.detail { content = m.pipelineView(bodyHeight) } if m.runOpen { content = m.runsView(bodyHeight) } if m.templateOpen { content = m.templatesView(bodyHeight) } if m.draftOpen { content = m.draftsView(bodyHeight) } if m.runForm != 0 { content = m.runFormView(bodyHeight) } if m.runPicker { content = m.runPickerView(bodyHeight) } if m.logOpen { content = m.logsView(bodyHeight) } else if m.paletteOpen { content = m.paletteView(bodyHeight) } else if m.versionsOpen { content = m.versionsView(bodyHeight) } else if m.confirmOpen { content = m.confirmView(bodyHeight) } if m.editorOpen { content = m.editorPickerView(bodyHeight) } if m.statusOpen { content = m.statusView(bodyHeight) } if m.actionConfirm != nil { content = m.actionConfirmView(bodyHeight) } if m.decisionOpen { content = m.decisionView(bodyHeight) } if m.artifactsOpen { content = m.artifactsView(bodyHeight) } if m.mutationCommand != nil { paragraphs := []string{"This opens a new session/editor and may modify a retained draft.", "Published definitions and previous run history are unchanged."} if strings.Contains(m.mutationTitle, "debugging") { paragraphs = []string{"Opens the configured harness with the pinned definition and retained failure evidence.", "Debugging may propose or edit a retained draft. It does not retry the run or change published history."} } content = wrappedDialog(m.width, bodyHeight, 76, m.mutationTitle, paragraphs, "y confirm · n / Esc cancel", m.mutationTop) } if m.editorChecking { content = popup(m.width, bodyHeight, 76, []string{"Checking published edits", m.loading("Refreshing pipeline information…"), "Local draft remains retained."}) } parts = append(parts, content) } return strings.Join(append(parts, footer), "\n") }