Repository: rsteube/carapace
Pull request #484 (2022-04-08 16:23:41)

powershell: added style support


Patch:
diff --git a/README.md b/README.md
index becef45..5851af8 100644
--- a/README.md
+++ b/README.md
@@ -66,6 +66,7 @@ PATH=$PATH:$(pwd)
 source <(example _carapace oil)
 
 # powershell
+Set-PSReadLineOption -Colors @{ "Selection" = "`e[7m" }
 Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
 $env:PATH += ":$pwd"
 example _carapace powershell | out-string | Invoke-Expression
diff --git a/docs/src/carapace/gen/hiddenSubcommand.md b/docs/src/carapace/gen/hiddenSubcommand.md
index dc3ac33..83adf4c 100644
--- a/docs/src/carapace/gen/hiddenSubcommand.md
+++ b/docs/src/carapace/gen/hiddenSubcommand.md
@@ -29,6 +29,7 @@ source out.nu
 source <(command _carapace)
 
 # powershell
+Set-PSReadLineOption -Colors @{ "Selection" = "`e[7m" }
 Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
 command _carapace | Out-String | Invoke-Expression
 
diff --git a/internal/shell/powershell/action.go b/internal/shell/powershell/action.go
index 5a7e1c2..7b2de76 100644
--- a/internal/shell/powershell/action.go
+++ b/internal/shell/powershell/action.go
@@ -7,6 +7,8 @@ import (
 	"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( // TODO
@@ -33,6 +35,16 @@ func ActionRawValues(currentWord string, nospace bool, values common.RawValues)
 	filtered := values.FilterPrefix(currentWord)
 	sort.Sort(common.ByDisplay(filtered))
 
+	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(filtered))
 	for _, val := range filtered {
 		if val.Value != "" { // must not be empty - any empty `''` parameter in CompletionResult causes an error
@@ -46,13 +58,30 @@ func ActionRawValues(currentWord string, nospace bool, values common.RawValues)
 				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%v`e[21;22;23;24;25;29;39;49m)", sgr(descriptionStyle), sanitizer.Replace(val.TrimmedDescription()))
+			}
+			listItemText = listItemText + "`e[0m"
+
 			vals = append(vals, completionResult{
 				CompletionText: val.Value,
-				ListItemText:   ensureNotEmpty(sanitizer.Replace(val.Display)),
-				ToolTip:        ensureNotEmpty(sanitizer.Replace(val.TrimmedDescription())),
+				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"
+}
diff --git a/internal/shell/powershell/snippet.go b/internal/shell/powershell/snippet.go
index e57fbf6..bb9974c 100644
--- a/internal/shell/powershell/snippet.go
+++ b/internal/shell/powershell/snippet.go
@@ -42,9 +42,9 @@ Function _%v_completer {
 
     $completions = @(
       if (!$wordToComplete) {
-        %v _carapace powershell $($elems| ForEach-Object {$_}) '""' | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText, [CompletionResultType]::ParameterValue, $_.ToolTip) }
+        %v _carapace powershell $($elems| ForEach-Object {$_}) '""' | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText.replace('`+"`"+`e[', "`+"`"+`e["), [CompletionResultType]::ParameterValue, $_.ToolTip) }
       } else {
-        %v _carapace powershell $($elems| ForEach-Object {$_}) | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText, [CompletionResultType]::ParameterValue, $_.ToolTip) }
+        %v _carapace powershell $($elems| ForEach-Object {$_}) | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText.replace('`+"`"+`e[', "`+"`"+`e["), [CompletionResultType]::ParameterValue, $_.ToolTip) }
       }
     )
 


Test patch:
diff --git a/example/cmd/_test/powershell.ps1 b/example/cmd/_test/powershell.ps1
index d606a2f..d9411e1 100644
--- a/example/cmd/_test/powershell.ps1
+++ b/example/cmd/_test/powershell.ps1
@@ -30,9 +30,9 @@ Function _example_completer {
 
     $completions = @(
       if (!$wordToComplete) {
-        example _carapace powershell $($elems| ForEach-Object {$_}) '""' | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText, [CompletionResultType]::ParameterValue, $_.ToolTip) }
+        example _carapace powershell $($elems| ForEach-Object {$_}) '""' | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText.replace('`e[', "`e["), [CompletionResultType]::ParameterValue, $_.ToolTip) }
       } else {
-        example _carapace powershell $($elems| ForEach-Object {$_}) | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText, [CompletionResultType]::ParameterValue, $_.ToolTip) }
+        example _carapace powershell $($elems| ForEach-Object {$_}) | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText.replace('`e[', "`e["), [CompletionResultType]::ParameterValue, $_.ToolTip) }
       }
     )