-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtui_dialog_field.go
164 lines (139 loc) · 3.55 KB
/
tui_dialog_field.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"github.com/andrianbdn/wg-cmd/theme"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type (
TuiDialogCancel struct{}
TuiDialogValue string
)
const (
tdfSelectionField = iota
tdfSelectionOK
tdfSelectionCancel
)
type TuiDialogField struct {
Title string
Question string
buttonOK1 string
buttonCancel2 string
ValidationFunc func(string) string
field textinput.Model
validationError string
selectedItem int
}
func NewTuiDialogName() TuiDialogField {
m := TuiDialogField{}
ti := textinput.New()
ti.Placeholder = ""
ti.CharLimit = 30
ti.Width = 50
ti.TextStyle = theme.Current.DialogInput
ti.Cursor.Style = theme.Current.DialogInputCursor
ti.Prompt = ""
ti.Focus()
ti.Cursor.Blink = true
m.field = ti
m.buttonOK1 = " OK "
m.buttonCancel2 = "Cancel"
return m
}
func (m TuiDialogField) Init() tea.Cmd {
return textinput.Blink
}
func (m TuiDialogField) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
if m.validationError != "" {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyEscape, tea.KeyEnter:
m.validationError = ""
return m, textinput.Blink
}
}
}
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyTab, tea.KeyDown, tea.KeyUp:
dir := 1
if msg.Type == tea.KeyUp {
dir = -1
}
m.selectedItem += dir
if m.selectedItem > tdfSelectionCancel {
m.selectedItem = tdfSelectionField
}
if m.selectedItem < tdfSelectionField {
m.selectedItem = tdfSelectionCancel
}
if m.selectedItem == tdfSelectionField {
m.field.Focus()
} else {
m.field.Blur()
}
case tea.KeyEscape, tea.KeyF3:
return m, func() tea.Msg {
return TuiDialogCancel{}
}
case tea.KeyEnter:
if m.selectedItem == tdfSelectionCancel {
return m, func() tea.Msg {
return TuiDialogCancel{}
}
}
m.validationError = m.ValidationFunc(m.field.Value())
if m.validationError == "" {
v := m.field.Value()
return m, func() tea.Msg {
return TuiDialogValue(v)
}
}
}
}
m.field, cmd = m.field.Update(msg)
return m, cmd
}
func (m TuiDialogField) View() string {
if m.validationError != "" {
return ErrorView("Error", m.validationError)
}
frameStyle := theme.Current.DialogBackground.Width(54).Padding(1, 2)
titleStyle := theme.Current.DialogTitle.Width(50).Align(0.5)
questionStyle := lipgloss.NewStyle().Width(50)
return frameStyle.Render(
lipgloss.JoinVertical(0,
titleStyle.Render(m.Title),
questionStyle.Render(m.Question),
m.field.View(),
m.RenderButtons(),
),
)
}
func (m TuiDialogField) RenderButtons() string {
bg := theme.Current.DialogBackground.Align(lipgloss.Center).Width(50)
renderButton := func(text string, hl bool) string {
firstLetter := text[0:1]
restText := text[1:]
var l, r string
var styleBtnText, styleBtnFirstLetter lipgloss.Style
if hl {
styleBtnText = theme.Current.DialogButtonFocus
styleBtnFirstLetter = theme.Current.DialogButtonFocusFirstLetter
l = "[<"
r = ">]"
} else {
styleBtnText = theme.Current.DialogBackground
styleBtnFirstLetter = theme.Current.DialogButtonFirstLetter
l = "[ "
r = " ]"
}
return styleBtnText.Render(l) + styleBtnFirstLetter.Render(firstLetter) + styleBtnText.Render(restText+r)
}
return bg.Render(renderButton(m.buttonOK1, m.selectedItem == tdfSelectionOK) +
theme.Current.DialogBackground.Render(" ") +
renderButton(m.buttonCancel2, m.selectedItem == tdfSelectionCancel))
}