-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_connection.go
242 lines (201 loc) · 4.99 KB
/
new_connection.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"fmt"
"strings"
"github.com/charmbracelet/bubbles/cursor"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
)
type Action string
const (
SUBMIT Action = "SUBMIT"
TEST Action = "TEST"
CANCEL Action = "CANCEL"
)
type TestStatus string
const (
PASSED TestStatus = "PASSED"
FAILED TestStatus = "FAILED"
NA TestStatus = "NA"
)
var (
focusedButton = focusedStyle.Copy().Render("[ Submit ]")
blurredButton = fmt.Sprintf("[ %s ]", blurredStyle.Render("Submit"))
focusedTestButton = focusedStyle.Copy().Render("[ Test ]")
blurredTestButton = fmt.Sprintf("[ %s ]", blurredStyle.Render("Test"))
errorTestButton = fmt.Sprintf("[ %s ]", errorStyle.Render("Test"))
successTestButton = fmt.Sprintf("[ %s ]", successStyle.Render("Test"))
)
type NewConnectionModel struct {
focusIndex int
inputs []textinput.Model
cursorMode cursor.Mode
connection Connection
testStatus TestStatus
action Action
}
func InitialNewConnectionModel() NewConnectionModel {
var newConnectionInputs = []string{
"Host",
"Port",
"User",
"Pass",
"Database",
"Name",
}
m := NewConnectionModel{
inputs: make([]textinput.Model, len(newConnectionInputs)),
action: SUBMIT,
testStatus: NA,
}
var t textinput.Model
for i, value := range newConnectionInputs {
t = textinput.New()
t.Cursor.Style = cursorStyle
t.CharLimit = 32
t.Placeholder = value
if i == 0 {
t.Focus()
t.PromptStyle = focusedItemStyle
t.TextStyle = focusedItemStyle
}
if value == "Pass" {
t.EchoMode = textinput.EchoPassword
t.EchoCharacter = '•'
}
m.inputs[i] = t
}
return m
}
func (m NewConnectionModel) Init() tea.Cmd {
return textinput.Blink
}
func (m NewConnectionModel) Update(msg tea.Msg) (NewConnectionModel, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "esc":
m.action = CANCEL
return m, nil
// Change cursor mode
case "ctrl+r":
m.cursorMode++
if m.cursorMode > cursor.CursorHide {
m.cursorMode = cursor.CursorBlink
}
cmds := make([]tea.Cmd, len(m.inputs))
for i := range m.inputs {
cmds[i] = m.inputs[i].Cursor.SetMode(m.cursorMode)
}
return m, tea.Batch(cmds...)
// Handle button actions
case "left", "right":
if m.focusIndex == len(m.inputs) {
if m.action == SUBMIT {
m.action = TEST
} else {
m.action = SUBMIT
}
}
if m.testStatus != NA {
m.testStatus = NA
}
case "enter":
if m.focusIndex == len(m.inputs) {
conn := Connection{
Host: m.inputs[0].Value(),
Port: m.inputs[1].Value(),
User: m.inputs[2].Value(),
Pass: m.inputs[3].Value(),
Database: m.inputs[4].Value(),
Name: m.inputs[5].Value(),
status: DISCONNECTED,
}
switch m.action {
case SUBMIT:
if conn.TestConnection() == PASSED {
m.connection = conn
}
case TEST:
if m.testStatus == NA {
m.testStatus = conn.TestConnection()
}
}
}
return m.updateInputStates()
// Set focus to next input
case "tab", "shift+tab", "up", "down":
s := msg.String()
// Cycle indexes
if s == "up" || s == "shift+tab" {
m.focusIndex--
} else {
m.focusIndex++
}
if m.focusIndex > len(m.inputs) {
m.focusIndex = 0
} else if m.focusIndex < 0 {
m.focusIndex = len(m.inputs)
}
return m.updateInputStates()
}
}
// Handle character input and blinking
cmd := m.updateInputs(msg)
return m, cmd
}
func (m *NewConnectionModel) updateInputStates() (NewConnectionModel, tea.Cmd) {
cmds := make([]tea.Cmd, len(m.inputs))
for i := 0; i <= len(m.inputs)-1; i++ {
if i == m.focusIndex {
// Set focused state
cmds[i] = m.inputs[i].Focus()
m.inputs[i].PromptStyle = focusedItemStyle
m.inputs[i].TextStyle = focusedItemStyle
continue
}
// Remove focused state
m.inputs[i].Blur()
m.inputs[i].PromptStyle = noStyle
m.inputs[i].TextStyle = noStyle
}
return *m, tea.Batch(cmds...)
}
func (m *NewConnectionModel) updateInputs(msg tea.Msg) tea.Cmd {
cmds := make([]tea.Cmd, len(m.inputs))
// Only text inputs with Focus() set will respond, so it's safe to simply
// update all of them here without any further logic.
for i := range m.inputs {
m.inputs[i], cmds[i] = m.inputs[i].Update(msg)
}
return tea.Batch(cmds...)
}
func (m NewConnectionModel) View() string {
var b strings.Builder
b.WriteString("New Connection\n\n")
for i := range m.inputs {
b.WriteString(m.inputs[i].View())
if i < len(m.inputs)-1 {
b.WriteRune('\n')
}
}
submitButton := &blurredButton
testButton := &blurredTestButton
if m.focusIndex == len(m.inputs) {
switch m.action {
case SUBMIT:
submitButton = &focusedButton
case TEST:
switch m.testStatus {
case PASSED:
testButton = &successTestButton
case FAILED:
testButton = &errorTestButton
case NA:
testButton = &focusedTestButton
}
}
}
fmt.Fprintf(&b, "\n\n%s%s\n\n", *submitButton, *testButton)
return paginationStyle.Render(b.String())
}