Skip to content

Commit 1252671

Browse files
authored
Merge pull request #768 from rsteube/no_color
support NO_COLOR env
2 parents 103abbf + bca121f commit 1252671

File tree

8 files changed

+52
-15
lines changed

8 files changed

+52
-15
lines changed

context.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"github.com/rsteube/carapace/internal/common"
11+
"github.com/rsteube/carapace/internal/env"
1112
"github.com/rsteube/carapace/internal/shell/zsh"
1213
"github.com/rsteube/carapace/third_party/github.com/drone/envsubst"
1314
"github.com/rsteube/carapace/third_party/golang.org/x/sys/execabs"
@@ -46,9 +47,9 @@ func NewContext(args ...string) Context {
4647
}
4748

4849
isGoRun := func() bool { return strings.HasPrefix(os.Args[0], os.TempDir()+"/go-build") }
49-
if value, exists := os.LookupEnv("CARAPACE_SANDBOX"); exists && isGoRun() {
50+
if sandbox := env.Sandbox(); sandbox != "" && isGoRun() {
5051
var m common.Mock
51-
_ = json.Unmarshal([]byte(value), &m)
52+
_ = json.Unmarshal([]byte(sandbox), &m)
5253
context.Dir = m.Dir
5354
context.mockedReplies = m.Replies
5455
}

internal/common/value.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ func (r RawValues) Filter(values ...string) RawValues {
8282
return filtered
8383
}
8484

85+
// Decolor clears style for all values.
86+
func (r RawValues) Decolor() RawValues {
87+
rawValues := make(RawValues, len(r))
88+
for index, value := range r {
89+
value.Style = ""
90+
rawValues[index] = value
91+
}
92+
return rawValues
93+
}
94+
8595
// FilterPrefix filters values with given prefix.
8696
func (r RawValues) FilterPrefix(prefix string) RawValues {
8797
filtered := make(RawValues, 0)

internal/config/env.go

Lines changed: 0 additions & 8 deletions
This file was deleted.

internal/env/env.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package env
2+
3+
import "os"
4+
5+
func ColorDisabled() bool {
6+
return os.Getenv("NO_COLOR") != "" || os.Getenv("CLICOLOR") == "0"
7+
}
8+
9+
func Lenient() bool {
10+
return os.Getenv("CARAPACE_LENIENT") != ""
11+
}
12+
13+
func Hashdirs() string {
14+
return os.Getenv("CARAPACE_ZSH_HASH_DIRS")
15+
}
16+
17+
func Sandbox() string {
18+
return os.Getenv("CARAPACE_SANDBOX")
19+
}
20+
21+
func Log() bool {
22+
return os.Getenv("CARAPACE_LOG") != ""
23+
}

internal/shell/shell.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/rsteube/carapace/internal/common"
9+
"github.com/rsteube/carapace/internal/env"
910
"github.com/rsteube/carapace/internal/shell/bash"
1011
"github.com/rsteube/carapace/internal/shell/bash_ble"
1112
"github.com/rsteube/carapace/internal/shell/elvish"
@@ -20,6 +21,7 @@ import (
2021
"github.com/rsteube/carapace/internal/shell/xonsh"
2122
"github.com/rsteube/carapace/internal/shell/zsh"
2223
"github.com/rsteube/carapace/pkg/ps"
24+
"github.com/rsteube/carapace/pkg/style"
2325
"github.com/spf13/cobra"
2426
)
2527

@@ -71,6 +73,13 @@ func Value(shell string, value string, meta common.Meta, values common.RawValues
7173
"zsh": zsh.ActionRawValues,
7274
}
7375
if f, ok := shellFuncs[shell]; ok {
76+
if env.ColorDisabled() {
77+
style.Carapace.Value = style.Default
78+
style.Carapace.Description = style.Default
79+
style.Carapace.Error = style.Underlined
80+
style.Carapace.Usage = style.Italic
81+
values = values.Decolor()
82+
}
7483
filtered := values.FilterPrefix(value)
7584
switch shell {
7685
case "elvish", "export", "zsh": // shells with support for showing messages

internal/shell/zsh/namedDirectory.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package zsh
22

33
import (
4-
"os"
54
"strings"
5+
6+
"github.com/rsteube/carapace/internal/env"
67
)
78

89
type namedDirectories map[string]string
@@ -34,7 +35,7 @@ func (nd *namedDirectories) Replace(s string) string {
3435
}
3536

3637
func init() {
37-
if hashDirs, ok := os.LookupEnv("CARAPACE_ZSH_HASH_DIRS"); ok {
38+
if hashDirs := env.Hashdirs(); hashDirs != "" {
3839
for _, line := range strings.Split(hashDirs, "\n") {
3940
if splitted := strings.SplitN(line, "=", 2); len(splitted) == 2 {
4041
NamedDirectories[splitted[0]] = splitted[1]

log.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import (
66
"log"
77
"os"
88

9+
"github.com/rsteube/carapace/internal/env"
910
"github.com/rsteube/carapace/internal/uid"
1011
"github.com/rsteube/carapace/pkg/ps"
1112
)
1213

1314
var LOG = log.New(ioutil.Discard, "", log.Flags())
1415

1516
func init() {
16-
if _, enabled := os.LookupEnv("CARAPACE_LOG"); !enabled {
17+
if !env.Log() {
1718
return
1819
}
1920

traverse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"strings"
55

66
"github.com/rsteube/carapace/internal/common"
7-
"github.com/rsteube/carapace/internal/config"
7+
"github.com/rsteube/carapace/internal/env"
88
"github.com/rsteube/carapace/internal/pflagfork"
99
"github.com/rsteube/carapace/pkg/style"
1010
"github.com/spf13/cobra"
@@ -39,7 +39,7 @@ func traverse(c *cobra.Command, args []string) (Action, Context) {
3939
LOG.Printf("traverse called for %#v with args %#v\n", c.Name(), args)
4040
storage.preRun(c, args)
4141

42-
if config.IsLenient() {
42+
if env.Lenient() {
4343
LOG.Printf("allowing unknown flags")
4444
c.FParseErrWhitelist.UnknownFlags = true
4545
}

0 commit comments

Comments
 (0)