2019-10-21 01:07:26 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/gdamore/tcell"
|
|
|
|
"github.com/rivo/tview"
|
|
|
|
)
|
|
|
|
|
|
|
|
func adminscreen() {
|
|
|
|
log.SetOutput(ioutil.Discard)
|
|
|
|
|
2019-10-21 02:09:44 +02:00
|
|
|
messages := []*struct {
|
|
|
|
name string
|
|
|
|
label string
|
|
|
|
text string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "servermsg",
|
|
|
|
label: "server",
|
|
|
|
text: string(serverMsg),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "aboutmsg",
|
|
|
|
label: "about",
|
|
|
|
text: string(aboutMsg),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "loginmsg",
|
|
|
|
label: "login",
|
|
|
|
text: string(loginMsg),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-10-21 01:07:26 +02:00
|
|
|
app := tview.NewApplication()
|
|
|
|
var maindriver func(event *tcell.EventKey) *tcell.EventKey
|
|
|
|
|
|
|
|
table := tview.NewTable().SetFixed(1, 0).SetSelectable(true, false).
|
|
|
|
SetSelectedStyle(tcell.ColorBlack, tcell.ColorGreen, 0)
|
|
|
|
|
|
|
|
mainframe := tview.NewFrame(table)
|
|
|
|
mainframe.AddText(tview.Escape("honk admin - [q] quit"),
|
|
|
|
true, 0, tcell.ColorGreen)
|
|
|
|
mainframe.SetBorders(1, 0, 1, 0, 4, 0)
|
|
|
|
|
|
|
|
dupecell := func(base *tview.TableCell) *tview.TableCell {
|
|
|
|
rv := new(tview.TableCell)
|
|
|
|
*rv = *base
|
|
|
|
return rv
|
|
|
|
}
|
|
|
|
|
|
|
|
showtable := func() {
|
|
|
|
table.Clear()
|
|
|
|
|
|
|
|
row := 0
|
|
|
|
{
|
|
|
|
col := 0
|
|
|
|
headcell := tview.TableCell{
|
2019-10-21 02:09:44 +02:00
|
|
|
Color: tcell.ColorWhite,
|
|
|
|
NotSelectable: true,
|
2019-10-21 01:07:26 +02:00
|
|
|
}
|
|
|
|
cell := dupecell(&headcell)
|
2019-10-21 02:09:44 +02:00
|
|
|
cell.Text = "which "
|
2019-10-21 01:07:26 +02:00
|
|
|
table.SetCell(row, col, cell)
|
|
|
|
col++
|
|
|
|
cell = dupecell(&headcell)
|
2019-10-21 02:09:44 +02:00
|
|
|
cell.Text = "message"
|
2019-10-21 01:07:26 +02:00
|
|
|
table.SetCell(row, col, cell)
|
|
|
|
|
|
|
|
row++
|
|
|
|
}
|
2019-10-21 02:09:44 +02:00
|
|
|
for i := 0; i < 3; i++ {
|
2019-10-21 01:07:26 +02:00
|
|
|
col := 0
|
2019-10-21 02:09:44 +02:00
|
|
|
msg := messages[i]
|
2019-10-21 01:07:26 +02:00
|
|
|
headcell := tview.TableCell{
|
|
|
|
Color: tcell.ColorWhite,
|
|
|
|
}
|
|
|
|
cell := dupecell(&headcell)
|
2019-10-21 02:09:44 +02:00
|
|
|
cell.Text = msg.label
|
2019-10-21 01:07:26 +02:00
|
|
|
table.SetCell(row, col, cell)
|
|
|
|
col++
|
|
|
|
cell = dupecell(&headcell)
|
2019-10-21 02:09:44 +02:00
|
|
|
cell.Text = tview.Escape(msg.text)
|
2019-10-21 01:07:26 +02:00
|
|
|
table.SetCell(row, col, cell)
|
|
|
|
|
|
|
|
row++
|
|
|
|
}
|
|
|
|
|
|
|
|
app.SetInputCapture(maindriver)
|
|
|
|
app.SetRoot(mainframe, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
arrowadapter := func(event *tcell.EventKey) *tcell.EventKey {
|
|
|
|
switch event.Key() {
|
|
|
|
case tcell.KeyDown:
|
|
|
|
return tcell.NewEventKey(tcell.KeyTab, '\t', tcell.ModNone)
|
|
|
|
case tcell.KeyUp:
|
|
|
|
return tcell.NewEventKey(tcell.KeyBacktab, '\t', tcell.ModNone)
|
|
|
|
}
|
|
|
|
return event
|
|
|
|
}
|
|
|
|
|
|
|
|
editform := tview.NewForm()
|
2019-10-21 02:09:44 +02:00
|
|
|
descbox := tview.NewInputField().SetLabel("msg: ").SetFieldWidth(60)
|
2019-10-21 01:07:26 +02:00
|
|
|
editform.AddButton("save", nil)
|
|
|
|
editform.AddButton("cancel", nil)
|
|
|
|
savebutton := editform.GetButton(0)
|
|
|
|
editform.SetFieldTextColor(tcell.ColorBlack)
|
|
|
|
editform.SetFieldBackgroundColor(tcell.ColorGreen)
|
|
|
|
editform.SetLabelColor(tcell.ColorWhite)
|
|
|
|
editform.SetButtonTextColor(tcell.ColorGreen)
|
|
|
|
editform.SetButtonBackgroundColor(tcell.ColorBlack)
|
|
|
|
editform.GetButton(1).SetSelectedFunc(showtable)
|
|
|
|
editform.SetCancelFunc(showtable)
|
|
|
|
|
2019-10-21 02:09:44 +02:00
|
|
|
editframe := tview.NewFrame(editform)
|
|
|
|
editframe.SetBorders(1, 0, 1, 0, 4, 0)
|
2019-10-21 01:07:26 +02:00
|
|
|
|
|
|
|
showform := func() {
|
|
|
|
editform.Clear(false)
|
|
|
|
editform.AddFormItem(descbox)
|
|
|
|
app.SetInputCapture(arrowadapter)
|
2019-10-21 02:09:44 +02:00
|
|
|
app.SetRoot(editframe, true)
|
2019-10-21 01:07:26 +02:00
|
|
|
}
|
|
|
|
|
2019-10-21 02:09:44 +02:00
|
|
|
editmsg := func(which int) {
|
|
|
|
msg := messages[which]
|
|
|
|
editframe.Clear()
|
|
|
|
editframe.AddText(tview.Escape("edit "+msg.label+" message"),
|
|
|
|
true, 0, tcell.ColorGreen)
|
|
|
|
descbox.SetText(msg.text)
|
2019-10-21 01:07:26 +02:00
|
|
|
savebutton.SetSelectedFunc(func() {
|
2019-10-21 02:09:44 +02:00
|
|
|
msg.text = descbox.GetText()
|
|
|
|
updateconfig(msg.name, msg.text)
|
2019-10-21 01:07:26 +02:00
|
|
|
showtable()
|
|
|
|
})
|
|
|
|
showform()
|
|
|
|
}
|
|
|
|
|
2019-10-21 02:09:44 +02:00
|
|
|
table.SetSelectedFunc(func(row, col int) {
|
|
|
|
editmsg(row - 1)
|
|
|
|
})
|
|
|
|
|
2019-10-21 01:07:26 +02:00
|
|
|
maindriver = func(event *tcell.EventKey) *tcell.EventKey {
|
|
|
|
switch event.Rune() {
|
|
|
|
case 'e':
|
2019-10-21 02:09:44 +02:00
|
|
|
r, _ := table.GetSelection()
|
|
|
|
r--
|
|
|
|
editmsg(r)
|
2019-10-21 01:07:26 +02:00
|
|
|
case 'q':
|
|
|
|
app.Stop()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return event
|
|
|
|
}
|
|
|
|
|
|
|
|
showtable()
|
|
|
|
app.Run()
|
|
|
|
}
|