package console
import (
"strings"
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace/pkg/style"
completer "github.com/carapace-sh/carapace/pkg/x"
"github.com/reeflective/readline"
"github.com/reeflective/console/internal/completion"
"github.com/reeflective/console/internal/line"
)
func (c *Console ) complete (input []rune , pos int ) readline .Completions {
menu := c .activeMenu ()
carapace .Gen (menu .Command )
args , prefixComp , prefixLine := completion .SplitArgs (input , pos )
args = append ([]string {c .name , "_carapace" }, args ...)
completions , err := completer .Complete (menu .Command , args ...)
raw := make ([]readline .Completion , len (completions .Values ))
for idx , val := range completions .Values {
raw [idx ] = readline .Completion {
Value : line .UnescapeValue (prefixComp , prefixLine , val .Value ),
Display : val .Display ,
Description : val .Description ,
Style : style .SGR (val .Style ),
Tag : val .Tag ,
}
if !completions .Nospace .Matches (val .Value ) {
raw [idx ].Value = val .Value + " "
}
switch val .Tag {
case "shorthand flags" , "longhand flags" :
raw [idx ].Tag = "flags"
}
}
comps := readline .CompleteRaw (raw )
comps = comps .Usage ("%s" , completions .Usage )
comps = c .justifyCommandComps (comps )
if err != nil {
comps = readline .CompleteMessage ("failed to load config: " + err .Error())
}
for _ , msg := range completions .Messages .Get () {
comps = comps .Merge (readline .CompleteMessage (msg ))
}
suffixes , err := completions .Nospace .MarshalJSON ()
if len (suffixes ) > 0 && err == nil {
comps = comps .NoSpace ([]rune (string (suffixes ))...)
}
comps = comps .Prefix (prefixComp )
comps .PREFIX = prefixLine
completer .ClearStorage ()
menu .resetPreRun ()
menu .hideFilteredCommands (menu .Command )
return comps
}
func (c *Console ) justifyCommandComps (comps readline .Completions ) readline .Completions {
justified := []string {}
comps .EachValue (func (comp readline .Completion ) readline .Completion {
if !strings .HasSuffix (comp .Tag , "commands" ) {
return comp
}
justified = append (justified , comp .Tag )
comp .Style = ""
return comp
})
if len (justified ) > 0 {
return comps .JustifyDescriptions (justified ...)
}
return comps
}
func (c *Console ) highlightSyntax (input []rune ) string {
args , unprocessed , err := line .Split (string (input ), true )
if err != nil {
args = append (args , unprocessed )
}
done := make ([]string , 0 )
remain := args
trimmed := line .TrimSpaces (remain )
cmd , _ , _ := c .activeMenu ().Find (trimmed )
if cmd != nil {
done , remain = line .HighlightCommand (done , args , c .activeMenu ().Command , c .cmdHighlight )
}
done , remain = line .HighlightCommandFlags (done , remain , c .flagHighlight )
done = append (done , remain ...)
highlighted := strings .Join (done , "" )
return highlighted
}
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 .