package tui import ( "context" "errors" "sync" "charm.land/bubbles/v2/textinput" tea "charm.land/bubbletea/v2" "charm.land/lipgloss/v2" ) // ErrCancelled means the user closed a modal with esc. var ErrCancelled = errors.New("cancelled in the TUI") // Prompter asks the user in a modal, for session mode: one-time codes and // the session warnings of plugins. One modal shows at a time; other // questions wait for it. type Prompter struct { program *tea.Program mu sync.Mutex next int } func NewPrompter(p *tea.Program) *Prompter { return &Prompter{program: p} } // Prompt asks for text. A secret is masked as it is typed. func (pr *Prompter) Prompt(ctx context.Context, title, label string, secret bool) (string, error) { r, err := pr.ask(ctx, promptMsg{title: title, label: label, secret: secret}) if err != nil { return "", err } if !r.ok { return "", ErrCancelled } return r.text, nil } // Confirm shows message until the user accepts it with enter or turns it // down with esc. func (pr *Prompter) Confirm(ctx context.Context, title, message string) (bool, error) { r, err := pr.ask(ctx, promptMsg{title: title, label: message, confirm: true}) return r.ok, err } func (pr *Prompter) ask(ctx context.Context, msg promptMsg) (promptReply, error) { pr.mu.Lock() defer pr.mu.Unlock() pr.next++ reply := make(chan promptReply, 1) msg.id = pr.next msg.reply = reply pr.program.Send(msg) select { case r := <-reply: return r, nil case <-ctx.Done(): pr.program.Send(closePromptMsg{id: msg.id}) return promptReply{}, ctx.Err() } } type ( promptMsg struct { id int title string label string secret bool confirm bool reply chan<- promptReply } promptReply struct { text string ok bool } // closePromptMsg closes a modal whose question timed out. closePromptMsg struct{ id int } ) type modal struct { msg promptMsg input textinput.Model } func newModal(msg promptMsg) (*modal, tea.Cmd) { in := textinput.New() in.Prompt = "> " if msg.secret { in.EchoMode = textinput.EchoPassword } return &modal{msg: msg, input: in}, in.Focus() } // answer replies and closes the modal. func (m *Model) answer(r promptReply) { m.modal.msg.reply <- r m.modal = nil } func (m *Model) modalKey(msg tea.KeyPressMsg) tea.Cmd { switch msg.String() { case "ctrl+c": m.answer(promptReply{}) return tea.Quit case "esc": m.answer(promptReply{}) return nil case "enter": m.answer(promptReply{text: m.modal.input.Value(), ok: true}) return nil } if m.modal.msg.confirm { return nil } var cmd tea.Cmd m.modal.input, cmd = m.modal.input.Update(msg) return cmd } var modalBox = lipgloss.NewStyle().Border(lipgloss.RoundedBorder()).BorderForeground(lipgloss.Color("13")).Padding(1, 2) // modalView draws the modal in the middle of screen, which is the rest of // the view. func (m *Model) modalView(screen string) string { width := max(min(m.width-4, 72), 20) help := "enter send · esc cancel" if m.modal.msg.confirm { help = "enter continue · esc not now" } body := bold.Render(m.modal.msg.title) + "\n\n" + m.modal.msg.label if !m.modal.msg.confirm { m.modal.input.SetWidth(width - 10) body += "\n\n" + m.modal.input.View() } body += "\n\n" + faint.Render(help) box := modalBox.Width(width).Render(body) x := max((m.width-lipgloss.Width(box))/2, 0) y := max((m.height-lipgloss.Height(box))/2, 0) layers := lipgloss.NewCompositor(lipgloss.NewLayer(screen), lipgloss.NewLayer(box).X(x).Y(y).Z(1)) return lipgloss.NewCanvas(m.width, m.height).Compose(layers).Render() }