-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
301 lines (271 loc) · 8.97 KB
/
main.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
"log"
"math"
"strings"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
confirmationmodal "github.com/evertonstz/go-workflows/components/confirmation_modal"
"github.com/evertonstz/go-workflows/components/list"
"github.com/evertonstz/go-workflows/components/notification"
"github.com/evertonstz/go-workflows/components/persist"
textarea "github.com/evertonstz/go-workflows/components/text_area"
"github.com/evertonstz/go-workflows/models"
"github.com/evertonstz/go-workflows/shared"
)
var (
leftPanelWidthPercentage = 0.5
leftPanelStyle = lipgloss.NewStyle().
AlignHorizontal(lipgloss.Left)
rightPanelStyle = lipgloss.NewStyle().
AlignHorizontal(lipgloss.Left).
PaddingTop(2).
Width(15).
Height(5)
helpPanelStyle = lipgloss.NewStyle().PaddingLeft(2)
notificationPanelStyle = lipgloss.NewStyle().
PaddingLeft(2).
Height(1).
AlignHorizontal(lipgloss.Left)
)
type (
termDimensions struct {
width int
height int
}
panelsStyle struct {
leftPanelStyle lipgloss.Style
rightPanelStyle lipgloss.Style
helpPanelStyle lipgloss.Style
notificationPanelStyle lipgloss.Style
}
keys struct {
listKeys list.KeyMap
}
model struct {
confirmationModal confirmationmodal.Model
keys keys
help help.Model
state sessionState
list list.Model
textArea textarea.Model
persistPath string
notification notification.Model
termDimensions termDimensions
currentHelpHeight int
panelsStyle panelsStyle
}
sessionState uint
)
const (
listView sessionState = iota
editView
confirmationModalView
)
func (m model) Init() tea.Cmd {
return persist.InitPersistionManagerCmd("go-workflows")
}
func (m model) focused() sessionState {
return m.state
}
func (m *model) changeFocus(v sessionState) sessionState {
m.state = v
switch v {
case listView:
m.textArea.SetEditing(false)
case editView:
m.textArea.SetEditing(true)
case confirmationModalView:
m.textArea.SetEditing(false)
}
return m.state
}
func (m *model) updatePanelSizes() {
currentNotificationHeight := m.panelsStyle.notificationPanelStyle.GetHeight()
m.currentHelpHeight = strings.Count(m.help.View(m.keys.listKeys), "\n") + 1
m.panelsStyle.leftPanelStyle = m.panelsStyle.leftPanelStyle.
Width(int(math.Floor(float64(m.termDimensions.width) * leftPanelWidthPercentage))).
Height(m.termDimensions.height - m.currentHelpHeight - currentNotificationHeight)
m.panelsStyle.rightPanelStyle = m.panelsStyle.rightPanelStyle.
Width(m.termDimensions.width - m.panelsStyle.leftPanelStyle.GetWidth() - 4).
Height(m.termDimensions.height - m.currentHelpHeight - currentNotificationHeight)
m.panelsStyle.helpPanelStyle = m.panelsStyle.helpPanelStyle.
Width(m.termDimensions.width).
Height(m.currentHelpHeight)
leftWidthFrameSize, leftHeightFrameSize := m.panelsStyle.leftPanelStyle.GetFrameSize()
rightWidthFrameSize, rightHeightFrameSize := m.panelsStyle.rightPanelStyle.GetFrameSize()
leftPanelWidth := m.panelsStyle.leftPanelStyle.GetWidth() - leftWidthFrameSize
rightPanelWidth := m.panelsStyle.rightPanelStyle.GetWidth() - rightWidthFrameSize
m.list.SetSize(leftPanelWidth, m.panelsStyle.leftPanelStyle.GetHeight()-leftHeightFrameSize)
m.textArea.SetSize(rightPanelWidth, m.panelsStyle.rightPanelStyle.GetHeight()-rightHeightFrameSize)
}
func (m *model) toggleHelpShowAll() {
m.help.ShowAll = !m.help.ShowAll
m.updatePanelSizes()
}
func (m *model) rebuildConfirmationModel(title string, confirm string, cancel string, confirmCmd tea.Cmd, cancelCmd tea.Cmd) {
m.confirmationModal = confirmationmodal.NewConfirmationModal(
title,
confirm,
cancel,
confirmCmd,
cancelCmd,
)
}
func (m model) persistItems() tea.Cmd {
var items []models.Item
for _, i := range m.list.AllItems() {
items = append(items, models.Item{
Title: i.Title(),
Desc: i.Description(),
Command: i.Command(),
DateAdded: i.DateAdded(),
DateUpdated: i.DateUpdated()})
}
data := models.Items{Items: items}
return persist.PersistListData(m.persistPath, data)
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
case shared.DidCloseConfirmationModalMsg:
m.changeFocus(listView)
case shared.DidDeleteItemMsg:
updatedListModel, cmd := m.list.Update(msg)
m.list = updatedListModel.(list.Model)
cmds = append(cmds, cmd)
m.changeFocus(listView)
cmds = append(cmds, m.persistItems())
return m, tea.Batch(cmds...)
case shared.CopiedToClipboardMsg:
return m, notification.ShowNotificationCmd("Copied to clipboard!")
case persist.PersistedFileMsg:
return m, notification.ShowNotificationCmd("Saved!")
case persist.InitiatedPersistion:
m.persistPath = msg.DataFile
return m, persist.LoadDataFileCmd(msg.DataFile)
case persist.LoadedDataFileMsg:
m.list.Update(msg)
case tea.WindowSizeMsg:
m.termDimensions.width = msg.Width
m.termDimensions.height = msg.Height
m.updatePanelSizes()
case shared.DidUpdateItemMsg:
m.list.Update(msg)
return m, m.persistItems()
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keys.listKeys.Delete):
m.rebuildConfirmationModel("Are you sure you want to delete this workflow?",
"Yes",
"No",
shared.DeleteCurrentItemCmd(m.list.CurrentItemIndex()),
shared.CloseConfirmationModalCmd())
m.changeFocus(confirmationModalView)
case key.Matches(msg, m.keys.listKeys.Help):
m.toggleHelpShowAll()
case key.Matches(msg, m.keys.listKeys.Quit):
return m, tea.Quit
case key.Matches(msg, m.keys.listKeys.Esc):
if m.state == editView {
m.changeFocus(listView)
}
r, _ := m.list.Update(msg)
m.list = r.(list.Model)
return m, nil
case key.Matches(msg, m.keys.listKeys.Enter):
if m.focused() == listView && !m.list.InputOn() {
var c tea.Cmd
m.changeFocus(editView)
updatedListModel, c := m.list.Update(msg)
m.list = updatedListModel.(list.Model)
cmds = append(cmds, c)
updatedTextAreaModel, c := m.textArea.Update(msg)
m.textArea = updatedTextAreaModel.(textarea.Model)
cmds = append(cmds, c)
return m, tea.Batch(cmds...)
} else if m.focused() == listView && m.list.InputOn() {
var c tea.Cmd
_, c = m.list.Update(msg)
return m, tea.Batch(c)
}
default:
if m.help.ShowAll {
m.toggleHelpShowAll()
}
}
}
switch m.focused() {
case listView:
var c tea.Cmd
var updatedListAreaModel tea.Model
var updatedTextAreaModel tea.Model
updatedListAreaModel, c = m.list.Update(msg)
m.list = updatedListAreaModel.(list.Model)
cmds = append(cmds, c)
updatedTextAreaModel, c = m.textArea.Update(msg)
m.textArea = updatedTextAreaModel.(textarea.Model)
cmds = append(cmds, c)
case editView:
var c tea.Cmd
var updatedTextAreaModel tea.Model
updatedTextAreaModel, c = m.textArea.Update(msg)
m.textArea = updatedTextAreaModel.(textarea.Model)
cmds = append(cmds, c)
case confirmationModalView:
var c tea.Cmd
var updatedConfirmationModalModel tea.Model
updatedConfirmationModalModel, c = m.confirmationModal.Update(msg)
m.confirmationModal = updatedConfirmationModalModel.(confirmationmodal.Model)
cmds = append(cmds, c)
}
notfyModel, notfyMsg := m.notification.Update(msg)
cmds = append(cmds, notfyMsg)
m.notification = notfyModel
return m, tea.Batch(cmds...)
}
func (m model) View() string {
var mainContent string
if m.focused() == listView {
mainContent = lipgloss.JoinHorizontal(lipgloss.Bottom,
m.panelsStyle.leftPanelStyle.Render(m.list.View()),
m.panelsStyle.rightPanelStyle.Render(m.textArea.View()))
} else if m.focused() == editView {
mainContent = lipgloss.JoinHorizontal(lipgloss.Bottom,
m.panelsStyle.leftPanelStyle.Faint(true).Render(m.list.View()),
m.panelsStyle.rightPanelStyle.Render(m.textArea.View()))
} else if m.focused() == confirmationModalView {
mainContent = lipgloss.JoinHorizontal(lipgloss.Bottom,
m.panelsStyle.leftPanelStyle.Faint(true).Render(m.list.View()),
m.panelsStyle.rightPanelStyle.Render(m.confirmationModal.View()))
}
return lipgloss.JoinVertical(lipgloss.Left,
m.panelsStyle.notificationPanelStyle.Render(m.notification.View()),
mainContent,
m.panelsStyle.helpPanelStyle.Render(m.help.View(m.keys.listKeys)))
}
func new() model {
return model{
confirmationModal: confirmationmodal.NewConfirmationModal("", "", "", nil, nil),
keys: keys{listKeys: list.Keys},
help: help.New(),
list: list.New(),
textArea: textarea.New(),
notification: notification.New("Workflows"),
panelsStyle: panelsStyle{
leftPanelStyle: leftPanelStyle,
rightPanelStyle: rightPanelStyle,
helpPanelStyle: helpPanelStyle,
notificationPanelStyle: notificationPanelStyle,
},
currentHelpHeight: 0,
}
}
func main() {
p := tea.NewProgram(new(), tea.WithAltScreen())
if _, err := p.Run(); err != nil {
log.Fatalf("Error starting app: %v", err)
}
}