-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
724 lines (627 loc) · 21 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
package main
import (
_ "embed"
"html/template"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"sigs.k8s.io/yaml"
)
const (
mainPage = "*main*"
newNetworkPage = "*new_network*"
splitNetworkPage = "*split_network*"
summarizeNetworkPage = "*summarize_network*"
allocateNetworkSubnetsModePage = "*allocate_network_subnets_mode*"
allocateNetworkHostsModePage = "*allocate_network_hosts_mode*"
updateNetworkAllocationPage = "*update_network_allocation*"
deallocateNetworkPage = "*deallocate_network*"
deleteNetworkPage = "*delete_network*"
quitPage = "*quit*"
dataDirName = ".ez-ipam"
networksDirName = "networks"
markdownFileName = "EZ-IPAM.md"
)
var (
//go:embed markdown.tmpl
markdownTmpl string
app *tview.Application
pages *tview.Pages
positionLine *tview.TextView
navigationPanel *tview.List
detailsPanel *tview.TextView
statusLine *tview.TextView
keysLine *tview.TextView
newNetworkDialog *tview.Form
splitNetworkDialog *tview.Form
summarizeNetworkDialog *tview.Modal
allocateNetworkSubnetsModeDialog *tview.Form
allocateNetworkHostsModeDialog *tview.Form
updateNetworkAllocationDialog *tview.Form
deallocateNetworkDialog *tview.Modal
deleteNetworkDialog *tview.Modal
quitDialog *tview.Modal
)
func main() {
{
app = tview.NewApplication()
pages = tview.NewPages()
rootFlex := tview.NewFlex().SetDirection(tview.FlexRow)
positionLine = tview.NewTextView()
positionLine.SetBorder(true)
positionLine.SetTitle("Navigation")
positionLine.SetText("Home")
rootFlex.AddItem(positionLine, 3, 1, false)
middleFlex := tview.NewFlex().SetDirection(tview.FlexColumn)
rootFlex.AddItem(middleFlex, 0, 2, false)
navigationPanel = tview.NewList()
navigationPanel.ShowSecondaryText(false)
navigationPanel.SetBorder(true).SetTitle("Menu")
middleFlex.AddItem(navigationPanel, 0, 1, false)
detailsFlex := tview.NewFlex().SetDirection(tview.FlexRow)
middleFlex.AddItem(detailsFlex, 0, 2, false)
detailsPanel = tview.NewTextView()
detailsPanel.SetBorder(true).SetTitle("Details")
detailsFlex.AddItem(detailsPanel, 0, 1, false)
keysLine = tview.NewTextView()
keysLine.SetBorder(false)
updateKeysLine()
rootFlex.AddItem(keysLine, 1, 1, false)
statusLine = tview.NewTextView()
statusLine.SetBorder(true)
statusLine.SetTitle("Status")
detailsFlex.AddItem(statusLine, 3, 1, false)
pages.AddPage(mainPage, rootFlex, true, true)
}
positionLine.SetFocusFunc(func() {
app.SetFocus(navigationPanel)
})
detailsPanel.SetFocusFunc(func() {
app.SetFocus(navigationPanel)
})
statusLine.SetFocusFunc(func() {
app.SetFocus(navigationPanel)
})
navigationPanel.SetChangedFunc(func(index int, mainText, secondaryText string, shortcut rune) {
selected := menuItems.GetByParentAndID(currentMenuItem, mainText)
if selected == nil {
panic("Failed to find currently changed menu item!")
}
currentMenuFocus = selected
currentMenuFocus.OnChangedFunc()
updateKeysLine()
})
navigationPanel.SetSelectedFunc(func(index int, mainText, secondaryText string, shortcut rune) {
selected := menuItems.GetByParentAndID(currentMenuItem, mainText)
if selected == nil {
panic("Failed to find currently selected menu item!")
}
oldMenuItem := currentMenuItem
currentMenuItem = selected
reloadMenu(oldMenuItem)
currentMenuItem.OnSelectedFunc()
updateKeysLine()
})
navigationPanel.SetDoneFunc(func() {
if currentMenuItem == nil {
return
}
currentMenuItem.OnDoneFunc()
oldMenuItem := currentMenuItem
currentMenuItem = currentMenuItem.GetParent()
reloadMenu(oldMenuItem)
if currentMenuItem == nil {
positionLine.Clear()
positionLine.SetText("Home")
} else {
currentMenuItem.OnSelectedFunc()
}
updateKeysLine()
})
navigationPanel.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyBS, tcell.KeyBackspace2:
return tcell.NewEventKey(tcell.KeyEscape, 0, tcell.ModNone)
case tcell.KeyCtrlU:
return tcell.NewEventKey(tcell.KeyPgUp, 0, tcell.ModNone)
case tcell.KeyCtrlD:
return tcell.NewEventKey(tcell.KeyPgDn, 0, tcell.ModNone)
case tcell.KeyRune:
switch event.Rune() {
case 'h':
return tcell.NewEventKey(tcell.KeyLeft, tcell.RuneLArrow, tcell.ModNone)
case 'j':
return tcell.NewEventKey(tcell.KeyDown, tcell.RuneDArrow, tcell.ModNone)
case 'k':
return tcell.NewEventKey(tcell.KeyUp, tcell.RuneUArrow, tcell.ModNone)
case 'l':
return tcell.NewEventKey(tcell.KeyRight, tcell.RuneRArrow, tcell.ModNone)
case 'q':
pages.ShowPage(quitPage)
app.SetFocus(quitDialog)
return nil
}
}
if currentMenuItem != nil {
if e := currentMenuItem.CurrentMenuInputCapture(event); e != event {
return e
}
}
if currentMenuFocus != nil {
if e := currentMenuFocus.CurrentFocusInputCapture(event); e != event {
return e
}
}
return event
})
{
height := 7
width := 51
newNetworkDialog = tview.NewForm().SetButtonsAlign(tview.AlignCenter).
AddInputField("CIDR", "", 42, nil, nil).
AddButton("Save", func() {
AddNewNetwork(getAndClearTextFromInputField(newNetworkDialog, "CIDR"))
pages.SwitchToPage(mainPage)
app.SetFocus(navigationPanel)
}).
AddButton("Cancel", func() {
getAndClearTextFromInputField(newNetworkDialog, "CIDR")
pages.SwitchToPage(mainPage)
app.SetFocus(navigationPanel)
})
newNetworkDialog.SetBorder(true).SetTitle("New Network")
newNetworkDialogFlex := tview.NewFlex().SetDirection(tview.FlexColumn).
AddItem(nil, 0, 1, false).
AddItem(
tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(nil, 0, 1, false).
AddItem(newNetworkDialog, height, 1, false).
AddItem(nil, 0, 1, false),
width, 1, false).
AddItem(nil, 0, 1, false)
pages.AddPage(newNetworkPage, newNetworkDialogFlex, true, false)
}
{
height := 7
width := 66
splitNetworkDialog = tview.NewForm().SetButtonsAlign(tview.AlignCenter).
AddInputField("New Networks Prefix", "", 42, nil, nil).
AddButton("Save", func() {
newPrefix := getAndClearTextFromInputField(splitNetworkDialog, "New Networks Prefix")
newPrefix = strings.TrimLeft(newPrefix, "/")
newPrefixInt, err := strconv.Atoi(newPrefix)
if err != nil {
statusLine.Clear()
statusLine.SetText("Invalid new prefix, should be a number representing smaller networks than this parent " + err.Error())
}
SplitNetwork(newPrefixInt)
pages.SwitchToPage(mainPage)
app.SetFocus(navigationPanel)
}).
AddButton("Cancel", func() {
getAndClearTextFromInputField(splitNetworkDialog, "New Networks Prefix")
pages.SwitchToPage(mainPage)
app.SetFocus(navigationPanel)
})
splitNetworkDialog.SetBorder(true)
splitNetworkDialogFlex := tview.NewFlex().SetDirection(tview.FlexColumn).
AddItem(nil, 0, 1, false).
AddItem(
tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(nil, 0, 1, false).
AddItem(splitNetworkDialog, height, 1, false).
AddItem(nil, 0, 1, false),
width, 1, false).
AddItem(nil, 0, 1, false)
pages.AddPage(splitNetworkPage, splitNetworkDialogFlex, true, false)
}
{
summarizeNetworkDialog = tview.NewModal().
AddButtons([]string{"Yes", "No"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
switch buttonLabel {
case "Yes":
SummarizeNetwork()
fallthrough
case "No":
fallthrough
default:
summarizeNetworkDialog.SetText("")
pages.SwitchToPage(mainPage)
app.SetFocus(navigationPanel)
}
})
pages.AddPage(summarizeNetworkPage, summarizeNetworkDialog, true, false)
}
{
height := 15
width := 59
allocateNetworkSubnetsModeDialog = tview.NewForm().SetButtonsAlign(tview.AlignCenter).
AddInputField("Display Name", "", 40, nil, nil).
AddTextArea("Description", "", 48, 5, 0, nil).
AddInputField("Subnets Prefix", "", 40, nil, nil).
AddButton("Save", func() {
displayName := getAndClearTextFromInputField(allocateNetworkSubnetsModeDialog, "Display Name")
description := getAndClearTextFromTextArea(allocateNetworkSubnetsModeDialog, "Description")
subnetsPrefix := getAndClearTextFromInputField(allocateNetworkSubnetsModeDialog, "Subnets Prefix")
subnetsPrefix = strings.TrimLeft(subnetsPrefix, "/")
subnetsPrefixInt, err := strconv.Atoi(subnetsPrefix)
if err != nil {
statusLine.Clear()
statusLine.SetText("Invalid subnets prefix, should be a number representing smaller networks than this parent " + err.Error())
}
AllocateNetworkInSubnetsMode(displayName, description, subnetsPrefixInt)
pages.SwitchToPage(mainPage)
app.SetFocus(navigationPanel)
}).
AddButton("Cancel", func() {
getAndClearTextFromInputField(allocateNetworkSubnetsModeDialog, "Display Name")
getAndClearTextFromTextArea(allocateNetworkSubnetsModeDialog, "Description")
getAndClearTextFromInputField(allocateNetworkSubnetsModeDialog, "Subnets Prefix")
pages.SwitchToPage(mainPage)
app.SetFocus(navigationPanel)
})
allocateNetworkSubnetsModeDialog.SetBorder(true)
allocateNetworkSubnetsModeFlex := tview.NewFlex().SetDirection(tview.FlexColumn).
AddItem(nil, 0, 1, false).
AddItem(
tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(nil, 0, 1, false).
AddItem(allocateNetworkSubnetsModeDialog, height, 1, false).
AddItem(nil, 0, 1, false),
width, 1, false).
AddItem(nil, 0, 1, false)
pages.AddPage(allocateNetworkSubnetsModePage, allocateNetworkSubnetsModeFlex, true, false)
}
{
height := 13
width := 57
allocateNetworkHostsModeDialog = tview.NewForm().SetButtonsAlign(tview.AlignCenter).
AddInputField("Display Name", "", 40, nil, nil).
AddTextArea("Description", "", 48, 5, 0, nil).
AddButton("Save", func() {
displayName := getAndClearTextFromInputField(allocateNetworkHostsModeDialog, "Display Name")
description := getAndClearTextFromTextArea(allocateNetworkHostsModeDialog, "Description")
AllocateNetworkInHostsMode(displayName, description)
pages.SwitchToPage(mainPage)
app.SetFocus(navigationPanel)
}).
AddButton("Cancel", func() {
getAndClearTextFromInputField(allocateNetworkHostsModeDialog, "Display Name")
getAndClearTextFromTextArea(allocateNetworkHostsModeDialog, "Description")
pages.SwitchToPage(mainPage)
app.SetFocus(navigationPanel)
})
allocateNetworkHostsModeDialog.SetBorder(true)
allocateNetworkHostsModeFlex := tview.NewFlex().SetDirection(tview.FlexColumn).
AddItem(nil, 0, 1, false).
AddItem(
tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(nil, 0, 1, false).
AddItem(allocateNetworkHostsModeDialog, height, 1, false).
AddItem(nil, 0, 1, false),
width, 1, false).
AddItem(nil, 0, 1, false)
pages.AddPage(allocateNetworkHostsModePage, allocateNetworkHostsModeFlex, true, false)
}
{
height := 13
width := 59
updateNetworkAllocationDialog = tview.NewForm().SetButtonsAlign(tview.AlignCenter).
AddInputField("Display Name", "", 42, nil, nil).
AddTextArea("Description", "", 48, 5, 0, nil).
AddButton("Save", func() {
displayName := getAndClearTextFromInputField(updateNetworkAllocationDialog, "Display Name")
description := getAndClearTextFromTextArea(updateNetworkAllocationDialog, "Description")
UpdateNetworkAllocation(displayName, description)
pages.SwitchToPage(mainPage)
app.SetFocus(navigationPanel)
}).
AddButton("Cancel", func() {
getAndClearTextFromInputField(updateNetworkAllocationDialog, "Display Name")
getAndClearTextFromTextArea(updateNetworkAllocationDialog, "Description")
pages.SwitchToPage(mainPage)
app.SetFocus(navigationPanel)
})
updateNetworkAllocationDialog.SetBorder(true)
updateNetworkAllocationFlex := tview.NewFlex().SetDirection(tview.FlexColumn).
AddItem(nil, 0, 1, false).
AddItem(
tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(nil, 0, 1, false).
AddItem(updateNetworkAllocationDialog, height, 1, false).
AddItem(nil, 0, 1, false),
width, 1, false).
AddItem(nil, 0, 1, false)
pages.AddPage(updateNetworkAllocationPage, updateNetworkAllocationFlex, true, false)
}
{
deallocateNetworkDialog = tview.NewModal().
AddButtons([]string{"Yes", "No"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
switch buttonLabel {
case "Yes":
DeallocateNetwork()
fallthrough
case "No":
fallthrough
default:
deallocateNetworkDialog.SetText("")
pages.SwitchToPage(mainPage)
app.SetFocus(navigationPanel)
}
})
pages.AddPage(deallocateNetworkPage, deallocateNetworkDialog, true, false)
}
{
deleteNetworkDialog = tview.NewModal().
AddButtons([]string{"Yes", "No"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
switch buttonLabel {
case "Yes":
DeleteNetwork()
fallthrough
case "No":
fallthrough
default:
deleteNetworkDialog.SetText("")
pages.SwitchToPage(mainPage)
app.SetFocus(navigationPanel)
}
})
pages.AddPage(deleteNetworkPage, deleteNetworkDialog, true, false)
}
{
quitDialog = tview.NewModal().SetText("Do you want to quit? All unsaved changes will be lost.").
AddButtons([]string{"Quit", "Cancel"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
switch buttonLabel {
case "Quit":
app.Stop()
case "Cancel":
fallthrough
default:
pages.SwitchToPage(mainPage)
app.SetFocus(navigationPanel)
}
})
pages.AddPage(quitPage, quitDialog, true, false)
}
app.SetRoot(pages, true)
app.EnableMouse(true)
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyCtrlC:
return nil
case tcell.KeyCtrlS:
save()
return nil
case tcell.KeyCtrlQ:
// This is "hidden" quit without the confirmation dialog - use for local debugging, maybe should disable from the release version
app.Stop()
return nil
// case tcell.KeyCtrlD:
// statusLine.Clear()
// currentFocus := app.GetFocus()
// currentFocusStr := reflect.TypeOf(currentFocus).String()
// statusLine.SetText(currentFocusStr)
// return nil
}
return event
})
pages.SwitchToPage(mainPage)
app.SetFocus(navigationPanel)
load()
if err := app.Run(); err != nil {
panic(err)
}
}
func load() {
networks := &MenuStatic{
MenuFolder: &MenuFolder{
ID: "Networks",
},
Index: 0,
Description: `
In the network menu you can slice and dice the network
`,
}
menuItems.MustAdd(networks)
currentDir, err := os.Getwd()
if err != nil {
panic("Failed to get current directory: " + err.Error())
}
dataDir := filepath.Join(currentDir, dataDirName)
networkDir := filepath.Join(dataDir, networksDirName)
networkFiles, err := os.ReadDir(networkDir)
if err != nil {
if !os.IsNotExist(err) {
panic("Failed to read " + networkDir + " directory: " + err.Error())
}
if err := os.MkdirAll(networkDir, 0755); err != nil {
panic("Failed to create " + networkDir + " directory: " + err.Error())
}
}
for _, networkFile := range networkFiles {
if networkFile.IsDir() {
continue
}
bytes, err := os.ReadFile(filepath.Join(networkDir, networkFile.Name()))
if err != nil {
panic("Failed to read " + networkFile.Name() + " file: " + err.Error())
}
network := &Network{}
if err := yaml.Unmarshal(bytes, network); err != nil {
panic("Failed to unmarshal " + networkFile.Name() + " file: " + err.Error())
}
menuItems[network.GetPath()] = network
}
for _, menuItem := range menuItems {
if err := menuItem.Validate(); err != nil {
panic("Failed to load " + menuItem.GetPath() + ": " + err.Error())
}
}
reloadMenu(nil)
}
func save() {
currentDir, err := os.Getwd()
if err != nil {
panic("Failed to get current directory: " + err.Error())
}
dataDir := filepath.Join(currentDir, dataDirName)
dataTmpDir := dataDir + ".tmp"
networksTmpDir := filepath.Join(dataTmpDir, networksDirName)
if err := os.RemoveAll(networksTmpDir); err != nil {
panic("Failed to remove " + networksTmpDir + " directory: " + err.Error())
}
if err := os.MkdirAll(networksTmpDir, 0755); err != nil {
panic("Failed to create " + networksTmpDir + " directory: " + err.Error())
}
for _, menuItem := range menuItems {
switch m := menuItem.(type) {
case *MenuStatic:
// This is not serializable
case *Network:
id, err := CIDRToIdentifier(m.ID)
if err != nil {
panic("Failed to convert " + m.ID + " to identifier: " + err.Error())
}
fileName := filepath.Join(networksTmpDir, id+".yaml")
bytes, err := yaml.Marshal(menuItem)
if err != nil {
panic("Failed to marshal " + menuItem.GetPath() + " to yaml: " + err.Error())
}
if err := os.WriteFile(fileName, bytes, 0644); err != nil {
panic("Failed to write " + fileName + " file: " + err.Error())
}
default:
}
}
if err := os.RemoveAll(dataDir); err != nil {
panic("Failed to remove " + dataDir + " directory: " + err.Error())
}
if err := os.Rename(dataTmpDir, dataDir); err != nil {
panic("Failed to rename " + dataTmpDir + " to " + dataDir + " directory: " + err.Error())
}
networksIndex := []string{}
networksDisplayNames := map[string]string{}
networksDataIndex := map[string][]string{}
networksData := map[string]map[string]string{}
networksOpts := map[string]map[string]int{}
var recursivelyPopulateNetworksData func(n *Network)
recursivelyPopulateNetworksData = func(n *Network) {
index, data, err := n.RenderDetailsMap()
if err != nil {
panic("Failed to render details map for " + n.GetPath() + ": " + err.Error())
}
networksIndex = append(networksIndex, n.GetID())
networksDisplayNames[n.GetID()] = n.GetID()
networksDataIndex[n.GetID()] = index
networksData[n.GetID()] = data
networksOpts[n.GetID()] = map[string]int{
"AllocationMode": int(n.AllocationMode),
}
childs := menuItems.GetChilds(n)
for _, child := range childs {
nn, ok := child.(*Network)
if !ok {
panic("Failed to cast " + child.GetPath() + " to network")
}
recursivelyPopulateNetworksData(nn)
}
}
networksMenuItem := menuItems.GetByParentAndID(nil, "Networks")
for _, menuItem := range menuItems.GetChilds(networksMenuItem) {
n, ok := menuItem.(*Network)
if !ok {
panic("Failed to cast " + menuItem.GetPath() + " to network")
}
recursivelyPopulateNetworksData(n)
}
template := template.Must(template.New(markdownFileName).Parse(markdownTmpl))
input := map[string]interface{}{
"NetworksIndex": networksIndex,
"NetworksDisplayNames": networksDisplayNames,
"NetworksDataIndex": networksDataIndex,
"NetworksData": networksData,
"NetworksOpts": networksOpts,
}
mdFile, err := os.Create(filepath.Join(currentDir, markdownFileName))
if err != nil {
panic("Failed to create " + markdownFileName + " file: " + err.Error())
}
defer mdFile.Close()
if err := template.Execute(mdFile, input); err != nil {
panic("Failed to execute template for " + markdownFileName + " file: " + err.Error())
}
statusLine.Clear()
statusLine.SetText("Saved")
}
func reloadMenu(focusedItem MenuItem) {
navigationPanel.Clear()
newMenuItems := menuItems.GetChilds(currentMenuItem)
fromIndex := -1
for i, menuItem := range newMenuItems {
if focusedItem != nil && focusedItem.GetID() == menuItem.GetID() {
fromIndex = i
}
navigationPanel.AddItem(menuItem.GetID(), menuItem.GetPath(), 0, nil)
}
if fromIndex >= 0 {
navigationPanel.SetCurrentItem(fromIndex)
}
}
func updateKeysLine() {
keysLine.Clear()
keysLine.SetText(" " + strings.Join(append(append(globalKeys, currentMenuItemKeys...), currentFocusKeys...), " | "))
}
func getFormItemByLabel(form *tview.Form, label string) (int, tview.FormItem) {
formItemIndex := form.GetFormItemIndex(label)
if formItemIndex < 0 {
panic("Failed to find " + label + " form item index")
}
formItem := form.GetFormItem(formItemIndex)
if formItem == nil {
panic("Failed to find " + label + " form item")
}
return formItemIndex, formItem
}
func getAndClearTextFromInputField(form *tview.Form, label string) string {
_, formItem := getFormItemByLabel(form, label)
inputField, ok := formItem.(*tview.InputField)
if !ok {
panic("Failed to cast " + label + " input field")
}
text := inputField.GetText()
inputField.SetText("")
return text
}
func setTextFromInputField(form *tview.Form, label, value string) {
_, formItem := getFormItemByLabel(form, label)
inputField, ok := formItem.(*tview.InputField)
if !ok {
panic("Failed to cast " + label + " input field")
}
inputField.SetText(value)
}
func getAndClearTextFromTextArea(form *tview.Form, label string) string {
_, formItem := getFormItemByLabel(form, label)
textArea, ok := formItem.(*tview.TextArea)
if !ok {
panic("Failed to cast " + label + " text area")
}
text := textArea.GetText()
textArea.SetText("", false)
return text
}
func setTextFromTextArea(form *tview.Form, label, value string) {
_, formItem := getFormItemByLabel(form, label)
textArea, ok := formItem.(*tview.TextArea)
if !ok {
panic("Failed to cast " + label + " text area")
}
textArea.SetText(value, true)
}