readline/complete.go

387 lines
8.8 KiB
Go
Raw Normal View History

2015-09-25 17:56:00 +03:00
package readline
import (
"bufio"
2015-09-25 17:56:00 +03:00
"bytes"
"fmt"
"io"
)
2015-09-25 19:31:09 +03:00
type AutoCompleter interface {
2015-09-27 05:33:19 +03:00
// Readline will pass the whole line and current offset to it
// Completer need to pass all the candidates, and how long they shared the same characters in line
// Example:
2016-04-01 20:23:53 +03:00
// [go, git, git-shell, grep]
2015-09-27 05:33:19 +03:00
// Do("g", 1) => ["o", "it", "it-shell", "rep"], 1
2016-04-01 20:23:53 +03:00
// Do("gi", 2) => ["t", "t-shell"], 2
// Do("git", 3) => ["", "-shell"], 3
2015-09-27 05:33:19 +03:00
Do(line []rune, pos int) (newLine [][]rune, length int)
2015-09-25 19:31:09 +03:00
}
2016-09-03 06:26:39 +03:00
type TabCompleter struct{}
func (t *TabCompleter) Do([]rune, int) ([][]rune, int) {
return [][]rune{[]rune("\t")}, 0
}
2015-09-25 17:56:00 +03:00
type opCompleter struct {
2016-03-13 13:32:48 +03:00
w io.Writer
op *Operation
width int
2015-09-25 17:56:00 +03:00
inCompleteMode bool
inSelectMode bool
2015-09-27 05:33:19 +03:00
candidate [][]rune
candidateSource []rune
candidateOff int
candidateChoise int
candidateColNum int
2024-08-03 22:33:57 +03:00
// State for pagination
maxLine int // Maximum allowed columns on a single terminal
pageIdx int // Current page
2015-09-25 17:56:00 +03:00
}
2016-03-13 13:32:48 +03:00
func newOpCompleter(w io.Writer, op *Operation, width int) *opCompleter {
2015-09-25 17:56:00 +03:00
return &opCompleter{
2024-08-03 22:33:57 +03:00
w: w,
op: op,
width: width,
maxLine: 5,
2015-09-25 17:56:00 +03:00
}
}
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) {
2024-08-03 22:33:57 +03:00
// Number of elements in a full screen
numCandidatePerPage := o.candidateColNum * o.maxLine
// Number of elements in the current screen, which could be non-full
matrixSize := o.numCandidatesCurPage()
pageStart := o.pageIdx * numCandidatePerPage
candidateChoiceModPage := o.candidateChoise - pageStart
candidateChoiceModPage += i
candidateChoiceModPage %= matrixSize
if candidateChoiceModPage < 0 {
candidateChoiceModPage += matrixSize
}
o.candidateChoise = candidateChoiceModPage + pageStart
}
func (o *opCompleter) nextLine(i int) {
// Number of elements in a full screen
candidatesCurPage := o.numCandidatesCurPage()
numCandidatesFullPage := o.maxLine * o.candidateColNum
pageStart := o.pageIdx * numCandidatesFullPage
// Number of elements in the current screen, which could be non-full
numLines := o.getLinesCurPage()
rectangleSize := numLines * o.candidateColNum
candidateChoiceModPage := o.candidateChoise - pageStart
candidateChoiceModPage += i * o.candidateColNum
if candidateChoiceModPage >= candidatesCurPage {
if candidateChoiceModPage < rectangleSize {
candidateChoiceModPage += o.candidateColNum
}
candidateChoiceModPage -= rectangleSize
} else if candidateChoiceModPage < 0 {
candidateChoiceModPage += rectangleSize
if candidateChoiceModPage > candidatesCurPage {
candidateChoiceModPage -= o.candidateColNum
}
2015-09-25 17:56:00 +03:00
}
2024-08-03 22:33:57 +03:00
o.candidateChoise = candidateChoiceModPage + pageStart
2015-09-25 17:56:00 +03:00
}
func (o *opCompleter) OnComplete() bool {
if o.width == 0 {
return false
}
2015-09-25 17:56:00 +03:00
if o.IsInCompleteSelectMode() {
o.doSelect()
return true
2015-09-25 17:56:00 +03:00
}
buf := o.op.buf
rs := buf.Runes()
if o.IsInCompleteMode() && o.candidateSource != nil && runes.Equal(rs, o.candidateSource) {
2015-09-25 17:56:00 +03:00
o.EnterCompleteSelectMode()
o.doSelect()
return true
2015-09-25 17:56:00 +03:00
}
o.ExitCompleteSelectMode()
2015-09-27 05:33:19 +03:00
o.candidateSource = rs
2016-04-01 20:23:53 +03:00
newLines, offset := o.op.cfg.AutoComplete.Do(rs, buf.idx)
2015-09-25 17:56:00 +03:00
if len(newLines) == 0 {
o.ExitCompleteMode(false)
return true
2015-09-25 17:56:00 +03:00
}
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 true
2015-09-25 17:56:00 +03:00
}
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 true
2015-09-25 17:56:00 +03:00
}
}
o.EnterCompleteMode(offset, newLines)
return true
2015-09-25 17:56:00 +03:00
}
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:
2024-08-03 22:33:57 +03:00
o.nextLine(1)
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:
2024-08-03 22:33:57 +03:00
o.nextLine(-1)
case CharK:
o.updatePage(1)
case CharJ:
o.updatePage(-1)
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
}
2024-08-03 22:33:57 +03:00
// Number of candidate completions we can show on the current page,
// which might be different from number of candidates on a full page if
// we are on the last page.
func (o *opCompleter) numCandidatesCurPage() int {
numCandidatePerPage := o.candidateColNum * o.maxLine
pageStart := o.pageIdx * numCandidatePerPage
if len(o.candidate)-pageStart >= numCandidatePerPage {
return numCandidatePerPage
}
return len(o.candidate) - pageStart
}
// Number of lines on the current page
func (o *opCompleter) getLinesCurPage() int {
curPageSize := o.numCandidatesCurPage()
numLines := curPageSize / o.candidateColNum
if curPageSize%o.candidateColNum != 0 {
numLines += 1
2015-09-25 17:56:00 +03:00
}
2024-08-03 22:33:57 +03:00
return numLines
2015-09-25 17:56:00 +03:00
}
2016-03-13 13:32:48 +03:00
func (o *opCompleter) OnWidthChange(newWidth int) {
o.width = newWidth
}
2024-08-03 22:33:57 +03:00
// Move page
func (o *opCompleter) updatePage(offset int) {
if !o.inCompleteMode {
return
}
nextPageIdx := o.pageIdx + offset
if nextPageIdx < 0 {
return
}
nextPageStart := nextPageIdx * o.candidateColNum * o.maxLine
if nextPageStart > len(o.candidate) {
return
}
o.pageIdx = nextPageIdx
o.candidateChoise = nextPageStart
}
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
}
}
colWidth += o.candidateOff + 1
same := o.op.buf.RuneSlice(-o.candidateOff)
// -1 to avoid reach the end of line
width := o.width - 1
colNum := width / colWidth
if colNum != 0 {
colWidth += (width - (colWidth * colNum)) / colNum
}
2015-09-27 05:33:19 +03:00
o.candidateColNum = colNum
buf := bufio.NewWriter(o.w)
2015-09-25 17:56:00 +03:00
buf.Write(bytes.Repeat([]byte("\n"), lineCnt))
2015-09-25 17:56:00 +03:00
colIdx := 0
lines := 1
buf.WriteString("\033[J")
2024-08-03 22:33:57 +03:00
// Compute the candidates to show on the current page
numCandidatePerPage := o.candidateColNum * o.maxLine
startIdx := o.pageIdx * numCandidatePerPage
endIdx := (o.pageIdx + 1) * numCandidatePerPage
if endIdx > len(o.candidate) {
endIdx = len(o.candidate)
}
for idx := startIdx; idx < endIdx; idx += 1 {
c := o.candidate[idx]
2015-09-27 05:33:19 +03:00
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-runes.WidthAll(c)-runes.WidthAll(same)))
2015-09-25 17:56:00 +03:00
if inSelect {
buf.WriteString("\033[0m")
}
colIdx++
if colIdx == colNum {
buf.WriteString("\n")
lines++
colIdx = 0
}
}
2024-08-03 22:33:57 +03:00
// Add an extra line for navigation instructions
if colIdx != 0 {
buf.WriteString("\n")
lines++
}
navigationMsg := "(j: prev page) (k: next page)"
buf.WriteString(navigationMsg)
buf.Write(bytes.Repeat([]byte(" "), width-len(navigationMsg)))
2015-09-25 17:56:00 +03:00
// move back
fmt.Fprintf(buf, "\033[%dA\r", lineCnt-1+lines)
fmt.Fprintf(buf, "\033[%dC", o.op.buf.idx+o.op.buf.PromptLen())
buf.Flush()
2015-09-25 17:56:00 +03:00
}
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
2024-08-03 22:33:57 +03:00
// Initialize for complete mode
colWidth := 0
for _, c := range candidate {
w := runes.WidthAll(c)
if w > colWidth {
colWidth = w
}
}
colWidth += offset + 1
width := o.width - 1
colNum := width / colWidth
if colNum != 0 {
colWidth += (width - (colWidth * colNum)) / colNum
}
o.candidateColNum = colNum
o.pageIdx = 0
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()
}