go-prompt/key_bind.go

63 lines
997 B
Go
Raw Normal View History

2017-08-13 04:09:45 +00:00
package prompt
2017-08-14 16:50:33 +00:00
type KeyBindFunc func(*Buffer)
2017-08-13 04:09:45 +00:00
type KeyBind struct {
Key Key
Fn KeyBindFunc
}
2017-08-13 04:33:51 +00:00
type KeyBindMode string
const (
CommonKeyBind KeyBindMode = "common"
2017-08-14 16:50:33 +00:00
EmacsKeyBind KeyBindMode = "emacs"
2017-08-13 04:33:51 +00:00
)
2017-08-14 16:50:33 +00:00
var commonKeyBindings = []KeyBind{
2017-08-13 04:09:45 +00:00
// Go to the End of the line
{
Key: End,
2017-08-14 16:50:33 +00:00
Fn: func(buf *Buffer) {
2017-08-13 04:09:45 +00:00
x := []rune(buf.Document().TextAfterCursor())
buf.CursorRight(len(x))
},
},
// Go to the beginning of the line
{
Key: Home,
2017-08-14 16:50:33 +00:00
Fn: func(buf *Buffer) {
2017-08-13 04:09:45 +00:00
x := []rune(buf.Document().TextBeforeCursor())
buf.CursorLeft(len(x))
},
},
// Delete character under the cursor
{
Key: Delete,
2017-08-14 16:50:33 +00:00
Fn: func(buf *Buffer) {
2017-08-13 04:09:45 +00:00
buf.Delete(1)
},
},
// Backspace
{
Key: Backspace,
2017-08-14 16:50:33 +00:00
Fn: func(buf *Buffer) {
2017-08-13 04:09:45 +00:00
buf.DeleteBeforeCursor(1)
},
},
// Right allow: Forward one character
{
Key: Right,
2017-08-14 16:50:33 +00:00
Fn: func(buf *Buffer) {
2017-08-13 04:09:45 +00:00
buf.CursorRight(1)
},
},
// Left allow: Backward one character
{
Key: Left,
2017-08-14 16:50:33 +00:00
Fn: func(buf *Buffer) {
2017-08-13 04:09:45 +00:00
buf.CursorLeft(1)
},
},
}