diff --git a/editor/editor.go b/editor/editor.go index 759246c..ccfba84 100644 --- a/editor/editor.go +++ b/editor/editor.go @@ -75,32 +75,43 @@ func (e *editor) drawRows(ab *buffer) { } func (e *editor) ProcessKeypress() bool { - str, _ := terminal.Read() + // Handle simplest inputs first + ch, _ := terminal.ReadKey() + if ch == fn.Ctrl('q') { + // Clean up on exit + terminal.Write(terminal.ClearScreen + terminal.ResetCursor) - // Escape sequences can be less fun... + return false + } else { + // Back up so that the input can be read as a string + terminal.Write(terminal.ClearScreen + terminal.ResetCursor) + fmt.Printf("%v\r\n", keyMap) + return false + // terminal.UnReadKey() + } + + str := terminal.Read() + + //// 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 } } - for _, ch := range str { - // Clean up on exit - if ch == fn.Ctrl('q') { - terminal.Write(terminal.ClearScreen + terminal.ResetCursor) - - return false - } - } - return true } +//func (e *editor) processEscapeCode() bool { +// +//} + func (e *editor) moveCursor (rawKey string) { key := keyMap[rawKey] diff --git a/editor/keymap.go b/editor/keymap.go index 72b8d7c..a8676b0 100644 --- a/editor/keymap.go +++ b/editor/keymap.go @@ -16,13 +16,13 @@ const( // ---------------------------------------------------------------------------- const ( - keyUp = "ARROW_UP" - keyDown = "ARROW_DOWN" - keyLeft = "ARROW_LEFT" - keyRight = "ARROW_RIGHT" + keyUp = '↑' + keyDown = '↓' + keyLeft = '←' + keyRight = '→' ) -var keyMap = map[string]string{ +var keyMap = map[string]rune{ KeyArrowUp: keyUp, KeyArrowDown: keyDown, KeyArrowLeft: keyLeft, diff --git a/terminal/ansi.go b/terminal/ansi.go index 3760c78..35246ac 100644 --- a/terminal/ansi.go +++ b/terminal/ansi.go @@ -35,8 +35,10 @@ const ( // ---------------------------------------------------------------------------- // Add the ANSI escape code prefix to the relevant escape code -func Code (s string) string { - return fmt.Sprintf("%s%s", EscPrefix, s) +func Code (s string, a ...interface{}) string { + str := fmt.Sprintf(s, a...) + + return EscPrefix + str } @@ -46,5 +48,5 @@ func MoveCursor(x int, y int) string { x += 1 y += 1 - return Code(fmt.Sprintf("%d;%dH", y, x)) + return Code("%d;%dH", y, x) } \ No newline at end of file diff --git a/terminal/io.go b/terminal/io.go index 4ed6eb5..9dcc283 100644 --- a/terminal/io.go +++ b/terminal/io.go @@ -17,15 +17,19 @@ func ReadKey() (rune, int) { return ch, size } -func Read() (string, int) { - var buff []byte - - size, err := reader.Read(buff) - if err != nil { - panic(err) +func Read() string { + scanner := bufio.NewScanner(os.Stdin) + if scanner.Scan() { + return scanner.Text() } - return string(buff), size + err := scanner.Err() + + if err != nil { + panic(fmt.Sprintf("Failed to read string from stdin %v", err)) + } + + return "(╯°□°)╯︵ ┻━┻" } // Print string to stdout