Skip to content

Commit 10de81b

Browse files
committed
Add License
1 parent f04bab8 commit 10de81b

File tree

2 files changed

+81
-27
lines changed

2 files changed

+81
-27
lines changed

LICENSE.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Oliver Kuederle
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

main.go

+60-27
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ func shellout(command string) (error, string, string) {
2222
}
2323

2424
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
3132
}
3233

3334
func CreateApplication() *Tui {
@@ -40,6 +41,7 @@ func (t *Tui) Init() {
4041
t.form = tview.NewForm()
4142
t.menu = tview.NewFlex()
4243
t.help = tview.NewTextView()
44+
t.secondHelp = tview.NewTextView()
4345
t.pages = tview.NewPages()
4446
}
4547

@@ -106,13 +108,23 @@ func (t *Tui) CreateTable(rows []string) {
106108
}
107109
}).SetSelectedFunc(func(row int, column int) {
108110
t.table.SetSelectable(false, false)
111+
if row == 0 {
112+
t.app.SetFocus(t.table)
113+
return
114+
}
109115
t.CreateModal("Are you sure you want to remove this rule?",
110116
func() {
111117
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() {
113124
t.pages.HidePage("modal")
114125
t.app.SetFocus(t.table)
115-
})
126+
},
127+
)
116128
})
117129
}
118130

@@ -122,22 +134,20 @@ func (t *Tui) ReloadTable() {
122134
t.CreateTable(data)
123135
}
124136

125-
func (t *Tui) CreateModal(text string, action func(), finally func()) {
137+
func (t *Tui) CreateModal(text string, confirm func(), cancel func(), finally func()) {
126138
modal := tview.NewModal()
127139
t.pages.AddPage("modal", modal.SetText(text).AddButtons([]string{"Confirm", "Cancel"}).SetDoneFunc(func(i int, label string) {
128140
if label == "Confirm" {
129-
action()
141+
confirm()
130142
t.ReloadTable()
143+
} else {
144+
cancel()
131145
}
132146
modal.ClearButtons()
133147
finally()
134148
}), true, true)
135149
}
136150

137-
func (t *Tui) CreateHelp(text string) {
138-
t.help.SetText(text).SetBorderPadding(1, 0, 1, 0)
139-
}
140-
141151
func (t *Tui) CreateMenu() {
142152
menuList := tview.NewList()
143153
menuList.
@@ -147,49 +157,67 @@ func (t *Tui) CreateMenu() {
147157
}).
148158
AddItem("Remove a rule", "", 'd', func() {
149159
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)
151161
}).
152162
AddItem("Disable ufw", "", 's', func() {
153163
t.CreateModal("Are you sure you want to disable ufw?",
154164
func() {
155165
shellout("ufw --force disable")
166+
t.app.Stop()
156167
},
157168
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)
159174
},
160175
)
161176
}).
162177
AddItem("Reset rules", "", 'r', func() {
163178
t.CreateModal("Are you sure you want to reset all rules?",
164179
func() {
165180
shellout("ufw --force reset")
181+
t.app.Stop()
166182
},
167183
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)
169189
},
170190
)
171191
}).
172192
AddItem("Exit", "", 'q', func() { t.app.Stop() })
173-
menuList.SetBorderPadding(1, 0, 1, 1)
193+
menuList.SetShortcutColor(tcell.ColorDarkCyan).SetBorderPadding(1, 0, 1, 1)
174194
t.menu.AddItem(menuList, 0, 1, true)
175195
t.menu.SetBorder(true).SetTitle(" Menu ")
176196
}
177197

178198
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).
181202
AddDropDown("Protocol", []string{"tcp", "udp"}, 0, nil).
182203
AddDropDown("Action", []string{"ALLOW", "DENY", "REJECT", "LIMIT"}, 0, nil).
183-
AddInputField("From", "", 20, nil, nil).
204+
AddInputField("From *", "", 20, nil, nil).
184205
AddInputField("Comment", "", 40, nil, nil).
185206
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)
187214
}
188215

189216
func (t *Tui) Reset() {
190217
t.pages.HidePage("form")
191218
t.form.Clear(true)
192219
t.help.Clear()
220+
t.secondHelp.Clear()
193221
t.app.SetFocus(t.menu)
194222
}
195223

@@ -222,19 +250,20 @@ func (t *Tui) CreateLayout() *tview.Pages {
222250

223251
base := tview.NewFlex().AddItem(
224252
columns.
225-
AddItem(t.menu, 0, 1, true).
253+
AddItem(t.menu, 0, 2, true).
226254
AddItem(t.table, 0, 4, false),
227255
0, 1, true,
228256
)
229257

230258
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),
233262
0, 3, false,
234263
)
235264

236265
t.pages.AddAndSwitchToPage("base", base, true)
237-
t.pages.AddPage("form", form, false, false)
266+
t.pages.AddPage("form", form, true, false)
238267
return t.pages
239268
}
240269

@@ -254,11 +283,15 @@ func main() {
254283
func() {
255284
shellout("ufw --force enable")
256285
},
286+
func() {
287+
tui.app.Stop()
288+
},
257289
func() {
258290
tui.pages.HidePage("modal")
259291
tui.pages.ShowPage("base")
260292
tui.app.SetFocus(tui.menu)
261-
})
293+
},
294+
)
262295
}
263296

264297
tui.CreateTable(data)

0 commit comments

Comments
 (0)