package tui import ( "cmp" "fmt" "slices" "strings" "albedo/cli/internal/daemon" tea "charm.land/bubbletea/v2" "github.com/charmbracelet/x/ansi" ) // checklistKey settles a key on the checklist: ↑↓ moves between finishing // and later, enter does the highlighted one, esc is later. func (q *cdQuestion) checklistKey(msg tea.KeyPressMsg) cdChoice { switch msg.String() { case "up", "down", "ctrl+p", "ctrl+n", "tab", "shift+tab": q.later = !q.later case "enter": if q.later { return cdLater } return cdFinish case "esc", "ctrl+c": return cdLater } return cdNothing } // tried says a finish was attempted and did not complete, so each binding // shows how it went. func (q *cdQuestion) tried() bool { if q.finishFailure() != "" { return true } for _, b := range q.move.Bindings { if b.Status != "" && b.Status != "pending" { return true } } return false } // checklistView draws the box for a move in its checklist: what finishing // disarms here, then finish or later. func (q *cdQuestion) checklistView(width, height int) []string { outer := cdBoxWidth(width) if width >= 80 { outer = 76 // room for a webhook's address on one line } inner := outer - 6 host := q.host() face := DefaultStyles.Agent.Render(connectingFace(host, q.frame)) var head, tail []string wrap := func(rows *[]string, style func(...string) string, text string) { for line := range strings.SplitSeq(ansi.Wrap(text, inner, " "), "\n") { *rows = append(*rows, style(line)) } } head = append(head, "") if _, path := daemon.SplitLocation(q.target); path != "" { wrap(&head, DefaultStyles.Muted.Render, q.target) } var keys []hint var bindings, compact [][]string switch q.phase { case cdMoving: head = append(head, DefaultStyles.Muted.Render("reading the move to "+host)+" "+face) keys = []hint{{"esc", "hide"}} case cdFailed: wrap(&head, DefaultStyles.Error.Render, q.failure) keys = []hint{{"esc", "close"}, {"enter", "retry"}} default: intro := "Live on " + host + " now; this copy still runs until the move finishes." if len(q.move.Bindings) == 0 { intro += " Nothing here fires on its own." } else { intro += " Finishing disarms what still fires here:" } wrap(&head, unstyled, intro) head = append(head, "") for _, b := range q.listedBindings() { bindings = append(bindings, q.bindingRows(b, inner, false)) compact = append(compact, q.bindingRows(b, inner, true)) } if len(bindings) > 0 { tail = append(tail, "") } if failure := q.finishFailure(); failure != "" { wrap(&tail, DefaultStyles.Error.Render, failure) tail = append(tail, "") } tail, keys = q.checklistChoices(tail, face) } tail = append(tail, "") hints := cdKeys(keys) tail = append(tail, strings.Repeat(" ", max(0, inner-ansi.StringWidth(hints)))+hints, "") room := height - 2 - len(head) - len(tail) if len(slices.Concat(bindings...)) > room { bindings = compact } return cdFrame("/cd", append(append(head, fitBindings(bindings, room)...), tail...), outer) } // listedBindings are the move's bindings in the order the checklist lists // them: once a finish was tried, the failed ones first, so a short screen // still shows them. func (q *cdQuestion) listedBindings() []daemon.MoveBinding { bindings := q.move.Bindings if q.tried() { bindings = slices.Clone(bindings) slices.SortStableFunc(bindings, func(a, b daemon.MoveBinding) int { return cmp.Compare(bindingRank(a.Status), bindingRank(b.Status)) }) } return bindings } func bindingRank(status string) int { switch status { case "failed": return 0 case "disarmed": return 2 } return 1 } // finishFailure is why the last finish did not complete, if it did not. func (q *cdQuestion) finishFailure() string { return cmp.Or(q.failure, q.move.Error) } // checklistChoices adds the two lines, or what replaced them, to rows and // returns them with the keys under the box. func (q *cdQuestion) checklistChoices(rows []string, face string) ([]string, []hint) { switch q.phase { case cdFinishing: return append(rows, DefaultStyles.Muted.Render("disarming and finishing")+" "+face), []hint{{"esc", "hide"}} case cdAttaching: return append(rows, DefaultStyles.Success.Render("moved")+DefaultStyles.Muted.Render(" · opening it on "+q.host())+" "+face), []hint{{"esc", "hide"}} } finish, verb := "disarm all and finish the move", "finish" switch { case q.tried(): finish, verb = "retry: disarm the rest and finish", "retry" case len(q.move.Bindings) == 0: finish = "finish the move" } line := func(selected bool, label string) string { if selected { return DefaultStyles.Prompt.Render("› " + label) } return DefaultStyles.Muted.Render(" " + label) } rows = append(rows, line(!q.later, finish), line(q.later, "later")) if q.later { verb = "later" } return rows, []hint{{"esc", "later"}, {"enter", verb}} } // bindingRows is one binding as the checklist lists it: its kind and label, // what it does, a webhook's old and new address, and once a finish was // tried, whether it was disarmed. compact leaves only the kind and label. func (q *cdQuestion) bindingRows(b daemon.MoveBinding, inner int, compact bool) []string { mark := DefaultStyles.Faint.Render("· ") if q.tried() { switch b.Status { case "disarmed": mark = DefaultStyles.Success.Render("✓ ") case "failed": mark = DefaultStyles.Error.Render("✗ ") } } const indent = 12 // the mark, the kind and a gap pad := strings.Repeat(" ", indent) var rows []string hang := func(first string, style func(...string) string, text string) { for i, line := range strings.Split(ansi.Wrap(text, max(1, inner-indent), " "), "\n") { lead := pad if i == 0 { lead = first } rows = append(rows, lead+style(line)) } } hang(mark+DefaultStyles.Muted.Render(fmt.Sprintf("%-10s", b.Kind)), unstyled, b.Label) if compact { return rows } if b.Detail != "" && b.Detail != b.Label { hang(pad, DefaultStyles.Faint.Render, b.Detail) } switch { case b.NewURL != "": hang(pad, DefaultStyles.Faint.Render, "re-point its sender") if b.OldURL != "" { hang(pad, DefaultStyles.Faint.Render, "from "+b.OldURL) } hang(pad, unstyled, "to "+b.NewURL) case b.OldURL != "": hang(pad, DefaultStyles.Faint.Render, "senders post to "+b.OldURL) } if b.Status == "failed" && b.Error != "" { hang(pad, DefaultStyles.Error.Render, b.Error) } return rows } // unstyled is text in the terminal's own ink, for where a style is asked for. func unstyled(s ...string) string { return strings.Join(s, "") } // fitBindings lays the bindings' rows into room rows, whole bindings only, // and says how many more there are when some do not fit. func fitBindings(bindings [][]string, room int) []string { var rows []string for i, b := range bindings { left := len(bindings) - i - 1 need := len(b) if left > 0 { need++ // the line saying how many more } if len(rows)+need > room { return append(rows, DefaultStyles.Faint.Render(fmt.Sprintf(" … and %d more", len(bindings)-i))) } rows = append(rows, b...) } return rows }