all repos — honk @ v0.8.1

my fork of honk

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	"io/ioutil"
 20	"log"
 21
 22	"github.com/gdamore/tcell"
 23	"github.com/rivo/tview"
 24)
 25
 26func adminscreen() {
 27	log.SetOutput(ioutil.Discard)
 28
 29	messages := []*struct {
 30		name  string
 31		label string
 32		text  string
 33	}{
 34		{
 35			name:  "servermsg",
 36			label: "server",
 37			text:  string(serverMsg),
 38		},
 39		{
 40			name:  "aboutmsg",
 41			label: "about",
 42			text:  string(aboutMsg),
 43		},
 44		{
 45			name:  "loginmsg",
 46			label: "login",
 47			text:  string(loginMsg),
 48		},
 49	}
 50
 51	app := tview.NewApplication()
 52	var maindriver func(event *tcell.EventKey) *tcell.EventKey
 53
 54	table := tview.NewTable().SetFixed(1, 0).SetSelectable(true, false).
 55		SetSelectedStyle(tcell.ColorBlack, tcell.ColorPurple, 0)
 56
 57	mainframe := tview.NewFrame(table)
 58	mainframe.AddText(tview.Escape("honk admin - [q] quit"),
 59		true, 0, tcell.ColorPurple)
 60	mainframe.SetBorders(1, 0, 1, 0, 4, 0)
 61
 62	dupecell := func(base *tview.TableCell) *tview.TableCell {
 63		rv := new(tview.TableCell)
 64		*rv = *base
 65		return rv
 66	}
 67
 68	showtable := func() {
 69		table.Clear()
 70
 71		row := 0
 72		{
 73			col := 0
 74			headcell := tview.TableCell{
 75				Color:         tcell.ColorWhite,
 76				NotSelectable: true,
 77			}
 78			cell := dupecell(&headcell)
 79			cell.Text = "which       "
 80			table.SetCell(row, col, cell)
 81			col++
 82			cell = dupecell(&headcell)
 83			cell.Text = "message"
 84			table.SetCell(row, col, cell)
 85
 86			row++
 87		}
 88		for i := 0; i < 3; i++ {
 89			col := 0
 90			msg := messages[i]
 91			headcell := tview.TableCell{
 92				Color: tcell.ColorWhite,
 93			}
 94			cell := dupecell(&headcell)
 95			cell.Text = msg.label
 96			table.SetCell(row, col, cell)
 97			col++
 98			cell = dupecell(&headcell)
 99			cell.Text = tview.Escape(msg.text)
100			table.SetCell(row, col, cell)
101
102			row++
103		}
104
105		app.SetInputCapture(maindriver)
106		app.SetRoot(mainframe, true)
107	}
108
109	arrowadapter := func(event *tcell.EventKey) *tcell.EventKey {
110		switch event.Key() {
111		case tcell.KeyDown:
112			return tcell.NewEventKey(tcell.KeyTab, '\t', tcell.ModNone)
113		case tcell.KeyUp:
114			return tcell.NewEventKey(tcell.KeyBacktab, '\t', tcell.ModNone)
115		}
116		return event
117	}
118
119	editform := tview.NewForm()
120	descbox := tview.NewInputField().SetLabel("msg: ").SetFieldWidth(60)
121	editform.AddButton("save", nil)
122	editform.AddButton("cancel", nil)
123	savebutton := editform.GetButton(0)
124	editform.SetFieldTextColor(tcell.ColorBlack)
125	editform.SetFieldBackgroundColor(tcell.ColorPurple)
126	editform.SetLabelColor(tcell.ColorWhite)
127	editform.SetButtonTextColor(tcell.ColorPurple)
128	editform.SetButtonBackgroundColor(tcell.ColorBlack)
129	editform.GetButton(1).SetSelectedFunc(showtable)
130	editform.SetCancelFunc(showtable)
131
132	editframe := tview.NewFrame(editform)
133	editframe.SetBorders(1, 0, 1, 0, 4, 0)
134
135	showform := func() {
136		editform.Clear(false)
137		editform.AddFormItem(descbox)
138		app.SetInputCapture(arrowadapter)
139		app.SetRoot(editframe, true)
140	}
141
142	editmsg := func(which int) {
143		msg := messages[which]
144		editframe.Clear()
145		editframe.AddText(tview.Escape("edit "+msg.label+" message"),
146			true, 0, tcell.ColorPurple)
147		descbox.SetText(msg.text)
148		savebutton.SetSelectedFunc(func() {
149			msg.text = descbox.GetText()
150			updateconfig(msg.name, msg.text)
151			showtable()
152		})
153		showform()
154	}
155
156	table.SetSelectedFunc(func(row, col int) {
157		editmsg(row - 1)
158	})
159
160	maindriver = func(event *tcell.EventKey) *tcell.EventKey {
161		switch event.Rune() {
162		case 'e':
163			r, _ := table.GetSelection()
164			r--
165			editmsg(r)
166		case 'q':
167			app.Stop()
168			return nil
169		}
170		return event
171	}
172
173	showtable()
174	app.Run()
175}