diff --git a/editor/editor.go b/editor/editor.go index a7b2646..3aea800 100644 --- a/editor/editor.go +++ b/editor/editor.go @@ -1,6 +1,7 @@ package editor import ( + "strings" "timshome.page/gilo/fn" "timshome.page/gilo/terminal" ) @@ -27,10 +28,9 @@ func New() *editor { } func (e *editor) ProcessKeypress() bool { - ch, size := terminal.ReadKey() var runes []rune - for ; size != 0; ch, size = terminal.ReadKey() { + for ch, size := terminal.ReadKey(); size != 0; ch, size = terminal.ReadKey() { if ch == fn.Ctrl('q') { // Clean up on exit terminal.Write(terminal.ClearScreen + terminal.ResetCursor) @@ -39,46 +39,30 @@ func (e *editor) ProcessKeypress() bool { } runes = append(runes, ch) - - print(runes) } terminal.Write("%v", runes) + str := string(runes) + runes = runes[:0] - return false + // Escape sequences can be less fun... + if strings.Contains(str, terminal.EscPrefix) { + code := strings.TrimPrefix(str, terminal.EscPrefix) - //str = terminal.Read() - // - //// Handle simplest inputs first - //ch, _ := terminal.ReadKey() + switch code { + case KeyArrowUp, KeyArrowDown, KeyArrowLeft, KeyArrowRight: + e.moveCursor(code) + runes = runes[:0] + return true + default: + // Do something later + terminal.Write("Code: %v", str) + } + } - - //runes := terminal.Read() - //str := string(runes) - // - //terminal.Write(terminal.ClearScreen + terminal.ResetCursor) - //terminal.WriteLn(fmt.Sprintf("%v\r\r", runes)) - // - ////// Escape sequences can be less fun... - //if strings.Contains(str, terminal.EscPrefix) { - // code := strings.TrimPrefix(str, terminal.EscPrefix) - // - // switch code { - // case KeyArrowUp, KeyArrowDown, KeyArrowLeft, KeyArrowRight: - // e.moveCursor(code) - // return true - // default: - // // Do something later - // } - //} - // - //return true + return true } -//func (e *editor) processEscapeCode() bool { -// -//} - func (e *editor) moveCursor (rawKey string) { key := keyMap[rawKey]