package readline
import (
"fmt"
"github.com/reeflective/readline/internal/color"
"github.com/reeflective/readline/internal/completion"
"github.com/reeflective/readline/internal/history"
"github.com/reeflective/readline/internal/keymap"
)
func (rl *Shell ) completionCommands () commands {
return map [string ]func (){
"complete" : rl .completeWord ,
"possible-completions" : rl .possibleCompletions ,
"insert-completions" : rl .insertCompletions ,
"menu-complete" : rl .menuComplete ,
"menu-complete-backward" : rl .menuCompleteBackward ,
"delete-char-or-list" : rl .deleteCharOrList ,
"menu-complete-next-tag" : rl .menuCompleteNextTag ,
"menu-complete-prev-tag" : rl .menuCompletePrevTag ,
"accept-and-menu-complete" : rl .acceptAndMenuComplete ,
"vi-registers-complete" : rl .viRegistersComplete ,
"menu-incremental-search" : rl .menuIncrementalSearch ,
}
}
func (rl *Shell ) completeWord () {
rl .History .SkipSave ()
if !rl .completer .IsActive () {
rl .startMenuComplete (rl .commandCompletion )
if rl .Config .GetBool ("menu-complete-display-prefix" ) {
return
}
}
rl .completer .Select (1 , 0 )
rl .completer .SkipDisplay ()
}
func (rl *Shell ) possibleCompletions () {
rl .History .SkipSave ()
rl .startMenuComplete (rl .commandCompletion )
}
func (rl *Shell ) insertCompletions () {
rl .History .Save ()
if !rl .completer .IsActive () {
rl .startMenuComplete (rl .commandCompletion )
}
for i := 0 ; i < rl .completer .Matches (); i ++ {
rl .completer .Select (1 , 0 )
rl .completer .Cancel (false , false )
}
rl .completer .Cancel (false , false )
rl .completer .ClearMenu (true )
}
func (rl *Shell ) menuComplete () {
rl .History .SkipSave ()
if !rl .completer .IsActive () {
rl .startMenuComplete (rl .commandCompletion )
if rl .Config .GetBool ("menu-complete-display-prefix" ) {
return
}
}
rl .completer .Select (1 , 0 )
}
func (rl *Shell ) deleteCharOrList () {
switch {
case rl .cursor .Pos () < rl .line .Len ():
rl .line .CutRune (rl .cursor .Pos ())
default :
rl .possibleCompletions ()
}
}
func (rl *Shell ) menuCompleteBackward () {
rl .History .SkipSave ()
if !rl .completer .IsActive () {
rl .startMenuComplete (rl .commandCompletion )
}
rl .completer .Select (-1 , 0 )
}
func (rl *Shell ) menuCompleteNextTag () {
rl .History .SkipSave ()
if !rl .completer .IsActive () {
return
}
rl .completer .SelectTag (true )
}
func (rl *Shell ) menuCompletePrevTag () {
rl .History .SkipSave ()
if !rl .completer .IsActive () {
return
}
rl .completer .SelectTag (false )
}
func (rl *Shell ) acceptAndMenuComplete () {
rl .History .SkipSave ()
if !rl .completer .IsActive () {
return
}
if !rl .completer .IsInserting () {
return
}
rl .completer .Cancel (false , false )
rl .completer .Select (1 , 0 )
}
func (rl *Shell ) viRegistersComplete () {
rl .History .SkipSave ()
rl .startMenuComplete (rl .Buffers .Complete )
}
func (rl *Shell ) menuIncrementalSearch () {
rl .History .SkipSave ()
rl .completer .GenerateWith (rl .commandCompletion )
rl .completer .IsearchStart ("completions" , false , false )
}
func (rl *Shell ) startMenuComplete (completer completion .Completer ) {
rl .History .SkipSave ()
rl .Keymap .SetLocal (keymap .MenuSelect )
rl .completer .GenerateWith (completer )
}
func (rl *Shell ) commandCompletion () completion .Values {
if rl .Completer == nil {
return completion .Values {}
}
line , cursor := rl .completer .Line ()
comps := rl .Completer (*line , cursor .Pos ())
return comps .convert ()
}
func (rl *Shell ) historyCompletion (forward , filterLine , substring bool ) {
switch {
case rl .Keymap .Local () == keymap .MenuSelect || rl .Keymap .Local () == keymap .Isearch || rl .completer .AutoCompleting ():
if rl .History .OnLastSource () {
rl .History .Cycle (true )
rl .completer .ResetForce ()
rl .Hint .Reset ()
return
}
rl .History .Cycle (true )
fallthrough
default :
if rl .History .Current () == nil {
rl .Hint .SetTemporary (fmt .Sprintf ("%s%s%s %s" , color .Dim , color .FgRed , "No command history source" , color .Reset ))
return
}
completer := func () completion .Values {
maxLines := rl .Display .AvailableHelperLines ()
return history .Complete (rl .History , forward , filterLine , maxLines , rl .completer .IsearchRegex )
}
if substring {
rl .completer .GenerateWith (completer )
rl .completer .IsearchStart (rl .History .Name (), true , true )
} else {
rl .startMenuComplete (completer )
rl .completer .AutocompleteForce ()
}
}
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .