Skip to content

Commit

Permalink
powershell: tooltip support
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Nov 8, 2024
1 parent f887f2a commit 088eccc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
4 changes: 2 additions & 2 deletions example/cmd/_test/powershell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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[")) }
}
)

Expand Down
5 changes: 5 additions & 0 deletions internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
9 changes: 8 additions & 1 deletion internal/shell/powershell/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()))
Expand All @@ -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),
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/shell/powershell/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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[")) }
}
)
Expand Down

0 comments on commit 088eccc

Please sign in to comment.