-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.go
78 lines (64 loc) · 1.38 KB
/
input.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
72
73
74
75
76
77
78
package main
import (
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type Input int
type inputModel struct {
textInput textinput.Model
isActive bool
id Input
}
func (m inputModel) Init() tea.Cmd {
return textinput.Blink
}
func (m inputModel) Update(msg tea.Msg) (inputModel, tea.Cmd) {
var cmd tea.Cmd
m.textInput, cmd = m.textInput.Update(msg)
if cmd != nil {
return m, cmd
}
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
return m, tea.Quit
case "enter":
m.isActive = false
return m, m.doneInput
case "esc":
m.isActive = false
return m, m.cancelInput
}
}
return m, nil
}
func (m inputModel) View() string {
return m.textInput.View()
}
func InitInput(id Input, input, de, placeholder string) inputModel {
ti := textinput.New()
ti.Prompt = input + lipgloss.NewStyle().Foreground(lipgloss.Color("3")).Render(" ? ")
ti.Placeholder = placeholder
ti.SetValue(de)
ti.Focus()
ti.CharLimit = 33
ti.Width = 33
return inputModel{
id: id,
textInput: ti,
isActive: true,
}
}
type inputDoneMsg struct {
id Input
value string
}
func (m inputModel) doneInput() tea.Msg {
return inputDoneMsg{m.id, m.textInput.Value()}
}
type inputCancelMsg struct{}
func (m inputModel) cancelInput() tea.Msg {
return inputCancelMsg{}
}