-
Notifications
You must be signed in to change notification settings - Fork 30
/
question_model.go
68 lines (58 loc) · 1.59 KB
/
question_model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package tui
import (
tea "github.com/charmbracelet/bubbletea"
"github.com/stateful/runme/v3/internal/log"
"github.com/stateful/runme/v3/internal/tui/prompt"
"go.uber.org/zap"
)
type StandaloneQuestionModel struct {
tea.Model
confirmed bool
log *zap.Logger
}
func NewStandaloneQuestionModel(
text string,
keyMap *KeyMap,
styles *Styles,
opts ...Option,
) StandaloneQuestionModel {
return StandaloneQuestionModel{
Model: NewModel(
questionWrapModel{prompt.NewQuestionModel(text)},
keyMap,
styles,
append([]Option{WithoutHelp()}, opts...)...,
),
log: log.Get().Named("renderer.StandaloneQuestionModel"),
}
}
func (m StandaloneQuestionModel) Confirmed() bool { return m.confirmed }
func (m StandaloneQuestionModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { // revive:disable-line
case prompt.Confirmed:
m.log.Debug("confirmed message received", zap.Bool("value", msg.Value))
m.confirmed = msg.Value
return m, tea.Quit
}
var cmd tea.Cmd
m.Model, cmd = m.Model.Update(msg)
return m, cmd
}
// questionWrapModel is a wrapper for prompt.Model
// to make it adherent to tea.Model interface.
type questionWrapModel struct {
prompt.QuestionModel
}
func (m questionWrapModel) Init() tea.Cmd {
var cmds []tea.Cmd
cmds = append(cmds, m.QuestionModel.Init(), m.QuestionModel.Focus())
return tea.Batch(cmds...)
}
func (m questionWrapModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
m.QuestionModel, cmd = m.QuestionModel.Update(msg)
return m, cmd
}
func (m questionWrapModel) View() string {
return m.QuestionModel.View() + "\n"
}