package tcsh
import (
"fmt"
"os"
"strings"
"github.com/carapace-sh/carapace/internal/common"
)
var sanitizer = strings .NewReplacer (
"\n" , `` ,
"\r" , `` ,
"\t" , `` ,
)
var quoter = 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 {
lastSegment := currentWord
for _ , r := range values {
if wordbreaks , ok := os .LookupEnv ("COMP_WORDBREAKS" ); ok {
wordbreaks = strings .Replace (wordbreaks , " " , "" , -1 )
if index := strings .LastIndexAny (currentWord , wordbreaks ); index != -1 {
r .Value = strings .TrimPrefix (r .Value , currentWord [:index +1 ])
lastSegment = currentWord [index +1 :]
}
}
}
if len (values ) > 1 && commonDisplayPrefix (values ...) != "" {
if valuePrefix := commonValuePrefix (values ...); lastSegment != valuePrefix {
values = common .RawValuesFrom (commonValuePrefix (values ...))
} else {
values [0 ].Display = " " + values [0 ].Display
}
}
vals := make ([]string , len (values ))
for index , val := range values {
if len (values ) == 1 {
vals [index ] = quoter .Replace (sanitizer .Replace (val .Value ))
} else {
if val .Description != "" {
vals [index ] = fmt .Sprintf ("%v_(%v)" , quoter .Replace (sanitizer .Replace (val .Value )), quoter .Replace (strings .Replace (sanitizer .Replace (val .TrimmedDescription ()), " " , "_" , -1 )))
} else {
vals [index ] = quoter .Replace (sanitizer .Replace (val .Value ))
}
}
}
return strings .Join (vals , "\n" )
}
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 .