Now with universal quitting action!

This commit is contained in:
Timothy Warren 2021-03-26 13:35:58 -04:00
parent 458f94f576
commit f205887294
4 changed files with 43 additions and 26 deletions

View File

@ -75,32 +75,43 @@ func (e *editor) drawRows(ab *buffer) {
} }
func (e *editor) ProcessKeypress() bool { 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) { if strings.Contains(str, terminal.EscPrefix) {
code := strings.TrimPrefix(str, terminal.EscPrefix) code := strings.TrimPrefix(str, terminal.EscPrefix)
switch code { switch code {
case KeyArrowUp, KeyArrowDown, KeyArrowLeft, KeyArrowRight: case KeyArrowUp, KeyArrowDown, KeyArrowLeft, KeyArrowRight:
e.moveCursor(code) e.moveCursor(code)
return true
default: default:
// Do something later // 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 return true
} }
//func (e *editor) processEscapeCode() bool {
//
//}
func (e *editor) moveCursor (rawKey string) { func (e *editor) moveCursor (rawKey string) {
key := keyMap[rawKey] key := keyMap[rawKey]

View File

@ -16,13 +16,13 @@ const(
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
const ( const (
keyUp = "ARROW_UP" keyUp = '↑'
keyDown = "ARROW_DOWN" keyDown = '↓'
keyLeft = "ARROW_LEFT" keyLeft = '←'
keyRight = "ARROW_RIGHT" keyRight = '→'
) )
var keyMap = map[string]string{ var keyMap = map[string]rune{
KeyArrowUp: keyUp, KeyArrowUp: keyUp,
KeyArrowDown: keyDown, KeyArrowDown: keyDown,
KeyArrowLeft: keyLeft, KeyArrowLeft: keyLeft,

View File

@ -35,8 +35,10 @@ const (
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Add the ANSI escape code prefix to the relevant escape code // Add the ANSI escape code prefix to the relevant escape code
func Code (s string) string { func Code (s string, a ...interface{}) string {
return fmt.Sprintf("%s%s", EscPrefix, s) str := fmt.Sprintf(s, a...)
return EscPrefix + str
} }
@ -46,5 +48,5 @@ func MoveCursor(x int, y int) string {
x += 1 x += 1
y += 1 y += 1
return Code(fmt.Sprintf("%d;%dH", y, x)) return Code("%d;%dH", y, x)
} }

View File

@ -17,15 +17,19 @@ func ReadKey() (rune, int) {
return ch, size return ch, size
} }
func Read() (string, int) { func Read() string {
var buff []byte scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
size, err := reader.Read(buff) return scanner.Text()
if err != nil {
panic(err)
} }
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 // Print string to stdout