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
|
|
|
|
|
2019-11-10 23:21:49 +01:00
|
|
|
/*
|
|
|
|
#include <termios.h>
|
|
|
|
*/
|
|
|
|
import "C"
|
2019-10-21 01:07:26 +02:00
|
|
|
import (
|
2019-11-11 01:13:28 +01:00
|
|
|
"bufio"
|
2019-11-10 23:21:49 +01:00
|
|
|
"fmt"
|
2019-10-21 01:07:26 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2019-11-10 23:21:49 +01:00
|
|
|
"os"
|
2019-11-11 02:20:56 +01:00
|
|
|
"os/signal"
|
2019-11-23 22:36:03 +01:00
|
|
|
"strings"
|
2019-10-21 01:07:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func adminscreen() {
|
|
|
|
log.SetOutput(ioutil.Discard)
|
2019-11-11 01:13:28 +01:00
|
|
|
stdout := bufio.NewWriter(os.Stdout)
|
2019-11-10 23:21:49 +01:00
|
|
|
esc := "\x1b"
|
|
|
|
smcup := esc + "[?1049h"
|
|
|
|
rmcup := esc + "[?1049l"
|
2019-10-21 01:07:26 +02:00
|
|
|
|
2019-11-11 01:13:28 +01: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),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cursel := 0
|
|
|
|
|
2019-11-10 23:21:49 +01:00
|
|
|
hidecursor := func() {
|
2019-11-11 01:13:28 +01:00
|
|
|
stdout.WriteString(esc + "[?25l")
|
2019-10-21 02:09:44 +02:00
|
|
|
}
|
2019-11-10 23:21:49 +01:00
|
|
|
showcursor := func() {
|
2019-11-11 01:13:28 +01:00
|
|
|
stdout.WriteString(esc + "[?12;25h")
|
2019-10-21 01:07:26 +02:00
|
|
|
}
|
2019-11-10 23:21:49 +01:00
|
|
|
movecursor := func(x, y int) {
|
2019-11-11 01:13:28 +01:00
|
|
|
stdout.WriteString(fmt.Sprintf(esc+"[%d;%dH", y, x))
|
|
|
|
}
|
|
|
|
moveleft := func() {
|
|
|
|
stdout.WriteString(esc + "[1D")
|
2019-10-21 01:07:26 +02:00
|
|
|
}
|
2019-11-10 23:21:49 +01:00
|
|
|
clearscreen := func() {
|
|
|
|
stdout.WriteString(esc + "[2J")
|
2019-10-21 01:07:26 +02:00
|
|
|
}
|
2019-11-23 22:36:03 +01:00
|
|
|
//clearline := func() { stdout.WriteString(esc + "[2K") }
|
2019-11-11 01:13:28 +01:00
|
|
|
colorfn := func(code int) func(string) string {
|
|
|
|
return func(s string) string {
|
|
|
|
return fmt.Sprintf(esc+"[%dm"+"%s"+esc+"[0m", code, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reverse := colorfn(7)
|
|
|
|
magenta := colorfn(35)
|
|
|
|
readchar := func() byte {
|
|
|
|
var buf [1]byte
|
|
|
|
os.Stdin.Read(buf[:])
|
|
|
|
c := buf[0]
|
|
|
|
return c
|
|
|
|
}
|
2019-10-21 01:07:26 +02:00
|
|
|
|
2019-11-10 23:21:49 +01:00
|
|
|
savedtio := new(C.struct_termios)
|
|
|
|
C.tcgetattr(1, savedtio)
|
|
|
|
restore := func() {
|
|
|
|
stdout.WriteString(rmcup)
|
|
|
|
showcursor()
|
2019-11-11 01:13:28 +01:00
|
|
|
stdout.Flush()
|
2019-11-10 23:21:49 +01:00
|
|
|
C.tcsetattr(1, C.TCSAFLUSH, savedtio)
|
2019-10-21 01:07:26 +02:00
|
|
|
}
|
2019-11-10 23:21:49 +01:00
|
|
|
defer restore()
|
2019-11-11 02:20:56 +01:00
|
|
|
go func() {
|
|
|
|
sig := make(chan os.Signal)
|
|
|
|
signal.Notify(sig, os.Interrupt)
|
|
|
|
<-sig
|
|
|
|
restore()
|
|
|
|
os.Exit(0)
|
|
|
|
}()
|
2019-11-10 23:21:49 +01:00
|
|
|
|
|
|
|
init := func() {
|
|
|
|
tio := new(C.struct_termios)
|
|
|
|
C.tcgetattr(1, tio)
|
|
|
|
tio.c_lflag = tio.c_lflag & ^C.uint(C.ECHO|C.ICANON)
|
|
|
|
C.tcsetattr(1, C.TCSADRAIN, tio)
|
|
|
|
|
|
|
|
hidecursor()
|
|
|
|
stdout.WriteString(smcup)
|
|
|
|
clearscreen()
|
|
|
|
movecursor(1, 1)
|
2019-11-11 01:13:28 +01:00
|
|
|
stdout.Flush()
|
|
|
|
}
|
|
|
|
|
2019-11-23 22:36:03 +01:00
|
|
|
editing := false
|
|
|
|
|
|
|
|
linecount := func(s string) int {
|
|
|
|
lines := 1
|
|
|
|
for i := range s {
|
|
|
|
if s[i] == '\n' {
|
|
|
|
lines++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
|
2019-11-11 01:13:28 +01:00
|
|
|
msglineno := func(idx int) int {
|
2019-11-23 22:36:03 +01:00
|
|
|
off := 2
|
|
|
|
if idx == -1 {
|
|
|
|
return off
|
|
|
|
}
|
|
|
|
for i, m := range messages {
|
|
|
|
off += 2
|
|
|
|
if i == idx {
|
|
|
|
return off
|
|
|
|
}
|
|
|
|
off += linecount(m.text)
|
|
|
|
}
|
|
|
|
off += 2
|
|
|
|
return off
|
|
|
|
}
|
|
|
|
|
|
|
|
forscreen := func(s string) string {
|
|
|
|
return strings.Replace(s, "\n", "\n ", -1)
|
2019-11-11 01:13:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
drawmessage := func(idx int) {
|
|
|
|
line := msglineno(idx)
|
|
|
|
movecursor(4, line)
|
|
|
|
label := messages[idx].label
|
|
|
|
if idx == cursel {
|
|
|
|
label = reverse(label)
|
2019-11-23 22:36:03 +01:00
|
|
|
if editing {
|
|
|
|
label = magenta(label)
|
|
|
|
}
|
2019-11-11 01:13:28 +01:00
|
|
|
}
|
2019-11-23 22:36:03 +01:00
|
|
|
text := forscreen(messages[idx].text)
|
|
|
|
stdout.WriteString(fmt.Sprintf("%s\n %s", label, text))
|
2019-11-11 01:13:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
drawscreen := func() {
|
|
|
|
clearscreen()
|
|
|
|
movecursor(4, msglineno(-1))
|
|
|
|
stdout.WriteString(magenta(serverName + " admin panel"))
|
|
|
|
for i := range messages {
|
2019-11-23 22:36:03 +01:00
|
|
|
if !editing || i != cursel {
|
|
|
|
drawmessage(i)
|
|
|
|
}
|
2019-11-11 01:13:28 +01:00
|
|
|
}
|
|
|
|
movecursor(4, msglineno(len(messages)))
|
2019-11-23 22:36:03 +01:00
|
|
|
dir := "j/k to move - q to quit - enter to edit"
|
|
|
|
if editing {
|
|
|
|
dir = "esc to end"
|
|
|
|
}
|
|
|
|
stdout.WriteString(magenta(dir))
|
|
|
|
if editing {
|
|
|
|
drawmessage(cursel)
|
|
|
|
}
|
2019-11-11 01:13:28 +01:00
|
|
|
stdout.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
selectnext := func() {
|
|
|
|
if cursel < len(messages)-1 {
|
2019-11-23 22:36:03 +01:00
|
|
|
movecursor(4, msglineno(cursel))
|
|
|
|
stdout.WriteString(messages[cursel].label)
|
2019-11-11 01:13:28 +01:00
|
|
|
cursel++
|
2019-11-23 22:36:03 +01:00
|
|
|
movecursor(4, msglineno(cursel))
|
|
|
|
stdout.WriteString(reverse(messages[cursel].label))
|
|
|
|
stdout.Flush()
|
2019-11-11 01:13:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
selectprev := func() {
|
|
|
|
if cursel > 0 {
|
2019-11-23 22:36:03 +01:00
|
|
|
movecursor(4, msglineno(cursel))
|
|
|
|
stdout.WriteString(messages[cursel].label)
|
2019-11-11 01:13:28 +01:00
|
|
|
cursel--
|
2019-11-23 22:36:03 +01:00
|
|
|
movecursor(4, msglineno(cursel))
|
|
|
|
stdout.WriteString(reverse(messages[cursel].label))
|
|
|
|
stdout.Flush()
|
2019-11-11 01:13:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
editsel := func() {
|
2019-11-23 22:36:03 +01:00
|
|
|
editing = true
|
2019-11-11 01:13:28 +01:00
|
|
|
showcursor()
|
2019-11-23 22:36:03 +01:00
|
|
|
drawscreen()
|
|
|
|
m := messages[cursel]
|
2019-11-11 01:13:28 +01:00
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
c := readchar()
|
|
|
|
switch c {
|
2019-11-23 22:36:03 +01:00
|
|
|
case '\x1b':
|
2019-11-11 01:13:28 +01:00
|
|
|
break loop
|
2019-11-23 22:36:03 +01:00
|
|
|
case '\n':
|
|
|
|
m.text += "\n"
|
|
|
|
drawscreen()
|
2019-11-11 01:13:28 +01:00
|
|
|
case 127:
|
2019-11-23 22:36:03 +01:00
|
|
|
if len(m.text) > 0 {
|
|
|
|
last := m.text[len(m.text)-1]
|
|
|
|
m.text = m.text[:len(m.text)-1]
|
|
|
|
if last == '\n' {
|
|
|
|
drawscreen()
|
|
|
|
} else {
|
|
|
|
moveleft()
|
|
|
|
stdout.WriteString(" ")
|
|
|
|
moveleft()
|
|
|
|
}
|
2019-11-11 01:13:28 +01:00
|
|
|
}
|
|
|
|
default:
|
2019-11-23 22:36:03 +01:00
|
|
|
m.text += string(c)
|
2019-11-11 01:13:28 +01:00
|
|
|
stdout.WriteString(string(c))
|
|
|
|
}
|
|
|
|
stdout.Flush()
|
|
|
|
}
|
2019-11-23 22:36:03 +01:00
|
|
|
editing = false
|
2019-11-11 01:13:28 +01:00
|
|
|
updateconfig(m.name, m.text)
|
|
|
|
hidecursor()
|
|
|
|
drawscreen()
|
2019-10-21 01:07:26 +02:00
|
|
|
}
|
|
|
|
|
2019-11-10 23:21:49 +01:00
|
|
|
init()
|
2019-11-11 01:13:28 +01:00
|
|
|
drawscreen()
|
2019-10-21 02:09:44 +02:00
|
|
|
|
2019-11-10 23:21:49 +01:00
|
|
|
for {
|
2019-11-11 01:13:28 +01:00
|
|
|
c := readchar()
|
2019-11-10 23:21:49 +01:00
|
|
|
switch c {
|
2019-10-21 01:07:26 +02:00
|
|
|
case 'q':
|
2019-11-10 23:21:49 +01:00
|
|
|
return
|
2019-11-11 01:13:28 +01:00
|
|
|
case 'j':
|
|
|
|
selectnext()
|
|
|
|
case 'k':
|
|
|
|
selectprev()
|
|
|
|
case '\n':
|
|
|
|
editsel()
|
2019-11-10 23:21:49 +01:00
|
|
|
default:
|
2019-11-11 01:13:28 +01:00
|
|
|
|
2019-10-21 01:07:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|