Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support meta-{backspace,left,right} key. #261

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions emacs.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,40 @@ var emacsKeyBindings = []KeyBind{
buf.CursorRight(1)
},
},
// Alt Right allow: Forward one word
{
Key: AltRight,
Fn: func(buf *Buffer) {
buf.CursorRight(buf.Document().FindEndOfCurrentWordWithSpace())
},
},
// Left allow: Backward one character
{
Key: ControlB,
Fn: func(buf *Buffer) {
buf.CursorLeft(1)
},
},
// Alt Left allow: Backward one word
{
Key: AltLeft,
Fn: func(buf *Buffer) {
buf.CursorLeft(len([]rune(buf.Document().TextBeforeCursor())) - buf.Document().FindStartOfPreviousWordWithSpace())
},
},
// Cut the Word before the cursor.
{
Key: ControlW,
Fn: func(buf *Buffer) {
buf.DeleteBeforeCursor(len([]rune(buf.Document().GetWordBeforeCursorWithSpace())))
},
},
{
Key: AltBackspace,
Fn: func(buf *Buffer) {
buf.DeleteBeforeCursor(len([]rune(buf.Document().GetWordBeforeCursorWithSpace())))
},
},
// Clear the Screen, similar to the clear command
{
Key: ControlL,
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/c-bata/go-prompt

go 1.14
go 1.22

require (
github.com/mattn/go-colorable v0.1.7
Expand All @@ -9,3 +9,5 @@ require (
github.com/pkg/term v1.2.0-beta.2
golang.org/x/sys v0.0.0-20200918174421-af09f7315aff
)

require github.com/mattn/go-isatty v0.0.12 // indirect
7 changes: 7 additions & 0 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,14 @@ var ASCIISequences = []*ASCIICode{
{Key: ControlCircumflex, ASCIICode: []byte{0x1e}},
{Key: ControlUnderscore, ASCIICode: []byte{0x1f}},
{Key: Backspace, ASCIICode: []byte{0x7f}},
{Key: AltBackspace, ASCIICode: []byte{0x1b, 0x7f}},

{Key: Up, ASCIICode: []byte{0x1b, 0x5b, 0x41}},
{Key: Down, ASCIICode: []byte{0x1b, 0x5b, 0x42}},
{Key: Right, ASCIICode: []byte{0x1b, 0x5b, 0x43}},
{Key: AltRight, ASCIICode: []byte{0x1b, 0x1b, 0x5b, 0x43}},
{Key: Left, ASCIICode: []byte{0x1b, 0x5b, 0x44}},
{Key: AltLeft, ASCIICode: []byte{0x1b, 0x1b, 0x5b, 0x44}},
{Key: Home, ASCIICode: []byte{0x1b, 0x5b, 0x48}},
{Key: Home, ASCIICode: []byte{0x1b, 0x30, 0x48}},
{Key: End, ASCIICode: []byte{0x1b, 0x5b, 0x46}},
Expand Down Expand Up @@ -166,4 +169,8 @@ var ASCIISequences = []*ASCIICode{

{Key: Ignore, ASCIICode: []byte{0x1b, 0x5b, 0x45}}, // Xterm
{Key: Ignore, ASCIICode: []byte{0x1b, 0x5b, 0x46}}, // Linux console

// MacOS Keybind
{Key: AltRight, ASCIICode: []byte{0x1b, 0x66}},
{Key: AltLeft, ASCIICode: []byte{0x1b, 0x62}},
}
3 changes: 2 additions & 1 deletion internal/term/raw.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

package term
Expand Down Expand Up @@ -25,5 +26,5 @@ func SetRaw(fd int) error {
n.Cc[syscall.VMIN] = 1
n.Cc[syscall.VTIME] = 0

return termios.Tcsetattr(uintptr(fd), termios.TCSANOW, (*unix.Termios)(n))
return termios.Tcsetattr(uintptr(fd), termios.TCSANOW, (*unix.Termios)(&n))
}
7 changes: 4 additions & 3 deletions internal/term/term.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

package term
Expand All @@ -15,13 +16,13 @@ var (
saveTermiosOnce sync.Once
)

func getOriginalTermios(fd int) (*unix.Termios, error) {
func getOriginalTermios(fd int) (unix.Termios, error) {
var err error
saveTermiosOnce.Do(func() {
saveTermiosFD = fd
saveTermios, err = termios.Tcgetattr(uintptr(fd))
})
return saveTermios, err
return *saveTermios, err
}

// Restore terminal's mode.
Expand All @@ -30,5 +31,5 @@ func Restore() error {
if err != nil {
return err
}
return termios.Tcsetattr(uintptr(saveTermiosFD), termios.TCSANOW, o)
return termios.Tcsetattr(uintptr(saveTermiosFD), termios.TCSANOW, &o)
}
3 changes: 3 additions & 0 deletions key.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.