go-prompt/key_bind.go

68 lines
1.2 KiB
Go
Raw Permalink Normal View History

2017-08-13 04:09:45 +00:00
package prompt
2018-02-15 07:36:35 +00:00
// KeyBindFunc receives buffer and processed it.
2017-08-14 16:50:33 +00:00
type KeyBindFunc func(*Buffer)
2017-08-13 04:09:45 +00:00
2018-02-15 07:36:35 +00:00
// KeyBind represents which key should do what operation.
2017-08-13 04:09:45 +00:00
type KeyBind struct {
Key Key
Fn KeyBindFunc
}
2018-02-15 07:36:35 +00:00
// KeyBindMode to switch a key binding flexibly.
2017-08-13 04:33:51 +00:00
type KeyBindMode string
const (
2018-02-15 07:36:35 +00:00
// CommonKeyBind is a mode without any keyboard shortcut
2017-08-13 04:33:51 +00:00
CommonKeyBind KeyBindMode = "common"
2018-02-15 07:36:35 +00:00
// EmacsKeyBind is a mode to use emacs-like keyboard shortcut
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)
},
},
}