package bash
import (
"fmt"
"os"
"strings"
shlex "github.com/carapace-sh/carapace-shlex/v2"
"github.com/carapace-sh/carapace/internal/common"
"github.com/carapace-sh/carapace/internal/env"
)
var sanitizer = strings .NewReplacer (
"\n" , `` ,
"\r" , `` ,
"\t" , `` ,
)
var escapingQuotedReplacer = strings .NewReplacer (
`\` , `\\` ,
`"` , `\"` ,
`$` , `\$` ,
"`" , "\\`" ,
)
var escapingReplacer = strings .NewReplacer (
`\` , `\\` ,
`&` , `\&` ,
`<` , `\<` ,
`>` , `\>` ,
"`" , "\\`" ,
`'` , `\'` ,
`"` , `\"` ,
`{` , `\{` ,
`}` , `\}` ,
`$` , `\$` ,
`#` , `\#` ,
`|` , `\|` ,
`?` , `\?` ,
`(` , `\(` ,
`)` , `\)` ,
`;` , `\;` ,
` ` , `\ ` ,
`[` , `\[` ,
`]` , `\]` ,
`*` , `\*` ,
)
var displayReplacer = strings .NewReplacer (
`${` , `\\\${` ,
)
func commonPrefix(a , b string ) string {
i := 0
for i < len (a ) && i < len (b ) && a [i ] == b [i ] {
i ++
}
return a [0 :i ]
}
func commonDisplayPrefix(values ...common .RawValue ) (prefix string ) {
for index , val := range values {
if index == 0 {
prefix = val .Display
} else {
prefix = commonPrefix (prefix , val .Display )
}
}
return
}
func commonValuePrefix(values ...common .RawValue ) (prefix string ) {
for index , val := range values {
if index == 0 {
prefix = val .Value
} else {
prefix = commonPrefix (prefix , val .Value )
}
}
return
}
func ActionRawValues (currentWord string , meta common .Meta , values common .RawValues ) string {
ctx := shlex .Complete (env .Compline (), shlex .Bash )
singleQuoted := ctx .QuotingState == shlex .QUOTING_STATE
for index , value := range values {
values [index ].Value = strings .TrimPrefix (value .Value , wordbreakPrefix )
}
lastSegment := strings .TrimPrefix (currentWord , wordbreakPrefix )
extended := make (common .RawValues , 0 , len (values ))
for _ , val := range values {
if strings .HasPrefix (val .Value , lastSegment ) {
extended = append (extended , val )
}
}
if len (extended ) > 0 && len (extended ) < len (values ) {
values = extended
}
if len (values ) > 1 && commonDisplayPrefix (values ...) != "" {
if valuePrefix := commonValuePrefix (values ...); lastSegment != valuePrefix && strings .HasPrefix (valuePrefix , lastSegment ) {
values = common .RawValuesFrom (valuePrefix )
} else {
values [0 ].Display = " " + values [0 ].Display
}
meta .Nospace .Add ('*' )
}
nospace := false
vals := make ([]string , len (values ))
for index , val := range values {
if len (values ) == 1 || compType != COMP_TYPE_LIST_SUCCESSIVE_TABS {
nospace = nospace || meta .Nospace .Matches (val .Value )
vals [index ] = sanitizer .Replace (val .Value )
switch {
case singleQuoted :
vals [index ] = "'" + strings .ReplaceAll (vals [index ], "'" , `'\''` ) + "'"
case strings .HasPrefix (vals [index ], "~" ):
vals [index ] = escapingReplacer .Replace (vals [index ])
case requiresQuoting (vals [index ]):
vals [index ] = fmt .Sprintf (`"%v"` , escapingQuotedReplacer .Replace (vals [index ]))
}
} else {
nospace = true
val .Display = displayReplacer .Replace (val .Display )
val .Description = displayReplacer .Replace (val .Description )
if val .Description != "" {
vals [index ] = fmt .Sprintf ("%v (%v)" , val .Display , sanitizer .Replace (val .TrimmedDescription ()))
} else {
vals [index ] = val .Display
}
}
}
return fmt .Sprintf ("%v\001%v" , nospace , strings .Join (vals , "\n" ))
}
func requiresQuoting(s string ) bool {
chars := " \t\r\n`" + `[]{}()<>;|$&:*#`
chars += `'"`
chars += os .Getenv ("COMP_WORDBREAKS" )
chars += `\`
return strings .ContainsAny (s , chars )
}
The pages are generated with Golds v0.8.7 . (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 .