From 088eccc669853ab8094256bcfab41f3cf1b1464c Mon Sep 17 00:00:00 2001 From: rsteube Date: Sat, 9 Nov 2024 00:43:08 +0100 Subject: [PATCH] powershell: tooltip support --- example/cmd/_test/powershell.ps1 | 4 ++-- internal/env/env.go | 5 +++++ internal/shell/powershell/action.go | 9 ++++++++- internal/shell/powershell/snippet.go | 4 ++-- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/example/cmd/_test/powershell.ps1 b/example/cmd/_test/powershell.ps1 index f72b5c77..e93671c5 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.replace('`e[', "`e["), [CompletionResultType]::ParameterValue, $_.ToolTip) } + example _carapace powershell $($elems| ForEach-Object {$_}) '' | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText.replace('`e[', "`e["), [CompletionResultType]::ParameterValue, $_.ToolTip.replace('`e[', "`e[")) } } else { - example _carapace powershell $($elems| ForEach-Object {$_}) | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText.replace('`e[', "`e["), [CompletionResultType]::ParameterValue, $_.ToolTip) } + example _carapace powershell $($elems| ForEach-Object {$_}) | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText.replace('`e[', "`e["), [CompletionResultType]::ParameterValue, $_.ToolTip.replace('`e[', "`e[")) } } ) diff --git a/internal/env/env.go b/internal/env/env.go index 436b0729..87584211 100644 --- a/internal/env/env.go +++ b/internal/env/env.go @@ -18,6 +18,7 @@ const ( CARAPACE_MATCH = "CARAPACE_MATCH" // match case insensitive CARAPACE_NOSPACE = "CARAPACE_NOSPACE" // nospace suffixes CARAPACE_SANDBOX = "CARAPACE_SANDBOX" // mock context for sandbox tests + CARAPACE_TOOLTIP = "CARAPACE_TOOLTIP" // enable tooltip style CARAPACE_ZSH_HASH_DIRS = "CARAPACE_ZSH_HASH_DIRS" // zsh hash directories CLICOLOR = "CLICOLOR" // disable color NO_COLOR = "NO_COLOR" // disable color @@ -70,3 +71,7 @@ func Match() string { // see match.Match func Nospace() string { return os.Getenv(CARAPACE_NOSPACE) } + +func Tooltip() bool { + return os.Getenv(CARAPACE_TOOLTIP) != "" // TODO force true|1 for all boolean environment variables +} diff --git a/internal/shell/powershell/action.go b/internal/shell/powershell/action.go index 9e21d551..3816adbd 100644 --- a/internal/shell/powershell/action.go +++ b/internal/shell/powershell/action.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/carapace-sh/carapace/internal/common" + "github.com/carapace-sh/carapace/internal/env" "github.com/carapace-sh/carapace/pkg/style" "github.com/carapace-sh/carapace/third_party/github.com/elves/elvish/pkg/ui" ) @@ -59,6 +60,12 @@ func ActionRawValues(currentWord string, meta common.Meta, values common.RawValu val.Style = valueStyle } + tooltip := " " + if env.Tooltip() { // TODO only check once + tooltip = val.Description + val.Description = "" + } + 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())) @@ -68,7 +75,7 @@ func ActionRawValues(currentWord string, meta common.Meta, values common.RawValu vals = append(vals, completionResult{ CompletionText: val.Value, ListItemText: ensureNotEmpty(listItemText), - ToolTip: ensureNotEmpty(" "), + ToolTip: ensureNotEmpty(tooltip), }) } } diff --git a/internal/shell/powershell/snippet.go b/internal/shell/powershell/snippet.go index 5c62cac4..0eec0529 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.replace('` + "`" + `e[', "` + "`" + `e["), [CompletionResultType]::ParameterValue, $_.ToolTip) } + %v _carapace powershell $($elems| ForEach-Object {$_}) '' | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText.replace('` + "`" + `e[', "` + "`" + `e["), [CompletionResultType]::ParameterValue, $_.ToolTip.replace('` + "`" + `e[', "` + "`" + `e[")) } } else { - %v _carapace powershell $($elems| ForEach-Object {$_}) | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText.replace('` + "`" + `e[', "` + "`" + `e["), [CompletionResultType]::ParameterValue, $_.ToolTip) } + %v _carapace powershell $($elems| ForEach-Object {$_}) | ConvertFrom-Json | ForEach-Object { [CompletionResult]::new($_.CompletionText, $_.ListItemText.replace('` + "`" + `e[', "` + "`" + `e["), [CompletionResultType]::ParameterValue, $_.ToolTip.replace('` + "`" + `e[', "` + "`" + `e[")) } } )