package editor import ( "strings" "timshome.page/gilo/fn" "timshome.page/gilo/terminal" ) // ---------------------------------------------------------------------------- // !Editor // ---------------------------------------------------------------------------- type cursor struct { x int y int } type editor struct { screen *terminal.Screen cursor *cursor } func New() *editor { screen := terminal.Size() cursor := &cursor { 0, 0 } return &editor{screen, cursor } } func (e *editor) ProcessKeypress() bool { var runes []rune // for ch, size := terminal.ReadKey(); size != 0; ch, size = terminal.ReadKey() { for i := 0; i < 3; i++ { ch, size := terminal.ReadKey() if size == 0 { break } if ch == fn.Ctrl('q') { // Clean up on exit terminal.Write(terminal.ClearScreen + terminal.ResetCursor) return false } runes = append(runes, ch) } terminal.Write("%v", runes) str := string(runes) runes = runes[:0] // 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) runes = runes[:0] return true default: // Do something later terminal.Write("Code: %v", str) } } return true } func (e *editor) moveCursor (rawKey string) { key := keyMap[rawKey] switch key { case keyLeft: e.cursor.x -= 1 case keyRight: e.cursor.x += 1 case keyUp: e.cursor.y -= 1 case keyDown: e.cursor.y += 1 } }