-
Notifications
You must be signed in to change notification settings - Fork 29
/
input_model.go
71 lines (61 loc) · 1.63 KB
/
input_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
69
70
71
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 StandaloneInputModel struct {
tea.Model
value string
pristine bool
log *zap.Logger
}
func NewStandaloneInputModel(
inputParams prompt.InputParams,
keyMap *KeyMap,
styles *Styles,
opts ...Option,
) StandaloneInputModel {
return StandaloneInputModel{
Model: NewModel(
inputWrapModel{prompt.NewInputModel(inputParams)},
keyMap,
styles,
append([]Option{WithoutHelp()}, opts...)...,
),
pristine: true,
log: log.Get().Named("renderer.StandaloneInputModel"),
}
}
func (m StandaloneInputModel) Value() (string, bool) { return m.value, !m.pristine }
func (m StandaloneInputModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { // revive:disable-line
case prompt.Done:
m.log.Debug("finished writing content", zap.Int("len", len(msg.Value)))
m.pristine = !m.pristine
m.value = msg.Value
return m, tea.Quit
}
var cmd tea.Cmd
m.Model, cmd = m.Model.Update(msg)
return m, cmd
}
// inputWrapModel is a wrapper for prompt.InputModel
// to make it adherent to tea.Model interface.
type inputWrapModel struct {
prompt.InputModel
}
func (m inputWrapModel) Init() tea.Cmd {
var cmds []tea.Cmd
cmds = append(cmds, m.InputModel.Init(), m.InputModel.Focus())
return tea.Batch(cmds...)
}
func (m inputWrapModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
m.InputModel, cmd = m.InputModel.Update(msg)
return m, cmd
}
func (m inputWrapModel) View() string {
return m.InputModel.View() + "\n"
}