readline/complete.go

306 lines
6.3 KiB
Go
Raw Normal View History

2015-09-25 17:56:00 +03:00
package readline
import (
"bytes"
"fmt"
"io"
2015-10-04 16:56:34 +03:00
"github.com/chzyer/readline/runes"
2015-09-25 17:56:00 +03:00
)
2015-09-25 19:31:09 +03:00
type AutoCompleter interface {
GetName() []rune
GetChildren() []AutoCompleter
}
func Do(ac AutoCompleter, line []rune, pos int) (newLine [][]rune, offset int) {
line = line[:pos]
goNext := false
var lineCompleter AutoCompleter
for _, child := range ac.GetChildren() {
childName := child.GetName()
if len(line) >= len(childName) {
if runes.HasPrefix(line, childName) {
if len(line) == len(childName) {
newLine = append(newLine, []rune{' '})
} else {
newLine = append(newLine, childName)
}
offset = len(childName)
lineCompleter = child
goNext = true
}
} else {
if runes.HasPrefix(childName, line) {
newLine = append(newLine, childName[len(line):])
offset = len(line)
lineCompleter = child
}
}
}
if len(newLine) != 1 {
return
}
tmpLine := make([]rune, 0, len(line))
for i := offset; i < len(line); i++ {
if line[i] == ' ' {
continue
}
tmpLine = append(tmpLine, line[i:]...)
return Do(lineCompleter, tmpLine, len(tmpLine))
}
if goNext {
return Do(lineCompleter, nil, 0)
}
return
2015-09-25 19:31:09 +03:00
}
2015-09-25 17:56:00 +03:00
type opCompleter struct {
w io.Writer
op *Operation
ac AutoCompleter
inCompleteMode bool
inSelectMode bool
2015-09-27 05:33:19 +03:00
candidate [][]rune
candidateSource []rune
candidateOff int
candidateChoise int
candidateColNum int
2015-09-25 17:56:00 +03:00
}
func newOpCompleter(w io.Writer, op *Operation) *opCompleter {
return &opCompleter{
w: w,
op: op,
ac: op.cfg.AutoComplete,
}
}
func (o *opCompleter) doSelect() {
2015-09-27 05:33:19 +03:00
if len(o.candidate) == 1 {
o.op.buf.WriteRunes(o.candidate[0])
2015-09-25 17:56:00 +03:00
o.ExitCompleteMode(false)
return
}
2015-09-27 05:33:19 +03:00
o.nextCandidate(1)
2015-09-25 17:56:00 +03:00
o.CompleteRefresh()
}
2015-09-27 05:33:19 +03:00
func (o *opCompleter) nextCandidate(i int) {
o.candidateChoise += i
o.candidateChoise = o.candidateChoise % len(o.candidate)
if o.candidateChoise < 0 {
o.candidateChoise = len(o.candidate) + o.candidateChoise
2015-09-25 17:56:00 +03:00
}
}
func (o *opCompleter) OnComplete() {
if o.IsInCompleteSelectMode() {
o.doSelect()
return
}
buf := o.op.buf
rs := buf.Runes()
2015-10-04 16:56:34 +03:00
if o.IsInCompleteMode() && runes.Equal(rs, o.candidateSource) {
2015-09-25 17:56:00 +03:00
o.EnterCompleteSelectMode()
o.doSelect()
return
}
o.ExitCompleteSelectMode()
2015-09-27 05:33:19 +03:00
o.candidateSource = rs
newLines, offset := Do(o.ac, rs, buf.idx)
2015-09-25 17:56:00 +03:00
if len(newLines) == 0 {
o.ExitCompleteMode(false)
return
}
2015-09-27 05:33:19 +03:00
// only Aggregate candidates in non-complete mode
2015-09-25 17:56:00 +03:00
if !o.IsInCompleteMode() {
if len(newLines) == 1 {
buf.WriteRunes(newLines[0])
o.ExitCompleteMode(false)
return
}
2015-10-04 16:58:34 +03:00
same, size := runes.Aggregate(newLines)
2015-09-25 17:56:00 +03:00
if size > 0 {
buf.WriteRunes(same)
o.ExitCompleteMode(false)
return
}
}
o.EnterCompleteMode(offset, newLines)
}
func (o *opCompleter) IsInCompleteSelectMode() bool {
return o.inSelectMode
}
func (o *opCompleter) IsInCompleteMode() bool {
return o.inCompleteMode
}
func (o *opCompleter) HandleCompleteSelect(r rune) bool {
next := true
switch r {
case CharEnter, CharCtrlJ:
next = false
2015-09-27 05:33:19 +03:00
o.op.buf.WriteRunes(o.op.candidate[o.op.candidateChoise])
2015-09-25 17:56:00 +03:00
o.ExitCompleteMode(false)
case CharLineStart:
2015-09-27 05:33:19 +03:00
num := o.candidateChoise % o.candidateColNum
o.nextCandidate(-num)
2015-09-25 17:56:00 +03:00
case CharLineEnd:
2015-09-27 05:33:19 +03:00
num := o.candidateColNum - o.candidateChoise%o.candidateColNum - 1
o.candidateChoise += num
if o.candidateChoise >= len(o.candidate) {
o.candidateChoise = len(o.candidate) - 1
2015-09-25 17:56:00 +03:00
}
case CharBackspace:
o.ExitCompleteSelectMode()
next = false
case CharTab, CharForward:
o.doSelect()
2015-10-01 17:44:43 +03:00
case CharBell, CharInterrupt:
2015-09-25 17:56:00 +03:00
o.ExitCompleteMode(true)
next = false
case CharNext:
2015-09-27 05:33:19 +03:00
tmpChoise := o.candidateChoise + o.candidateColNum
2015-09-25 17:56:00 +03:00
if tmpChoise >= o.getMatrixSize() {
tmpChoise -= o.getMatrixSize()
2015-09-27 05:33:19 +03:00
} else if tmpChoise >= len(o.candidate) {
tmpChoise += o.candidateColNum
2015-09-25 17:56:00 +03:00
tmpChoise -= o.getMatrixSize()
}
2015-09-27 05:33:19 +03:00
o.candidateChoise = tmpChoise
2015-09-25 17:56:00 +03:00
case CharBackward:
2015-09-27 05:33:19 +03:00
o.nextCandidate(-1)
2015-09-25 17:56:00 +03:00
case CharPrev:
2015-09-27 05:33:19 +03:00
tmpChoise := o.candidateChoise - o.candidateColNum
2015-09-25 17:56:00 +03:00
if tmpChoise < 0 {
tmpChoise += o.getMatrixSize()
2015-09-27 05:33:19 +03:00
if tmpChoise >= len(o.candidate) {
tmpChoise -= o.candidateColNum
2015-09-25 17:56:00 +03:00
}
}
2015-09-27 05:33:19 +03:00
o.candidateChoise = tmpChoise
2015-09-25 17:56:00 +03:00
default:
next = false
2015-09-25 20:01:20 +03:00
o.ExitCompleteSelectMode()
2015-09-25 17:56:00 +03:00
}
if next {
o.CompleteRefresh()
return true
}
return false
}
func (o *opCompleter) getMatrixSize() int {
2015-09-27 05:33:19 +03:00
line := len(o.candidate) / o.candidateColNum
if len(o.candidate)%o.candidateColNum != 0 {
2015-09-25 17:56:00 +03:00
line++
}
2015-09-27 05:33:19 +03:00
return line * o.candidateColNum
2015-09-25 17:56:00 +03:00
}
func (o *opCompleter) CompleteRefresh() {
if !o.inCompleteMode {
return
}
lineCnt := o.op.buf.CursorLineCount()
colWidth := 0
2015-09-27 05:33:19 +03:00
for _, c := range o.candidate {
2015-10-04 16:56:34 +03:00
w := runes.WidthAll(c)
2015-09-25 17:56:00 +03:00
if w > colWidth {
colWidth = w
}
}
2015-09-27 05:33:19 +03:00
colNum := getWidth() / (colWidth + o.candidateOff + 2)
o.candidateColNum = colNum
2015-09-25 17:56:00 +03:00
buf := bytes.NewBuffer(nil)
buf.Write(bytes.Repeat([]byte("\n"), lineCnt))
2015-09-27 05:33:19 +03:00
same := o.op.buf.RuneSlice(-o.candidateOff)
2015-09-25 17:56:00 +03:00
colIdx := 0
lines := 1
buf.WriteString("\033[J")
2015-09-27 05:33:19 +03:00
for idx, c := range o.candidate {
inSelect := idx == o.candidateChoise && o.IsInCompleteSelectMode()
2015-09-25 17:56:00 +03:00
if inSelect {
buf.WriteString("\033[30;47m")
}
buf.WriteString(string(same))
buf.WriteString(string(c))
buf.Write(bytes.Repeat([]byte(" "), colWidth-len(c)))
if inSelect {
buf.WriteString("\033[0m")
}
buf.WriteString(" ")
colIdx++
if colIdx == colNum {
buf.WriteString("\n")
lines++
colIdx = 0
}
}
// move back
fmt.Fprintf(buf, "\033[%dA\r", lineCnt-1+lines)
fmt.Fprintf(buf, "\033[%dC", o.op.buf.idx+o.op.buf.PromptLen())
o.w.Write(buf.Bytes())
}
2015-09-27 05:33:19 +03:00
func (o *opCompleter) aggCandidate(candidate [][]rune) int {
2015-09-25 17:56:00 +03:00
offset := 0
2015-09-27 05:33:19 +03:00
for i := 0; i < len(candidate[0]); i++ {
for j := 0; j < len(candidate)-1; j++ {
if i > len(candidate[j]) {
2015-09-25 17:56:00 +03:00
goto aggregate
}
2015-09-27 05:33:19 +03:00
if candidate[j][i] != candidate[j+1][i] {
2015-09-25 17:56:00 +03:00
goto aggregate
}
}
offset = i
}
aggregate:
return offset
}
func (o *opCompleter) EnterCompleteSelectMode() {
o.inSelectMode = true
2015-09-27 05:33:19 +03:00
o.candidateChoise = -1
2015-09-25 17:56:00 +03:00
o.CompleteRefresh()
}
2015-09-27 05:33:19 +03:00
func (o *opCompleter) EnterCompleteMode(offset int, candidate [][]rune) {
2015-09-25 17:56:00 +03:00
o.inCompleteMode = true
2015-09-27 05:33:19 +03:00
o.candidate = candidate
o.candidateOff = offset
2015-09-25 17:56:00 +03:00
o.CompleteRefresh()
}
func (o *opCompleter) ExitCompleteSelectMode() {
o.inSelectMode = false
2015-09-27 05:33:19 +03:00
o.candidate = nil
o.candidateChoise = -1
o.candidateOff = -1
o.candidateSource = nil
2015-09-25 17:56:00 +03:00
}
func (o *opCompleter) ExitCompleteMode(revent bool) {
o.inCompleteMode = false
o.ExitCompleteSelectMode()
}