1package ui23import (4 tea "charm.land/bubbletea/v2"56 "src.rest/cat/012/internal/ui/choicebar"7)89// question is a choice bar (package choicebar) as the model asks it:10// a small question on the context line answered with a key, e.g.11// "You have unsaved changes. Enter Save and quit D Discard Esc Cancel".12type question struct {13 msg string14 warn bool // the message is a warning, e.g. about losing work15 desc string // more on the status line, when the question needs it16 choices []choice17}1819type choice struct {20 key string // as tea reports it, e.g. "enter" or "d"21 label string22 run func(m *Model) tea.Cmd23}2425// ask opens a choice bar with q's question.26func (m *Model) ask(q question) {27 chs := make([]choicebar.Choice, len(q.choices))28 for i, ch := range q.choices {29 chs[i] = choicebar.Choice{Key: ch.key, Label: ch.label, Run: func() tea.Cmd { return ch.run(m) }}30 }31 m.openOverlay(choicebar.New(m.host(), q.msg, q.warn, q.desc, chs))32}3334// quit exits, asking first when there are unsaved changes. Enter saves35// and quits, so the quick path never loses work. In a pipeline it sends36// a table instead, or asks which (quitPiped).37func (m *Model) quit() tea.Cmd {38 if m.pipe.on {39 return m.quitPiped()40 }41 if !m.changed || m.shared() { // the others keep the workbook open42 return m.exit()43 }44 m.ask(question{45 msg: "You have unsaved changes.",46 warn: true,47 choices: []choice{48 {key: "enter", label: "Save and quit", run: func(m *Model) tea.Cmd {49 m.quitAfterSave = true50 return m.save()51 }},52 {key: "d", label: "Discard", run: (*Model).exit},53 {key: "esc", label: "Cancel", run: func(*Model) tea.Cmd { return nil }},54 },55 })56 return nil57}