multiserver/console.go

73 lines
1.3 KiB
Go
Raw Normal View History

2021-04-03 10:25:02 -07:00
package main
import (
"log"
"strings"
2021-04-03 10:38:24 -07:00
"unicode/utf8"
2021-04-03 10:25:02 -07:00
"github.com/tncardoso/gocurses"
)
var consoleInput []rune
func draw(msgs []string) {
gocurses.Clear()
row, _ := gocurses.Getmaxyx()
i := len(msgs)
for _, msg := range msgs {
gocurses.Mvaddstr(row-i-1, 0, msg)
i--
}
gocurses.Mvaddstr(row-i-1, 0, "> "+string(consoleInput))
gocurses.Refresh()
}
2021-04-03 10:38:24 -07:00
func initCurses(l *Logger) {
2021-04-03 10:25:02 -07:00
gocurses.Initscr()
gocurses.Cbreak()
gocurses.Noecho()
gocurses.Stdscr.Keypad(true)
2021-04-03 10:38:24 -07:00
go func() {
for {
var ch rune
ch1 := gocurses.Stdscr.Getch() % 255
if ch1 > 0x7F {
ch2 := gocurses.Stdscr.Getch()
ch, _ = utf8.DecodeRune([]byte{byte(ch1), byte(ch2)})
} else {
ch = rune(ch1)
}
switch ch {
case '\b':
if len(consoleInput) > 0 {
consoleInput = consoleInput[:len(consoleInput)-1]
}
case '\n':
params := strings.Split(string(consoleInput), " ")
2021-04-03 10:38:24 -07:00
consoleInput = []rune{}
if chatCommands[params[0]].function == nil {
log.Print("Unknown command " + params[0] + ".")
continue
}
if !chatCommands[params[0]].console {
log.Print("This command is not available to the console!")
continue
}
chatCommands[params[0]].function(nil, strings.Join(params[1:], " "))
2021-04-03 10:38:24 -07:00
default:
consoleInput = append(consoleInput, ch)
}
draw([]string(*l))
}
}()
2021-04-03 10:25:02 -07:00
}