add ascii-code-bind-option-and-find-current-word-end

This commit is contained in:
Chyroc 2018-04-11 20:14:38 +08:00
parent de51277535
commit 6a9503cb3a
5 changed files with 97 additions and 29 deletions

View File

@ -97,7 +97,7 @@ func (b *Buffer) CursorLeft(count int) {
return
}
// CursorRight move to right on the current line.
// CursorLineEnd move to right on the current line.
func (b *Buffer) CursorRight(count int) {
l := b.Document().GetCursorRightPosition(count)
b.CursorPosition += l

View File

@ -4,6 +4,8 @@ import (
"sort"
"strings"
"unicode/utf8"
"io/ioutil"
"fmt"
)
// Document has text displayed in terminal and cursor position.
@ -55,6 +57,13 @@ func (d *Document) GetWordBeforeCursor() string {
return x[d.FindStartOfPreviousWord():]
}
// GetWordAfterCursor returns the word after the cursor.
// If we have whitespace after the cursor this returns an empty string.
func (d *Document) GetWordAfterCursor() string {
x := d.TextAfterCursor()
return x[:d.FindEndOfCurrentWord()]
}
// GetWordBeforeCursorWithSpace returns the word before the cursor.
// Unlike GetWordBeforeCursor, it returns string containing space
func (d *Document) GetWordBeforeCursorWithSpace() string {
@ -62,6 +71,13 @@ func (d *Document) GetWordBeforeCursorWithSpace() string {
return x[d.FindStartOfPreviousWordWithSpace():]
}
// GetWordAfterCursorWithSpace returns the word after the cursor.
// Unlike GetWordAfterCursor, it returns string containing space
func (d *Document) GetWordAfterCursorWithSpace() string {
x := d.TextAfterCursor()
return x[:d.FindEndOfCurrentWordWithSpace()]
}
// FindStartOfPreviousWord returns an index relative to the cursor position
// pointing to the start of the previous word. Return `None` if nothing was found.
func (d *Document) FindStartOfPreviousWord() int {
@ -76,6 +92,20 @@ func (d *Document) FindStartOfPreviousWord() int {
return 0
}
// FindEndOfCurrentWord returns an index relative to the cursor position
// pointing to the end of the current word. Return `None` if nothing was found.
func (d *Document) FindEndOfCurrentWord() int {
// Reverse the text before the cursor, in order to do an efficient backwards search.
x := d.TextAfterCursor()
l := len(x)
for i := 0; i < l; i++ {
if x[i:i+1] == " " {
return i
}
}
return 0
}
// FindStartOfPreviousWordWithSpace is almost the same as FindStartOfPreviousWord.
// The only difference is to ignore contiguous spaces.
func (d *Document) FindStartOfPreviousWordWithSpace() int {
@ -94,6 +124,28 @@ func (d *Document) FindStartOfPreviousWordWithSpace() int {
return 0
}
// FindEndOfCurrentWordWithSpace is almost the same as FindEndOfCurrentWord.
// The only difference is to ignore contiguous spaces.
func (d *Document) FindEndOfCurrentWordWithSpace() int {
// Reverse the text before the cursor, in order to do an efficient backwards search.
x := d.TextAfterCursor()
ioutil.WriteFile("/tmp/fff", []byte(fmt.Sprintf("[%s]", x)), 0644)
l := len(x)
appear := false
for i := 0; i < l; i++ {
if x[i:i+1] != " " {
appear = true
}
if x[i:i+1] == " " && appear {
return i
}
if i == l-1 {
return l
}
}
return 0
}
// CurrentLineBeforeCursor returns the text from the start of the line until the cursor.
func (d *Document) CurrentLineBeforeCursor() string {
s := strings.Split(d.TextBeforeCursor(), "\n")

View File

@ -9,6 +9,12 @@ type KeyBind struct {
Fn KeyBindFunc
}
// ASCIICodeBind represents which []byte should do what operation
type ASCIICodeBind struct {
ASCIICode []byte
Fn KeyBindFunc
}
// KeyBindMode to switch a key binding flexibly.
type KeyBindMode string
@ -23,45 +29,31 @@ var commonKeyBindings = []KeyBind{
// Go to the End of the line
{
Key: End,
Fn: func(buf *Buffer) {
x := []rune(buf.Document().TextAfterCursor())
buf.CursorRight(len(x))
},
Fn: GoLineEnd,
},
// Go to the beginning of the line
{
Key: Home,
Fn: func(buf *Buffer) {
x := []rune(buf.Document().TextBeforeCursor())
buf.CursorLeft(len(x))
},
Fn: GoLineBeginning,
},
// Delete character under the cursor
{
Key: Delete,
Fn: func(buf *Buffer) {
buf.Delete(1)
},
Fn: DeleteChar,
},
// Backspace
{
Key: Backspace,
Fn: func(buf *Buffer) {
buf.DeleteBeforeCursor(1)
},
Fn: DeleteBeforeChar,
},
// Right allow: Forward one character
{
Key: Right,
Fn: func(buf *Buffer) {
buf.CursorRight(1)
},
Fn: GoRightChar,
},
// Left allow: Backward one character
{
Key: Left,
Fn: func(buf *Buffer) {
buf.CursorLeft(1)
},
Fn: GoLeftChar,
},
}

View File

@ -209,6 +209,14 @@ func OptionAddKeyBind(b ...KeyBind) Option {
}
}
// OptionAddKeyBind to set a custom key bind.
func OptionAddASCIICodeBind(b ...ASCIICodeBind) Option {
return func(p *Prompt) error {
p.ASCIICodeBindings = append(p.ASCIICodeBindings, b...)
return nil
}
}
// New returns a Prompt with powerful auto-completion.
func New(executor Executor, completer Completer, opts ...Option) *Prompt {
pt := &Prompt{

View File

@ -5,6 +5,7 @@ import (
"log"
"os"
"time"
"bytes"
)
const (
@ -19,14 +20,15 @@ type Completer func(Document) []Suggest
// Prompt is core struct of go-prompt.
type Prompt struct {
in ConsoleParser
buf *Buffer
renderer *Render
executor Executor
history *History
completion *CompletionManager
keyBindings []KeyBind
keyBindMode KeyBindMode
in ConsoleParser
buf *Buffer
renderer *Render
executor Executor
history *History
completion *CompletionManager
keyBindings []KeyBind
ASCIICodeBindings []ASCIICodeBind
keyBindMode KeyBindMode
}
// Exec is the struct contains user input context.
@ -140,6 +142,9 @@ func (p *Prompt) feed(b []byte) (shouldExit bool, exec *Exec) {
return
}
case NotDefined:
if p.handleASCIICodeBinding(b) {
return
}
p.buf.InsertText(string(b), false, true)
}
@ -199,6 +204,17 @@ func (p *Prompt) handleKeyBinding(key Key) {
}
}
func (p *Prompt) handleASCIICodeBinding(b []byte) bool {
checked := false
for _, kb := range p.ASCIICodeBindings {
if bytes.Compare(kb.ASCIICode, b) == 0 {
kb.Fn(p.buf)
checked = true
}
}
return checked
}
// Input just returns user input text.
func (p *Prompt) Input() string {
if l := os.Getenv(envDebugLogPath); l == "" {