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 9, 2024
1 parent f887f2a commit ff7fe1a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 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
24 changes: 19 additions & 5 deletions internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}
}
11 changes: 10 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 @@ -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
Expand All @@ -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()))
Expand All @@ -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),
})
}
}
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 ff7fe1a

Please sign in to comment.