make the admin command suck less

This commit is contained in:
Ted Unangst 2019-11-23 16:36:03 -05:00
parent c257424b1d
commit d200046210
1 changed files with 76 additions and 25 deletions

View File

@ -26,6 +26,7 @@ import (
"log" "log"
"os" "os"
"os/signal" "os/signal"
"strings"
) )
func adminscreen() { func adminscreen() {
@ -73,9 +74,7 @@ func adminscreen() {
clearscreen := func() { clearscreen := func() {
stdout.WriteString(esc + "[2J") stdout.WriteString(esc + "[2J")
} }
clearline := func() { //clearline := func() { stdout.WriteString(esc + "[2K") }
stdout.WriteString(esc + "[2K")
}
colorfn := func(code int) func(string) string { colorfn := func(code int) func(string) string {
return func(s string) string { return func(s string) string {
return fmt.Sprintf(esc+"[%dm"+"%s"+esc+"[0m", code, s) return fmt.Sprintf(esc+"[%dm"+"%s"+esc+"[0m", code, s)
@ -120,8 +119,36 @@ func adminscreen() {
stdout.Flush() stdout.Flush()
} }
editing := false
linecount := func(s string) int {
lines := 1
for i := range s {
if s[i] == '\n' {
lines++
}
}
return lines
}
msglineno := func(idx int) int { msglineno := func(idx int) int {
return 4 + idx*2 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)
} }
drawmessage := func(idx int) { drawmessage := func(idx int) {
@ -130,8 +157,12 @@ func adminscreen() {
label := messages[idx].label label := messages[idx].label
if idx == cursel { if idx == cursel {
label = reverse(label) label = reverse(label)
if editing {
label = magenta(label)
} }
stdout.WriteString(fmt.Sprintf("%s %s", label, messages[idx].text)) }
text := forscreen(messages[idx].text)
stdout.WriteString(fmt.Sprintf("%s\n %s", label, text))
} }
drawscreen := func() { drawscreen := func() {
@ -139,55 +170,75 @@ func adminscreen() {
movecursor(4, msglineno(-1)) movecursor(4, msglineno(-1))
stdout.WriteString(magenta(serverName + " admin panel")) stdout.WriteString(magenta(serverName + " admin panel"))
for i := range messages { for i := range messages {
if !editing || i != cursel {
drawmessage(i) drawmessage(i)
} }
}
movecursor(4, msglineno(len(messages))) movecursor(4, msglineno(len(messages)))
stdout.WriteString(magenta("j/k to move - q to quit - enter to edit")) 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)
}
stdout.Flush() stdout.Flush()
} }
selectnext := func() { selectnext := func() {
if cursel < len(messages)-1 { if cursel < len(messages)-1 {
movecursor(4, msglineno(cursel))
stdout.WriteString(messages[cursel].label)
cursel++ cursel++
movecursor(4, msglineno(cursel))
stdout.WriteString(reverse(messages[cursel].label))
stdout.Flush()
} }
drawscreen()
} }
selectprev := func() { selectprev := func() {
if cursel > 0 { if cursel > 0 {
movecursor(4, msglineno(cursel))
stdout.WriteString(messages[cursel].label)
cursel-- cursel--
movecursor(4, msglineno(cursel))
stdout.WriteString(reverse(messages[cursel].label))
stdout.Flush()
} }
drawscreen()
} }
editsel := func() { editsel := func() {
movecursor(4, msglineno(cursel)) editing = true
clearline()
m := messages[cursel]
stdout.WriteString(reverse(magenta(m.label)))
text := m.text
stdout.WriteString(" ")
stdout.WriteString(text)
showcursor() showcursor()
stdout.Flush() drawscreen()
m := messages[cursel]
loop: loop:
for { for {
c := readchar() c := readchar()
switch c { switch c {
case '\n': case '\x1b':
break loop break loop
case '\n':
m.text += "\n"
drawscreen()
case 127: case 127:
if len(text) > 0 { 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() moveleft()
stdout.WriteString(" ") stdout.WriteString(" ")
moveleft() moveleft()
text = text[:len(text)-1] }
} }
default: default:
text = text + string(c) m.text += string(c)
stdout.WriteString(string(c)) stdout.WriteString(string(c))
} }
stdout.Flush() stdout.Flush()
} }
m.text = text editing = false
updateconfig(m.name, m.text) updateconfig(m.name, m.text)
hidecursor() hidecursor()
drawscreen() drawscreen()