-
Notifications
You must be signed in to change notification settings - Fork 28
/
gui.go
275 lines (243 loc) · 6.82 KB
/
gui.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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
var UIactive = true
var AppStarted = false
var UIapp *tview.Application
var txtMatchs *tview.TextView
var txtStdout *tview.TextView
var txtStderr *tview.TextView
var UIselectedConfigPath string
var UItmpConfigPath string
var currentMainWindowSelector int = 0
var currentConfigWindowSelector int = 0
func InitUI() {
UIapp = tview.NewApplication()
txtMatchs = tview.NewTextView()
txtStdout = tview.NewTextView()
txtStderr = tview.NewTextView()
}
// MainWindow display application UI
func MainWindow() {
currentMainWindowSelector = 1
/*
* TEXTVIEW : Windows app name
*/
txtAppTitle := tview.NewTextView().
SetDynamicColors(true).
SetText(RenderFastfinderVersion()).
SetTextAlign(tview.AlignCenter)
/*
* TEXTVIEW : selection box helper
*/
txtInfoTitle := tview.NewTextView().
SetDynamicColors(true).
SetText("Press tab to navigate between log panels | Up and Down arrow to scroll text").
SetTextAlign(tview.AlignCenter)
/*
* TEXTVIEW : File matchs
*/
txtMatchs.
SetDynamicColors(true).
SetText("[yellow]File match:\n").
SetRegions(true).
SetChangedFunc(func() {
UIapp.Draw()
})
/*
* TEXTVIEW : Program execution Output
*/
txtStdout.
SetDynamicColors(true).
SetText("[grey]Init and execution informations:\n").
SetRegions(true).
SetChangedFunc(func() {
UIapp.Draw()
})
/*
* TEXTVIEW : execution errors
*/
txtStderr.
SetDynamicColors(true).
SetText("[red]Access and scan errors:\n").
SetRegions(true).
SetChangedFunc(func() {
UIapp.Draw()
})
/*
* Building window
*/
grid := tview.NewGrid().SetRows(1, -5, -2, 1).SetColumns(0, 0).SetBorders(true)
grid.AddItem(txtAppTitle, 0, 0, 1, 2, 0, 0, false)
grid.AddItem(txtMatchs, 1, 1, 1, 1, 0, 0, false)
grid.AddItem(txtStderr, 1, 0, 1, 1, 0, 0, false)
grid.AddItem(txtStdout, 2, 0, 1, 2, 0, 0, false)
grid.AddItem(txtInfoTitle, 3, 0, 1, 2, 0, 0, false)
grid.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyTab {
switch currentMainWindowSelector {
case 1:
currentMainWindowSelector++
UIapp.SetFocus(txtStdout)
txtInfoTitle.SetText("Selected box: Informations messages")
case 2:
currentMainWindowSelector++
UIapp.SetFocus(txtStderr)
txtInfoTitle.SetText("Selected box: Error messages")
case 3:
currentMainWindowSelector = 1
UIapp.SetFocus(txtMatchs)
txtInfoTitle.SetText("Selected box: Matchs messages")
}
return nil
}
return event
})
AppStarted = true
if err := UIapp.SetRoot(grid, true).SetFocus(txtMatchs).Run(); err != nil {
UIapp.Stop()
AppStarted = false
}
}
// OpenFileDialog show a navigable tree view of the current directory.
func OpenFileDialog() {
currentConfigWindowSelector = 1
/*
* TEXTVIEW : Dialog title
*/
lblDialog := tview.NewTextView().SetTextAlign(tview.AlignCenter).SetText("Fastfinder : Please select a yaml configuration file")
/*
* TEXTVIEW : selection box helper
*/
txtInfoTitle := tview.NewTextView().
SetDynamicColors(true).
SetText("Press tab to navigate between left and right pane | Up and Down arrow to scroll in file preview").
SetTextAlign(tview.AlignCenter)
/*
* TEXTVIEW : File preview
*/
textPreview := tview.NewTextView().
SetDynamicColors(true).
SetRegions(true).
SetChangedFunc(func() {
UIapp.Draw()
})
/*
* TREEVIEW : File and directory navigation
*/
rootDir, _ := os.Getwd()
root := tview.NewTreeNode(rootDir).
SetColor(tcell.ColorRed)
treeView := tview.NewTreeView().
SetRoot(root).
SetCurrentNode(root)
// function definition for adding files and directories to the treeview
add := func(target *tview.TreeNode, p string) {
files, err := ioutil.ReadDir(p)
if err != nil {
UIapp.Stop()
}
if filepath.Dir(p) != p {
parentNode := tview.NewTreeNode("../ (" + filepath.Dir(p) + ")").SetReference(filepath.Dir(p)).SetSelectable(true).SetColor(tcell.ColorRed)
target.AddChild(parentNode)
}
for _, file := range files {
node := tview.NewTreeNode(file.Name()).
SetReference(filepath.Join(p, file.Name())).
SetSelectable(file.IsDir() || !file.IsDir() && (strings.HasSuffix(strings.ToLower(file.Name()), ".yml") || strings.HasSuffix(strings.ToLower(file.Name()), ".yaml")))
if file.IsDir() {
node.SetColor(tcell.ColorWhite)
} else if !file.IsDir() && (strings.HasSuffix(strings.ToLower(file.Name()), ".yml") || strings.HasSuffix(strings.ToLower(file.Name()), ".yaml")) {
node.SetColor(tcell.ColorGreen)
} else {
node.SetColor(tcell.ColorGrey)
}
target.AddChild(node)
}
}
// Add the current directory as the root node
add(root, rootDir)
// If a file or directory was selected, open it.
treeView.SetSelectedFunc(func(node *tview.TreeNode) {
reference := node.GetReference()
p := reference.(string)
file, err := os.Open(p)
if err != nil {
return
}
fileInfo, err := file.Stat()
if err != nil {
return
}
if fileInfo.IsDir() {
// directory tree drawing
children := node.GetChildren()
if len(children) == 0 {
// Load and show files in this directory.
add(node, p)
} else {
// Collapse if visible, expand if collapsed.
node.SetExpanded(!node.IsExpanded())
}
} else {
// file preview & selection
if strings.HasSuffix(strings.ToLower(p), ".yml") || strings.HasSuffix(strings.ToLower(p), ".yaml") {
if UItmpConfigPath == p {
UIselectedConfigPath = p
UIapp.Stop()
}
UItmpConfigPath = p
d, err := os.ReadFile(p)
if err != nil {
return
}
textPreview.SetText("")
fmt.Fprintf(textPreview, "[yellow]== Press Enter again to select this configuration file ==[white]\n\n%s", d)
textPreview.ScrollTo(0, 0)
}
}
})
/*
* Building window
*/
grid := tview.NewGrid().SetRows(1, -1, 1).SetColumns(-3, -2).SetBorders(true)
grid.AddItem(lblDialog, 0, 0, 1, 2, 0, 0, false)
grid.AddItem(treeView, 1, 0, 1, 1, 0, 0, true)
grid.AddItem(textPreview, 1, 1, 1, 1, 0, 0, false)
grid.AddItem(txtInfoTitle, 2, 0, 1, 2, 0, 0, false)
grid.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyTab {
switch currentConfigWindowSelector {
case 1:
currentConfigWindowSelector++
UIapp.SetFocus(textPreview)
txtInfoTitle.SetText("Selected box: Config file preview")
case 2:
currentConfigWindowSelector = 1
UIapp.SetFocus(treeView)
txtInfoTitle.SetText("Selected box: File browser")
}
return nil
}
if event.Key() == tcell.KeyEnter {
if currentConfigWindowSelector == 2 {
currentConfigWindowSelector = 1
UIapp.SetFocus(treeView)
txtInfoTitle.SetText("Selected box: File browser")
}
}
return event
})
AppStarted = true
if err := UIapp.SetRoot(grid, true).SetFocus(treeView).Run(); err != nil {
UIapp.Stop()
AppStarted = false
}
}