Files
dibs/internal/tui/prompt.go
T
OverlordandClaude Bot 15a34e1f84 Add session mode: signing in and adding to the cart.
With cart_mode = "session", dibs signs in to the shop's account and puts
an item in the cart when it fires. The account's cookies are kept apart
from the watcher's and stored encrypted, with the key in the OS keyring.
Without a keyring, they're stored in a file only you can read.

An alert now goes out right away, without a cart link, and a second one
follows once the item is in the cart. If adding fails, the second alert
carries the cart link instead.

Amazon only accepts additions its page scripts have seen, so plugins can
now press a button in headless Chrome with ctx:click.

The TUI asks for one-time codes and shows the plugin's warning about
account locks in a modal. 'dibs login' signs in from a terminal, or
with --browser in a Chrome window, and 'dibs logout' deletes the cookies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 17:26:26 +02:00

147 lines
3.5 KiB
Go

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()
}