@@ -22,12 +22,13 @@ func shellout(command string) (error, string, string) {
22
22
}
23
23
24
24
type Tui struct {
25
- app * tview.Application
26
- form * tview.Form
27
- table * tview.Table
28
- menu * tview.Flex
29
- help * tview.TextView
30
- pages * tview.Pages
25
+ app * tview.Application
26
+ form * tview.Form
27
+ table * tview.Table
28
+ menu * tview.Flex
29
+ help * tview.TextView
30
+ secondHelp * tview.TextView
31
+ pages * tview.Pages
31
32
}
32
33
33
34
func CreateApplication () * Tui {
@@ -40,6 +41,7 @@ func (t *Tui) Init() {
40
41
t .form = tview .NewForm ()
41
42
t .menu = tview .NewFlex ()
42
43
t .help = tview .NewTextView ()
44
+ t .secondHelp = tview .NewTextView ()
43
45
t .pages = tview .NewPages ()
44
46
}
45
47
@@ -106,13 +108,23 @@ func (t *Tui) CreateTable(rows []string) {
106
108
}
107
109
}).SetSelectedFunc (func (row int , column int ) {
108
110
t .table .SetSelectable (false , false )
111
+ if row == 0 {
112
+ t .app .SetFocus (t .table )
113
+ return
114
+ }
109
115
t .CreateModal ("Are you sure you want to remove this rule?" ,
110
116
func () {
111
117
shellout (fmt .Sprintf ("ufw --force delete %d" , row ))
112
- }, func () {
118
+ },
119
+ func () {
120
+ t .pages .HidePage ("modal" )
121
+ t .app .SetFocus (t .table )
122
+ },
123
+ func () {
113
124
t .pages .HidePage ("modal" )
114
125
t .app .SetFocus (t .table )
115
- })
126
+ },
127
+ )
116
128
})
117
129
}
118
130
@@ -122,22 +134,20 @@ func (t *Tui) ReloadTable() {
122
134
t .CreateTable (data )
123
135
}
124
136
125
- func (t * Tui ) CreateModal (text string , action func (), finally func ()) {
137
+ func (t * Tui ) CreateModal (text string , confirm func (), cancel func (), finally func ()) {
126
138
modal := tview .NewModal ()
127
139
t .pages .AddPage ("modal" , modal .SetText (text ).AddButtons ([]string {"Confirm" , "Cancel" }).SetDoneFunc (func (i int , label string ) {
128
140
if label == "Confirm" {
129
- action ()
141
+ confirm ()
130
142
t .ReloadTable ()
143
+ } else {
144
+ cancel ()
131
145
}
132
146
modal .ClearButtons ()
133
147
finally ()
134
148
}), true , true )
135
149
}
136
150
137
- func (t * Tui ) CreateHelp (text string ) {
138
- t .help .SetText (text ).SetBorderPadding (1 , 0 , 1 , 0 )
139
- }
140
-
141
151
func (t * Tui ) CreateMenu () {
142
152
menuList := tview .NewList ()
143
153
menuList .
@@ -147,49 +157,67 @@ func (t *Tui) CreateMenu() {
147
157
}).
148
158
AddItem ("Remove a rule" , "" , 'd' , func () {
149
159
t .app .SetFocus (t .table )
150
- t .CreateHelp ("Press <Esc> to go back to the menu selection" )
160
+ t .help . SetText ("Press <Esc> to go back to the menu selection" ). SetBorderPadding ( 1 , 0 , 1 , 0 )
151
161
}).
152
162
AddItem ("Disable ufw" , "" , 's' , func () {
153
163
t .CreateModal ("Are you sure you want to disable ufw?" ,
154
164
func () {
155
165
shellout ("ufw --force disable" )
166
+ t .app .Stop ()
156
167
},
157
168
func () {
158
- t .app .Stop ()
169
+ t .pages .RemovePage ("modal" )
170
+ t .app .SetFocus (t .menu )
171
+ },
172
+ func () {
173
+ t .app .SetFocus (t .menu )
159
174
},
160
175
)
161
176
}).
162
177
AddItem ("Reset rules" , "" , 'r' , func () {
163
178
t .CreateModal ("Are you sure you want to reset all rules?" ,
164
179
func () {
165
180
shellout ("ufw --force reset" )
181
+ t .app .Stop ()
166
182
},
167
183
func () {
168
- t .app .Stop ()
184
+ t .app .SetFocus (t .menu )
185
+ },
186
+ func () {
187
+ t .pages .RemovePage ("modal" )
188
+ t .app .SetFocus (t .menu )
169
189
},
170
190
)
171
191
}).
172
192
AddItem ("Exit" , "" , 'q' , func () { t .app .Stop () })
173
- menuList .SetBorderPadding (1 , 0 , 1 , 1 )
193
+ menuList .SetShortcutColor ( tcell . ColorDarkCyan ). SetBorderPadding (1 , 0 , 1 , 1 )
174
194
t .menu .AddItem (menuList , 0 , 1 , true )
175
195
t .menu .SetBorder (true ).SetTitle (" Menu " )
176
196
}
177
197
178
198
func (t * Tui ) CreateForm () {
179
- t .CreateHelp ("Use <Tab> and <Enter> keys to navigate through the form" )
180
- t .form .AddInputField ("To" , "" , 20 , nil , nil ).
199
+ t .help .SetText ("Use <Tab> and <Enter> keys to navigate through the form" ).SetBorderPadding (1 , 0 , 1 , 1 )
200
+
201
+ t .form .AddInputField ("To" , "" , 20 , nil , nil ).SetFieldTextColor (tcell .ColorWhite ).
181
202
AddDropDown ("Protocol" , []string {"tcp" , "udp" }, 0 , nil ).
182
203
AddDropDown ("Action" , []string {"ALLOW" , "DENY" , "REJECT" , "LIMIT" }, 0 , nil ).
183
- AddInputField ("From" , "" , 20 , nil , nil ).
204
+ AddInputField ("From * " , "" , 20 , nil , nil ).
184
205
AddInputField ("Comment" , "" , 40 , nil , nil ).
185
206
AddButton ("Save" , t .CreateRule ).
186
- AddButton ("Cancel" , t .Cancel )
207
+ AddButton ("Cancel" , t .Cancel ).
208
+ SetButtonTextColor (tcell .ColorWhite ).
209
+ SetButtonBackgroundColor (tcell .ColorDarkCyan ).
210
+ SetFieldBackgroundColor (tcell .ColorDarkCyan ).
211
+ SetLabelColor (tcell .ColorWhite )
212
+
213
+ t .secondHelp .SetText ("* Leave empty for Anywhere" ).SetTextColor (tcell .ColorDarkCyan ).SetBorderPadding (1 , 0 , 1 , 1 )
187
214
}
188
215
189
216
func (t * Tui ) Reset () {
190
217
t .pages .HidePage ("form" )
191
218
t .form .Clear (true )
192
219
t .help .Clear ()
220
+ t .secondHelp .Clear ()
193
221
t .app .SetFocus (t .menu )
194
222
}
195
223
@@ -222,19 +250,20 @@ func (t *Tui) CreateLayout() *tview.Pages {
222
250
223
251
base := tview .NewFlex ().AddItem (
224
252
columns .
225
- AddItem (t .menu , 0 , 1 , true ).
253
+ AddItem (t .menu , 0 , 2 , true ).
226
254
AddItem (t .table , 0 , 4 , false ),
227
255
0 , 1 , true ,
228
256
)
229
257
230
258
form := columns .AddItem (tview .NewFlex ().SetDirection (tview .FlexRow ).
231
- AddItem (t .help , 0 , 1 , false ).
232
- AddItem (t .form , 0 , 8 , false ),
259
+ AddItem (t .help , 0 , 2 , false ).
260
+ AddItem (t .form , 0 , 8 , false ).
261
+ AddItem (t .secondHelp , 0 , 2 , false ),
233
262
0 , 3 , false ,
234
263
)
235
264
236
265
t .pages .AddAndSwitchToPage ("base" , base , true )
237
- t .pages .AddPage ("form" , form , false , false )
266
+ t .pages .AddPage ("form" , form , true , false )
238
267
return t .pages
239
268
}
240
269
@@ -254,11 +283,15 @@ func main() {
254
283
func () {
255
284
shellout ("ufw --force enable" )
256
285
},
286
+ func () {
287
+ tui .app .Stop ()
288
+ },
257
289
func () {
258
290
tui .pages .HidePage ("modal" )
259
291
tui .pages .ShowPage ("base" )
260
292
tui .app .SetFocus (tui .menu )
261
- })
293
+ },
294
+ )
262
295
}
263
296
264
297
tui .CreateTable (data )
0 commit comments