Skip to content

Commit bedfad9

Browse files
authored
Merge pull request #1192 from carapace-sh/powershell-nocolor
powershell: leave out escape codes for `CARAPACE_COLOR=0`
2 parents bb703cb + 1f2bc41 commit bedfad9

2 files changed

Lines changed: 53 additions & 24 deletions

File tree

internal/env/env.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
)
1212

1313
const (
14+
CARAPACE_COLOR = "CARAPACE_COLOR" // enable color
1415
CARAPACE_COMPLINE = "CARAPACE_COMPLINE" // TODO
1516
CARAPACE_COVERDIR = "CARAPACE_COVERDIR" // coverage directory for sandbox tests
1617
CARAPACE_EXPERIMENTAL = "CARAPACE_EXPERIMENTAL" // enable experimental features
@@ -29,6 +30,9 @@ const (
2930
)
3031

3132
func ColorDisabled() bool {
33+
if v, ok := os.LookupEnv(CARAPACE_COLOR); ok { // TODO multiple modes
34+
return v == "0"
35+
}
3236
return getBool(NO_COLOR) || os.Getenv(CLICOLOR) == "0"
3337
}
3438

internal/shell/powershell/action.go

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,44 +42,69 @@ func ActionRawValues(currentWord string, meta common.Meta, values common.RawValu
4242
descriptionStyle = s
4343
}
4444

45+
nocolor := env.ColorDisabled()
4546
tooltipEnabled := env.Tooltip()
4647

4748
vals := make([]completionResult, 0, len(values))
4849
for _, val := range values {
49-
if val.Value != "" { // must not be empty - any empty `''` parameter in CompletionResult causes an error
50-
val.Value = sanitizer.Replace(val.Value)
51-
nospace := meta.Nospace.Matches(val.Value)
50+
if val.Value == "" {
51+
continue // must not be empty - any empty `''` parameter in CompletionResult causes an error
52+
}
5253

53-
if strings.ContainsAny(val.Value, ` {}()[]*$?\"|<>&(),;#`+"`") {
54-
val.Value = fmt.Sprintf("'%v'", val.Value)
55-
}
54+
val.Value = sanitizer.Replace(val.Value)
55+
nospace := meta.Nospace.Matches(val.Value)
5656

57-
if !nospace {
58-
val.Value = val.Value + " "
59-
}
57+
if strings.ContainsAny(val.Value, ` {}()[]*$?\"|<>&(),;#`+"`") {
58+
val.Value = fmt.Sprintf("'%v'", val.Value)
59+
}
6060

61-
if val.Style == "" || ui.ParseStyling(val.Style) == nil {
62-
val.Style = valueStyle
63-
}
61+
if !nospace {
62+
val.Value = val.Value + " "
63+
}
6464

65-
tooltip := " "
66-
if tooltipEnabled && val.Description != "" {
67-
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()))
68-
val.Description = ""
65+
if val.Style == "" || ui.ParseStyling(val.Style) == nil {
66+
val.Style = valueStyle
67+
}
68+
69+
tooltip := " "
70+
if tooltipEnabled && val.Description != "" {
71+
switch nocolor {
72+
case true:
73+
tooltip = sanitizer.Replace(val.TrimmedDescription())
74+
default:
75+
tooltip = fmt.Sprintf("`e[%vm`e[%vm%v`e[21;22;23;24;25;29;39;49m",
76+
sgr(descriptionStyle+" bg-default"),
77+
sgr(descriptionStyle),
78+
sanitizer.Replace(val.TrimmedDescription()))
6979
}
80+
val.Description = ""
81+
}
7082

71-
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))
83+
var listItemText string
84+
switch nocolor {
85+
case true:
86+
listItemText = sanitizer.Replace(val.Display)
87+
if val.Description != "" {
88+
listItemText = fmt.Sprintf("%v (%v)", listItemText, sanitizer.Replace(val.TrimmedDescription()))
89+
}
90+
default:
91+
listItemText = fmt.Sprintf("`e[21;22;23;24;25;29m`e[%vm%v`e[21;22;23;24;25;29;39;49m",
92+
sgr(val.Style),
93+
sanitizer.Replace(val.Display))
7294
if val.Description != "" {
73-
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()))
95+
listItemText = listItemText + fmt.Sprintf("`e[%vm `e[%vm(%v)`e[21;22;23;24;25;29;39;49m",
96+
sgr(descriptionStyle+" bg-default"),
97+
sgr(descriptionStyle),
98+
sanitizer.Replace(val.TrimmedDescription()))
7499
}
75100
listItemText = listItemText + "`e[0m"
76-
77-
vals = append(vals, completionResult{
78-
CompletionText: val.Value,
79-
ListItemText: ensureNotEmpty(listItemText),
80-
ToolTip: ensureNotEmpty(tooltip),
81-
})
82101
}
102+
103+
vals = append(vals, completionResult{
104+
CompletionText: val.Value,
105+
ListItemText: ensureNotEmpty(listItemText),
106+
ToolTip: ensureNotEmpty(tooltip),
107+
})
83108
}
84109
m, _ := json.Marshal(vals)
85110
return string(m)

0 commit comments

Comments
 (0)