admin.go (view raw)
1//
2// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
3//
4// Permission to use, copy, modify, and distribute this software for any
5// purpose with or without fee is hereby granted, provided that the above
6// copyright notice and this permission notice appear in all copies.
7//
8// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
16package main
17
18import (
19 "html/template"
20 "io/ioutil"
21 "log"
22
23 "github.com/gdamore/tcell"
24 "github.com/rivo/tview"
25)
26
27func adminscreen() {
28 log.SetOutput(ioutil.Discard)
29
30 app := tview.NewApplication()
31 var maindriver func(event *tcell.EventKey) *tcell.EventKey
32
33 table := tview.NewTable().SetFixed(1, 0).SetSelectable(true, false).
34 SetSelectedStyle(tcell.ColorBlack, tcell.ColorGreen, 0)
35
36 mainframe := tview.NewFrame(table)
37 mainframe.AddText(tview.Escape("honk admin - [q] quit"),
38 true, 0, tcell.ColorGreen)
39 mainframe.SetBorders(1, 0, 1, 0, 4, 0)
40
41 dupecell := func(base *tview.TableCell) *tview.TableCell {
42 rv := new(tview.TableCell)
43 *rv = *base
44 return rv
45 }
46
47 showtable := func() {
48 table.Clear()
49
50 row := 0
51 {
52 col := 0
53 headcell := tview.TableCell{
54 Color: tcell.ColorWhite,
55 }
56 cell := dupecell(&headcell)
57 cell.Text = "Message"
58 table.SetCell(row, col, cell)
59 col++
60 cell = dupecell(&headcell)
61 cell.Text = ""
62 table.SetCell(row, col, cell)
63
64 row++
65 }
66 {
67 col := 0
68 headcell := tview.TableCell{
69 Color: tcell.ColorWhite,
70 }
71 cell := dupecell(&headcell)
72 cell.Text = "Server"
73 table.SetCell(row, col, cell)
74 col++
75 cell = dupecell(&headcell)
76 cell.Text = tview.Escape(string(serverMsg))
77 table.SetCell(row, col, cell)
78
79 row++
80 }
81 {
82 col := 0
83 headcell := tview.TableCell{
84 Color: tcell.ColorWhite,
85 }
86 cell := dupecell(&headcell)
87 cell.Text = "About"
88 table.SetCell(row, col, cell)
89 col++
90 cell = dupecell(&headcell)
91 cell.Text = tview.Escape(string(aboutMsg))
92 table.SetCell(row, col, cell)
93
94 row++
95 }
96 {
97 col := 0
98 headcell := tview.TableCell{
99 Color: tcell.ColorWhite,
100 }
101 cell := dupecell(&headcell)
102 cell.Text = "Login"
103 table.SetCell(row, col, cell)
104 col++
105 cell = dupecell(&headcell)
106 cell.Text = tview.Escape(string(loginMsg))
107 table.SetCell(row, col, cell)
108
109 row++
110 }
111
112 app.SetInputCapture(maindriver)
113 app.SetRoot(mainframe, true)
114 }
115
116 arrowadapter := func(event *tcell.EventKey) *tcell.EventKey {
117 switch event.Key() {
118 case tcell.KeyDown:
119 return tcell.NewEventKey(tcell.KeyTab, '\t', tcell.ModNone)
120 case tcell.KeyUp:
121 return tcell.NewEventKey(tcell.KeyBacktab, '\t', tcell.ModNone)
122 }
123 return event
124 }
125
126 editform := tview.NewForm()
127 namebox := tview.NewInputField().SetLabel("name").SetFieldWidth(20)
128 descbox := tview.NewInputField().SetLabel("description").SetFieldWidth(60)
129 orderbox := tview.NewInputField().SetLabel("order").SetFieldWidth(10)
130 editform.AddButton("save", nil)
131 editform.AddButton("cancel", nil)
132 savebutton := editform.GetButton(0)
133 editform.SetFieldTextColor(tcell.ColorBlack)
134 editform.SetFieldBackgroundColor(tcell.ColorGreen)
135 editform.SetLabelColor(tcell.ColorWhite)
136 editform.SetButtonTextColor(tcell.ColorGreen)
137 editform.SetButtonBackgroundColor(tcell.ColorBlack)
138 editform.GetButton(1).SetSelectedFunc(showtable)
139 editform.SetCancelFunc(showtable)
140
141 hadchanges := false
142
143 showform := func() {
144 editform.Clear(false)
145 editform.AddFormItem(namebox)
146 editform.AddFormItem(descbox)
147 editform.AddFormItem(orderbox)
148 app.SetInputCapture(arrowadapter)
149 app.SetRoot(editform, true)
150 }
151
152 editrepo := func(which string) {
153 namebox.SetText(which)
154 descbox.SetText("message")
155 savebutton.SetSelectedFunc(func() {
156 serverMsg = template.HTML(descbox.GetText())
157 hadchanges = true
158 showtable()
159 })
160 showform()
161 }
162
163 maindriver = func(event *tcell.EventKey) *tcell.EventKey {
164 switch event.Rune() {
165 case 'd':
166 case 'e':
167 editrepo("servermsg")
168 case 'q':
169 app.Stop()
170 return nil
171 }
172 return event
173 }
174
175 showtable()
176 app.Run()
177}