Now with universal quitting action!
This commit is contained in:
parent
458f94f576
commit
f205887294
@ -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]
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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)
|
||||
}
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user