Added ui:prompt-keypress to support single key presses
All checks were successful
ci / build (push) Successful in 3m41s

Have also fixed a bug in ui:prompt which was keeping the script running when the prompt was being cancelled
This commit is contained in:
Leon Mika 2025-10-26 07:34:14 +11:00
parent 022cec7393
commit 8dafa6fa8f
5 changed files with 122 additions and 18 deletions

View file

@ -2,6 +2,7 @@ package cmdpacks
import (
"context"
tea "github.com/charmbracelet/bubbletea"
"lmika.dev/cmd/dynamo-browse/internal/common/ui/commandctrl"
"lmika.dev/cmd/dynamo-browse/internal/common/ui/events"
@ -38,16 +39,26 @@ func (m *uiModule) uiPrompt(ctx context.Context, args ucl.CallArgs) (any, error)
}
resChan := make(chan string)
cancelChan := make(chan struct{})
go func() {
commandctrl.PostMsg(ctx, events.PromptForInput(prompt, nil, func(value string) tea.Msg {
resChan <- value
return nil
}))
commandctrl.PostMsg(ctx, events.PromptForInputMsg{
Prompt: prompt,
OnDone: func(value string) tea.Msg {
resChan <- value
return nil
},
OnCancel: func() tea.Msg {
cancelChan <- struct{}{}
return nil
},
})
}()
select {
case value := <-resChan:
return value, nil
case <-cancelChan:
return nil, nil
case <-ctx.Done():
return nil, ctx.Err()
}
@ -75,6 +86,38 @@ func (m *uiModule) uiConfirm(ctx context.Context, args ucl.CallArgs) (any, error
}
}
func (m *uiModule) uiInKey(ctx context.Context, args ucl.CallArgs) (any, error) {
var prompt string
if err := args.Bind(&prompt); err != nil {
return nil, err
}
resChan := make(chan string)
cancelChan := make(chan struct{})
go func() {
commandctrl.PostMsg(ctx, events.PromptForKeyMsg{
Prompt: prompt,
OnDone: func(value string) tea.Msg {
resChan <- value
return nil
},
OnCancel: func() tea.Msg {
cancelChan <- struct{}{}
return nil
},
})
}()
select {
case value := <-resChan:
return value, nil
case <-cancelChan:
return nil, nil
case <-ctx.Done():
return nil, ctx.Err()
}
}
func (m *uiModule) uiPromptTable(ctx context.Context, args ucl.CallArgs) (any, error) {
tables, err := m.tableService.ListTables(context.Background())
if err != nil {
@ -166,13 +209,14 @@ func moduleUI(
return ucl.Module{
Name: "ui",
Builtins: map[string]ucl.BuiltinHandler{
"command": m.uiCommand,
"prompt": m.uiPrompt,
"prompt-table": m.uiPromptTable,
"confirm": m.uiConfirm,
"query": m.uiQuery,
"filter": m.uiFilter,
"bind": m.uiBind,
"command": m.uiCommand,
"prompt": m.uiPrompt,
"prompt-table": m.uiPromptTable,
"prompt-keypress": m.uiInKey,
"confirm": m.uiConfirm,
"query": m.uiQuery,
"filter": m.uiFilter,
"bind": m.uiBind,
},
}, m.ckb
}

View file

@ -1,9 +1,10 @@
package events
import (
"log"
tea "github.com/charmbracelet/bubbletea"
"lmika.dev/cmd/dynamo-browse/internal/dynamo-browse/services"
"log"
)
func Error(err error) tea.Msg {
@ -31,10 +32,23 @@ func PromptForInput(prompt string, history services.HistoryProvider, onDone func
}
}
func PromptForKey(prompt string, onDone func(key string) tea.Msg) tea.Msg {
return PromptForKeyMsg{
Prompt: prompt,
OnDone: onDone,
}
}
func Confirm(prompt string, onResult func(yes bool) tea.Msg) tea.Msg {
return PromptForInput(prompt, nil, func(value string) tea.Msg {
return onResult(value == "y")
})
return PromptForInputMsg{
Prompt: prompt,
OnDone: func(value string) tea.Msg {
return onResult(value == "y")
},
OnCancel: func() tea.Msg {
return onResult(false)
},
}
}
func ConfirmYes(prompt string, onYes func() tea.Msg) tea.Msg {

View file

@ -27,3 +27,10 @@ type PromptForInputMsg struct {
OnCancel func() tea.Msg
OnTabComplete func(value string) (string, bool)
}
// PromptForKey indicates that the context is requesting a single key press
type PromptForKeyMsg struct {
Prompt string
OnDone func(key string) tea.Msg
OnCancel func() tea.Msg
}