Gerrit Bot has uploaded this change for review.
Add aliases for alt-right, alt-left and alt-delete
I noticed that <kbd>option</kbd>+<kbd>→</kbd> (move right by word), <kbd>option</kbd>+<kbd>←</kbd> (move left by word) and <kbd>option</kbd>+<kbd>delete</kbd> (delete by word) weren't working, at least on macOS. Used raw mode and some debugging to figure out what escape codes were being signaled.
This PR also removes two unnecessary checks for whether the first byte is the escape character. (Unnecessary because the function returns earlier if the first byte _isn't_ the escape character)
Sample program to see the change this PR makes:
Run this and then compare how alt-right, alt-left and alt-delete work with this change and without.
```go
package main
import (
"fmt"
"os"
"golang.org/x/term"
)
func main() {
fmt.Println("Use q to quit")
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
defer term.Restore(int(os.Stdin.Fd()), oldState)
t := term.NewTerminal(os.Stdin, "> ")
for {
line, err := t.ReadLine()
if err != nil {
fmt.Println(err)
break
}
if line == "q" {
break
}
t.Write([]byte(line + "\n"))
}
}
```
Change-Id: Id6cbac86a88eedf1bc37c9f80df5315643d2423c
GitHub-Last-Rev: 61c099772b8aa7c6407b61f48b17e52a20983e0c
GitHub-Pull-Request: golang/term#7
---
M terminal.go
1 file changed, 65 insertions(+), 2 deletions(-)
diff --git a/terminal.go b/terminal.go
index 535ab82..dea246c 100644
--- a/terminal.go
+++ b/terminal.go
@@ -181,7 +181,20 @@
return r, b[l:]
}
- if !pasteActive && len(b) >= 3 && b[0] == keyEscape && b[1] == '[' {
+ // b[0] == keyEscape from now on
+
+ if !pasteActive && len(b) >= 2 {
+ switch b[1] {
+ case 'b':
+ return keyAltLeft, b[2:]
+ case 'f':
+ return keyAltRight, b[2:]
+ case 127:
+ return keyDeleteWord, b[2:]
+ }
+ }
+
+ if !pasteActive && len(b) >= 3 && b[1] == '[' {
switch b[2] {
case 'A':
return keyUp, b[3:]
@@ -198,7 +211,7 @@
}
}
- if !pasteActive && len(b) >= 6 && b[0] == keyEscape && b[1] == '[' && b[2] == '1' && b[3] == ';' && b[4] == '3' {
+ if !pasteActive && len(b) >= 6 && b[1] == '[' && b[2] == '1' && b[3] == ';' && b[4] == '3' {
switch b[5] {
case 'C':
return keyAltRight, b[6:]
To view, visit change 383594. To unsubscribe, or for help writing mail filters, visit settings.