prompt.go

 1package plugin
 2
 3import (
 4	"log"
 5
 6	lua "github.com/yuin/gopher-lua"
 7)
 8
 9// PendingPrompt holds the state for a plugin-requested user input prompt.
10type PendingPrompt struct {
11	Placeholder string
12	callback    *lua.LFunction
13}
14
15// luaPrompt implements matcha.prompt(placeholder, callback).
16// It requests a text input overlay in the TUI. When the user submits,
17// the callback is called with their input string.
18func (m *Manager) luaPrompt(L *lua.LState) int { //nolint:gocritic
19	placeholder := L.CheckString(1)
20	fn := L.CheckFunction(2)
21
22	m.pendingPrompt = &PendingPrompt{
23		Placeholder: placeholder,
24		callback:    fn,
25	}
26	return 0
27}
28
29// TakePendingPrompt returns and clears any pending prompt request.
30func (m *Manager) TakePendingPrompt() (*PendingPrompt, bool) {
31	if m.pendingPrompt == nil {
32		return nil, false
33	}
34	p := m.pendingPrompt
35	m.pendingPrompt = nil
36	return p, true
37}
38
39// ResolvePrompt calls the stored prompt callback with the user's input.
40func (m *Manager) ResolvePrompt(prompt *PendingPrompt, input string) {
41	if prompt == nil || prompt.callback == nil {
42		return
43	}
44	if err := m.state.CallByParam(lua.P{
45		Fn:      prompt.callback,
46		NRet:    0,
47		Protect: true,
48	}, lua.LString(input)); err != nil {
49		log.Printf("plugin prompt callback error: %v", err)
50	}
51}