package powershell
import (
"encoding/json"
"fmt"
"strings"
"github.com/rsteube/carapace/internal/common"
"github.com/rsteube/carapace/pkg/style"
"github.com/rsteube/carapace/third_party/github.com/elves/elvish/pkg/ui"
)
var sanitizer = strings .NewReplacer (
"\n" , `` ,
"\t" , `` ,
)
type completionResult struct {
CompletionText string
ListItemText string
ToolTip string
}
func ensureNotEmpty(s string ) string {
if s == "" {
return " "
}
return s
}
func ActionRawValues (currentWord string , meta common .Meta , values common .RawValues ) string {
valueStyle := "default"
if s := style .Carapace .Value ; s != "" && ui .ParseStyling (s ) != nil {
valueStyle = s
}
descriptionStyle := "default"
if s := style .Carapace .Description ; s != "" && ui .ParseStyling (s ) != nil {
descriptionStyle = s
}
vals := make ([]completionResult , 0 , len (values ))
for _ , val := range values {
if val .Value != "" {
val .Value = sanitizer .Replace (val .Value )
nospace := meta .Nospace .Matches (val .Value )
if strings .ContainsAny (val .Value , ` {}()[]*$?\"|<>&(),;#` +"`" ) {
val .Value = fmt .Sprintf ("'%v'" , val .Value )
}
if !nospace {
val .Value = val .Value + " "
}
if val .Style == "" || ui .ParseStyling (val .Style ) == nil {
val .Style = valueStyle
}
listItemText := fmt .Sprintf ("`e[21;22;23;24;25;29m`e[%vm%v`e[21;22;23;24;25;29;39;49m" , sgr (val .Style ), sanitizer .Replace (val .Display ))
if val .Description != "" {
listItemText = listItemText + fmt .Sprintf ("`e[%vm `e[%vm(%v)`e[21;22;23;24;25;29;39;49m" , sgr (descriptionStyle +" bg-default" ), sgr (descriptionStyle ), sanitizer .Replace (val .TrimmedDescription ()))
}
listItemText = listItemText + "`e[0m"
vals = append (vals , completionResult {
CompletionText : val .Value ,
ListItemText : ensureNotEmpty (listItemText ),
ToolTip : ensureNotEmpty (" " ),
})
}
}
m , _ := json .Marshal (vals )
return string (m )
}
func sgr(s string ) string {
if result := style .SGR (s ); result != "" {
return result
}
return "39;49"
}
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 .