From ff7fe1ae73e5709180eff088caabf389347668a7 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 | 24 +++++++++++++++++++----- internal/shell/powershell/action.go | 11 ++++++++++- internal/shell/powershell/snippet.go | 4 ++-- 4 files changed, 33 insertions(+), 10 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..54edee6c 100644 --- a/internal/env/env.go +++ b/internal/env/env.go @@ -18,21 +18,22 @@ 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 ) func ColorDisabled() bool { - return os.Getenv(NO_COLOR) != "" || os.Getenv(CLICOLOR) == "0" + return getBool(NO_COLOR) || os.Getenv(CLICOLOR) == "0" } func Experimental() bool { - return os.Getenv(CARAPACE_EXPERIMENTAL) != "" + return getBool(CARAPACE_EXPERIMENTAL) } func Lenient() bool { - return os.Getenv(CARAPACE_LENIENT) != "" + return getBool(CARAPACE_LENIENT) } func Hashdirs() string { @@ -50,11 +51,11 @@ func Sandbox() (m *common.Mock, err error) { } func Log() bool { - return os.Getenv(CARAPACE_LOG) != "" + return getBool(CARAPACE_LOG) } func Hidden() bool { - return os.Getenv(CARAPACE_HIDDEN) != "" + return getBool(CARAPACE_HIDDEN) } func CoverDir() string { @@ -70,3 +71,16 @@ func Match() string { // see match.Match func Nospace() string { return os.Getenv(CARAPACE_NOSPACE) } + +func Tooltip() bool { + return getBool(CARAPACE_TOOLTIP) +} + +func getBool(s string) bool { + switch os.Getenv(s) { + case "true", "1": + return true + default: + return false + } +} diff --git a/internal/shell/powershell/action.go b/internal/shell/powershell/action.go index 9e21d551..148db6e7 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" ) @@ -41,6 +42,8 @@ func ActionRawValues(currentWord string, meta common.Meta, values common.RawValu descriptionStyle = s } + tooltipEnabled := env.Tooltip() + vals := make([]completionResult, 0, len(values)) for _, val := range values { if val.Value != "" { // must not be empty - any empty `''` parameter in CompletionResult causes an error @@ -59,6 +62,12 @@ func ActionRawValues(currentWord string, meta common.Meta, values common.RawValu val.Style = valueStyle } + tooltip := " " + if tooltipEnabled && val.Description != "" { + tooltip = 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())) + 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 +77,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[")) } } )