ok, i think we've got the admin screen working now

This commit is contained in:
Ted Unangst 2019-10-20 20:09:44 -04:00
parent b3e2afc7fb
commit a4a0168850
3 changed files with 64 additions and 57 deletions

100
admin.go
View File

@ -16,7 +16,6 @@
package main package main
import ( import (
"html/template"
"io/ioutil" "io/ioutil"
"log" "log"
@ -27,6 +26,28 @@ import (
func adminscreen() { func adminscreen() {
log.SetOutput(ioutil.Discard) log.SetOutput(ioutil.Discard)
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),
},
}
app := tview.NewApplication() app := tview.NewApplication()
var maindriver func(event *tcell.EventKey) *tcell.EventKey var maindriver func(event *tcell.EventKey) *tcell.EventKey
@ -51,59 +72,31 @@ func adminscreen() {
{ {
col := 0 col := 0
headcell := tview.TableCell{ headcell := tview.TableCell{
Color: tcell.ColorWhite, Color: tcell.ColorWhite,
NotSelectable: true,
} }
cell := dupecell(&headcell) cell := dupecell(&headcell)
cell.Text = "Message" cell.Text = "which "
table.SetCell(row, col, cell) table.SetCell(row, col, cell)
col++ col++
cell = dupecell(&headcell) cell = dupecell(&headcell)
cell.Text = "" cell.Text = "message"
table.SetCell(row, col, cell) table.SetCell(row, col, cell)
row++ row++
} }
{ for i := 0; i < 3; i++ {
col := 0 col := 0
msg := messages[i]
headcell := tview.TableCell{ headcell := tview.TableCell{
Color: tcell.ColorWhite, Color: tcell.ColorWhite,
} }
cell := dupecell(&headcell) cell := dupecell(&headcell)
cell.Text = "Server" cell.Text = msg.label
table.SetCell(row, col, cell) table.SetCell(row, col, cell)
col++ col++
cell = dupecell(&headcell) cell = dupecell(&headcell)
cell.Text = tview.Escape(string(serverMsg)) cell.Text = tview.Escape(msg.text)
table.SetCell(row, col, cell)
row++
}
{
col := 0
headcell := tview.TableCell{
Color: tcell.ColorWhite,
}
cell := dupecell(&headcell)
cell.Text = "About"
table.SetCell(row, col, cell)
col++
cell = dupecell(&headcell)
cell.Text = tview.Escape(string(aboutMsg))
table.SetCell(row, col, cell)
row++
}
{
col := 0
headcell := tview.TableCell{
Color: tcell.ColorWhite,
}
cell := dupecell(&headcell)
cell.Text = "Login"
table.SetCell(row, col, cell)
col++
cell = dupecell(&headcell)
cell.Text = tview.Escape(string(loginMsg))
table.SetCell(row, col, cell) table.SetCell(row, col, cell)
row++ row++
@ -124,9 +117,7 @@ func adminscreen() {
} }
editform := tview.NewForm() editform := tview.NewForm()
namebox := tview.NewInputField().SetLabel("name").SetFieldWidth(20) descbox := tview.NewInputField().SetLabel("msg: ").SetFieldWidth(60)
descbox := tview.NewInputField().SetLabel("description").SetFieldWidth(60)
orderbox := tview.NewInputField().SetLabel("order").SetFieldWidth(10)
editform.AddButton("save", nil) editform.AddButton("save", nil)
editform.AddButton("cancel", nil) editform.AddButton("cancel", nil)
savebutton := editform.GetButton(0) savebutton := editform.GetButton(0)
@ -138,33 +129,40 @@ func adminscreen() {
editform.GetButton(1).SetSelectedFunc(showtable) editform.GetButton(1).SetSelectedFunc(showtable)
editform.SetCancelFunc(showtable) editform.SetCancelFunc(showtable)
hadchanges := false editframe := tview.NewFrame(editform)
editframe.SetBorders(1, 0, 1, 0, 4, 0)
showform := func() { showform := func() {
editform.Clear(false) editform.Clear(false)
editform.AddFormItem(namebox)
editform.AddFormItem(descbox) editform.AddFormItem(descbox)
editform.AddFormItem(orderbox)
app.SetInputCapture(arrowadapter) app.SetInputCapture(arrowadapter)
app.SetRoot(editform, true) app.SetRoot(editframe, true)
} }
editrepo := func(which string) { editmsg := func(which int) {
namebox.SetText(which) msg := messages[which]
descbox.SetText("message") editframe.Clear()
editframe.AddText(tview.Escape("edit "+msg.label+" message"),
true, 0, tcell.ColorGreen)
descbox.SetText(msg.text)
savebutton.SetSelectedFunc(func() { savebutton.SetSelectedFunc(func() {
serverMsg = template.HTML(descbox.GetText()) msg.text = descbox.GetText()
hadchanges = true updateconfig(msg.name, msg.text)
showtable() showtable()
}) })
showform() showform()
} }
table.SetSelectedFunc(func(row, col int) {
editmsg(row - 1)
})
maindriver = func(event *tcell.EventKey) *tcell.EventKey { maindriver = func(event *tcell.EventKey) *tcell.EventKey {
switch event.Rune() { switch event.Rune() {
case 'd':
case 'e': case 'e':
editrepo("servermsg") r, _ := table.GetSelection()
r--
editmsg(r)
case 'q': case 'q':
app.Stop() app.Stop()
return nil return nil

View File

@ -2,6 +2,8 @@ changelog
-- next -- next
+ Somewhat functional admin console (TTY).
+ More JS free fallbacks for some basic functions. + More JS free fallbacks for some basic functions.
+ Add chpass command. + Add chpass command.

View File

@ -81,13 +81,20 @@ Site CSS may be overridden by creating a
.Pa views/local.css .Pa views/local.css
file. file.
.Pp .Pp
Custom messages may be set by editing the database. Custom HTML messages may be added to select pages by using the
(Sorry.) .Ic admin
.Dl update config set value = 'message' where key = 'servermsg'; command.
.Dl update config set value = 'message' where key = 'aboutmsg'; .Dl ./honk admin
.Dl update config set value = 'message' where key = 'loginmsg'; .Bl -tag -width tenletters
.It server
Displayed on the home page.
.It about
Displayed on the about page.
.It login
Displayed about the login form.
.El
.Pp .Pp
.Ss ADMIN .Ss USER ADMIN
New users can be added with the New users can be added with the
.Ic adduser .Ic adduser
command. command.