Better terminal input handling + Cursor movement (#80)

master
HimbeerserverDE 2021-05-02 17:57:17 +02:00
parent 132293e6b1
commit 2e9d4af86e
No known key found for this signature in database
GPG Key ID: 1A651504791E6A8B
1 changed files with 35 additions and 7 deletions

View File

@ -10,6 +10,7 @@ import (
) )
var consoleInput []rune var consoleInput []rune
var cursorPos int
func draw(msgs []string) { func draw(msgs []string) {
prompt, ok := ConfKey("console_prompt").(string) prompt, ok := ConfKey("console_prompt").(string)
@ -33,6 +34,11 @@ func draw(msgs []string) {
} }
gocurses.Mvaddstr(row-i-1, 0, prompt+string(consoleInput)) gocurses.Mvaddstr(row-i-1, 0, prompt+string(consoleInput))
if cursorPos >= len(consoleInput) {
cursorPos = len(consoleInput)
}
gocurses.Mvaddstr(row-i-1, len(prompt)+len(string(consoleInput))-cursorPos, "")
gocurses.Refresh() gocurses.Refresh()
} }
@ -101,13 +107,17 @@ func initCurses(l *Logger) {
for { for {
var ch rune var ch rune
ch1 := gocurses.Stdscr.Getch() % 255 ch1 := gocurses.Getch()
if ch1 > 0x7F { if ch1%255 > 0x7F {
ch2 := gocurses.Stdscr.Getch() ch2 := gocurses.Getch()
ch, _ = utf8.DecodeRune([]byte{byte(ch1), byte(ch2)}) ch, _ = utf8.DecodeRune([]byte{byte(ch1), byte(ch2)})
} else {
if ch1 != 3 && ch1 != 4 && ch1 != 5 && ch1 != 6 && ch1 != 339 && ch1 != 338 {
ch = rune(ch1 % 255)
} else { } else {
ch = rune(ch1) ch = rune(ch1)
} }
}
switch ch { switch ch {
case 3: case 3:
@ -115,6 +125,16 @@ func initCurses(l *Logger) {
case 4: case 4:
consoleInput = h.Prev(consoleInput) consoleInput = h.Prev(consoleInput)
case 5: case 5:
cursorPos += 1
if cursorPos > len(consoleInput) {
cursorPos = len(consoleInput)
}
case 6:
cursorPos -= 1
if cursorPos < 0 {
cursorPos = 0
}
case 339:
rows, _ := gocurses.Getmaxyx() rows, _ := gocurses.Getmaxyx()
start := len(l.lines) - rows + 1 - l.offset start := len(l.lines) - rows + 1 - l.offset
if start < 0 { if start < 0 {
@ -127,15 +147,19 @@ func initCurses(l *Logger) {
l.offset = len(l.lines) - 1 l.offset = len(l.lines) - 1
} }
} }
case 6: case 338:
l.offset -= 1 l.offset -= 1
if l.offset < 0 { if l.offset < 0 {
l.offset = 0 l.offset = 0
} }
case '\b': case '\b':
if len(consoleInput) > 0 { if len(consoleInput) > 0 {
if cursorPos > 0 {
consoleInput = append(consoleInput[:len(consoleInput)-cursorPos-1], consoleInput[len(consoleInput)-cursorPos:]...)
} else {
consoleInput = consoleInput[:len(consoleInput)-1] consoleInput = consoleInput[:len(consoleInput)-1]
} }
}
case '\t': case '\t':
if strings.Count(string(consoleInput), " ") > 0 { if strings.Count(string(consoleInput), " ") > 0 {
consoleInput = autoCompleteName(consoleInput) consoleInput = autoCompleteName(consoleInput)
@ -159,8 +183,12 @@ func initCurses(l *Logger) {
chatCommands[params[0]].function(nil, strings.Join(params[1:], " ")) chatCommands[params[0]].function(nil, strings.Join(params[1:], " "))
default: default:
if cursorPos > 0 {
consoleInput = append(consoleInput[:len(consoleInput)-cursorPos], append([]rune{ch}, consoleInput[len(consoleInput)-cursorPos:]...)...)
} else {
consoleInput = append(consoleInput, ch) consoleInput = append(consoleInput, ch)
} }
}
rows, _ := gocurses.Getmaxyx() rows, _ := gocurses.Getmaxyx()
start := len(l.lines) - rows + 1 - l.offset start := len(l.lines) - rows + 1 - l.offset