2021-04-03 10:25:02 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-04-03 11:05:23 -07:00
|
|
|
"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':
|
2021-04-03 11:05:23 -07:00
|
|
|
params := strings.Split(string(consoleInput), " ")
|
2021-04-03 10:38:24 -07:00
|
|
|
consoleInput = []rune{}
|
2021-04-03 11:05:23 -07:00
|
|
|
|
|
|
|
if chatCommands[params[0]].function == nil {
|
|
|
|
log.Print("Unknown command " + params[0] + ".")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-04-03 12:09:21 -07:00
|
|
|
if !chatCommands[params[0]].console {
|
|
|
|
log.Print("This command is not available to the console!")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-04-03 11:05:23 -07:00
|
|
|
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
|
|
|
}
|