Add refactor change
This commit is contained in:
parent
3b91e741d7
commit
8aeed013a4
|
@ -6,6 +6,7 @@ type WinSize struct {
|
|||
}
|
||||
|
||||
type Color int
|
||||
|
||||
const (
|
||||
DefaultColor Color = iota
|
||||
|
||||
|
@ -36,7 +37,7 @@ type ConsoleParser interface {
|
|||
// TearDown
|
||||
TearDown() error
|
||||
// GetSCIICode returns ASCIICode correspond to input byte codes.
|
||||
GetASCIICode(b []byte) *ASCIICode
|
||||
GetKey(b []byte) Key
|
||||
// GetWinSize returns winsize struct which is the response of ioctl(2).
|
||||
GetWinSize() *WinSize
|
||||
}
|
||||
|
|
|
@ -1,102 +1,102 @@
|
|||
package prompt
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFilter(t *testing.T) {
|
||||
var scenarioTable = [] struct {
|
||||
var scenarioTable = []struct {
|
||||
scenario string
|
||||
filter CompletionFilter
|
||||
list []Completion
|
||||
substr string
|
||||
ignoreCase bool
|
||||
expected []Completion
|
||||
} {
|
||||
}{
|
||||
{
|
||||
scenario: "Contains don't ignore case",
|
||||
filter: FilterContains,
|
||||
list: []Completion{
|
||||
scenario: "Contains don't ignore case",
|
||||
filter: FilterContains,
|
||||
list: []Completion{
|
||||
{Text: "abcde"},
|
||||
{Text: "fghij"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
substr: "cd",
|
||||
ignoreCase: false,
|
||||
expected: []Completion{
|
||||
expected: []Completion{
|
||||
{Text: "abcde"},
|
||||
},
|
||||
},
|
||||
{
|
||||
scenario: "Contains ignore case",
|
||||
filter: FilterContains,
|
||||
list: []Completion{
|
||||
scenario: "Contains ignore case",
|
||||
filter: FilterContains,
|
||||
list: []Completion{
|
||||
{Text: "abcde"},
|
||||
{Text: "fghij"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
substr: "cd",
|
||||
ignoreCase: true,
|
||||
expected: []Completion{
|
||||
expected: []Completion{
|
||||
{Text: "abcde"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
},
|
||||
{
|
||||
scenario: "HasPrefix don't ignore case",
|
||||
filter: FilterHasPrefix,
|
||||
list: []Completion{
|
||||
scenario: "HasPrefix don't ignore case",
|
||||
filter: FilterHasPrefix,
|
||||
list: []Completion{
|
||||
{Text: "abcde"},
|
||||
{Text: "fghij"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
substr: "abc",
|
||||
ignoreCase: false,
|
||||
expected: []Completion{
|
||||
expected: []Completion{
|
||||
{Text: "abcde"},
|
||||
},
|
||||
},
|
||||
{
|
||||
scenario: "HasPrefix ignore case",
|
||||
filter: FilterHasPrefix,
|
||||
list: []Completion{
|
||||
scenario: "HasPrefix ignore case",
|
||||
filter: FilterHasPrefix,
|
||||
list: []Completion{
|
||||
{Text: "abcde"},
|
||||
{Text: "fabcj"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
substr: "abc",
|
||||
ignoreCase: true,
|
||||
expected: []Completion{
|
||||
expected: []Completion{
|
||||
{Text: "abcde"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
},
|
||||
{
|
||||
scenario: "HasSuffix don't ignore case",
|
||||
filter: FilterHasSuffix,
|
||||
list: []Completion{
|
||||
scenario: "HasSuffix don't ignore case",
|
||||
filter: FilterHasSuffix,
|
||||
list: []Completion{
|
||||
{Text: "abcde"},
|
||||
{Text: "fcdej"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
substr: "cde",
|
||||
ignoreCase: false,
|
||||
expected: []Completion{
|
||||
expected: []Completion{
|
||||
{Text: "abcde"},
|
||||
},
|
||||
},
|
||||
{
|
||||
scenario: "HasSuffix ignore case",
|
||||
filter: FilterHasSuffix,
|
||||
list: []Completion{
|
||||
scenario: "HasSuffix ignore case",
|
||||
filter: FilterHasSuffix,
|
||||
list: []Completion{
|
||||
{Text: "abcde"},
|
||||
{Text: "fcdej"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
substr: "cde",
|
||||
ignoreCase: true,
|
||||
expected: []Completion{
|
||||
expected: []Completion{
|
||||
{Text: "abcde"},
|
||||
{Text: "ABCDE"},
|
||||
},
|
||||
|
|
3
key.go
3
key.go
|
@ -115,4 +115,7 @@ const (
|
|||
|
||||
// Key which is ignored. (The key binding for this key should not do anything.)
|
||||
Ignore
|
||||
|
||||
// Key is not defined
|
||||
NotDefined
|
||||
)
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
package prompt
|
||||
|
||||
var InputHandler = defaultHandler
|
||||
|
||||
func defaultHandler(ac *ASCIICode, buffer *Buffer) {
|
||||
switch ac.Key {
|
||||
case Left:
|
||||
buffer.CursorLeft(1)
|
||||
case Right:
|
||||
buffer.CursorRight(1)
|
||||
case Backspace:
|
||||
buffer.DeleteBeforeCursor(1)
|
||||
case ControlI: // this is equivalent with TabKey.
|
||||
fallthrough
|
||||
case Tab:
|
||||
break
|
||||
case ControlT:
|
||||
break
|
||||
case Up:
|
||||
break
|
||||
case Down:
|
||||
break
|
||||
default:
|
||||
break
|
||||
}
|
||||
return
|
||||
}
|
|
@ -179,6 +179,7 @@ func NewPrompt(executor Executor, completer Completer, opts ...option) *Prompt {
|
|||
completer: completer,
|
||||
maxCompletions: 6,
|
||||
selected: -1,
|
||||
history: make([]string, 0, 10),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
|
|
35
prompt.go
35
prompt.go
|
@ -68,22 +68,9 @@ func (p *Prompt) Run() {
|
|||
|
||||
func (p *Prompt) feed(b []byte) (shouldExecute, shouldExit bool, input string) {
|
||||
shouldExecute = false
|
||||
ac := p.in.GetASCIICode(b)
|
||||
if ac == nil {
|
||||
if p.selected != -1 {
|
||||
c := p.completer(p.buf.Text())[p.selected]
|
||||
w := p.buf.Document().GetWordBeforeCursor()
|
||||
if w != "" {
|
||||
p.buf.DeleteBeforeCursor(len([]rune(w)))
|
||||
}
|
||||
p.buf.InsertText(c.Text, false, true)
|
||||
}
|
||||
p.selected = -1
|
||||
p.buf.InsertText(string(b), false, true)
|
||||
return
|
||||
}
|
||||
key := p.in.GetKey(b)
|
||||
|
||||
switch ac.Key {
|
||||
switch key {
|
||||
case ControlJ:
|
||||
fallthrough
|
||||
case Enter:
|
||||
|
@ -117,8 +104,24 @@ func (p *Prompt) feed(b []byte) (shouldExecute, shouldExit bool, input string) {
|
|||
fallthrough
|
||||
case Tab:
|
||||
p.selected += 1
|
||||
case Left:
|
||||
p.buf.CursorLeft(1)
|
||||
case Right:
|
||||
p.buf.CursorRight(1)
|
||||
case Backspace:
|
||||
p.buf.DeleteBeforeCursor(1)
|
||||
case NotDefined:
|
||||
if p.selected != -1 {
|
||||
c := p.completer(p.buf.Text())[p.selected]
|
||||
w := p.buf.Document().GetWordBeforeCursor()
|
||||
if w != "" {
|
||||
p.buf.DeleteBeforeCursor(len([]rune(w)))
|
||||
}
|
||||
p.buf.InsertText(c.Text, false, true)
|
||||
}
|
||||
p.selected = -1
|
||||
p.buf.InsertText(string(b), false, true)
|
||||
default:
|
||||
InputHandler(ac, p.buf)
|
||||
p.selected = -1
|
||||
}
|
||||
return
|
||||
|
|
12
render.go
12
render.go
|
@ -3,12 +3,12 @@ package prompt
|
|||
import "strings"
|
||||
|
||||
const (
|
||||
leftPrefix = " "
|
||||
leftSuffix = " "
|
||||
rightPrefix = " "
|
||||
rightSuffix = " "
|
||||
leftMargin = len(leftPrefix + leftSuffix)
|
||||
rightMargin = len(rightPrefix + rightSuffix)
|
||||
leftPrefix = " "
|
||||
leftSuffix = " "
|
||||
rightPrefix = " "
|
||||
rightSuffix = " "
|
||||
leftMargin = len(leftPrefix + leftSuffix)
|
||||
rightMargin = len(rightPrefix + rightSuffix)
|
||||
completionMargin = leftMargin + rightMargin
|
||||
)
|
||||
|
||||
|
|
|
@ -35,13 +35,13 @@ func (t *VT100Parser) TearDown() error {
|
|||
return termios.Tcsetattr(uintptr(t.fd), termios.TCSANOW, &t.origTermios)
|
||||
}
|
||||
|
||||
func (t *VT100Parser) GetASCIICode(b []byte) *ASCIICode {
|
||||
func (t *VT100Parser) GetKey(b []byte) Key {
|
||||
for _, k := range asciiSequences {
|
||||
if bytes.Compare(k.ASCIICode, b) == 0 {
|
||||
return k
|
||||
return k.Key
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return NotDefined
|
||||
}
|
||||
|
||||
// winsize is winsize struct got from the ioctl(2) system call.
|
||||
|
|
Loading…
Reference in New Issue