Skip to content

Commit

Permalink
Merge pull request #657 from rsteube/style-context
Browse files Browse the repository at this point in the history
style: fix context awareness
  • Loading branch information
rsteube authored Jan 1, 2023
2 parents b14f0f5 + 357c43d commit 2596913
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 24 deletions.
21 changes: 13 additions & 8 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/rsteube/carapace/internal/cache"
"github.com/rsteube/carapace/internal/common"
pkgcache "github.com/rsteube/carapace/pkg/cache"
"github.com/rsteube/carapace/pkg/style"
)

// Action indicates how to complete a flag or positional argument.
Expand Down Expand Up @@ -104,20 +105,20 @@ func (a Action) UsageF(f func() string) Action {
//
// ActionValues("yes").Style(style.Green)
// ActionValues("no").Style(style.Red)
func (a Action) Style(style string) Action {
return a.StyleF(func(s string) string {
return style
func (a Action) Style(s string) Action {
return a.StyleF(func(_ string, _ style.Context) string {
return s
})
}

// Style sets the style using a reference
//
// ActionValues("value").StyleR(&style.Carapace.Value)
// ActionValues("description").StyleR(&style.Carapace.Value)
func (a Action) StyleR(style *string) Action {
func (a Action) StyleR(s *string) Action {
return ActionCallback(func(c Context) Action {
if style != nil {
return a.Style(*style)
if s != nil {
return a.Style(*s)
}
return a
})
Expand All @@ -127,11 +128,15 @@ func (a Action) StyleR(style *string) Action {
//
// ActionValues("dir/", "test.txt").StyleF(style.ForPathExt)
// ActionValues("true", "false").StyleF(style.ForKeyword)
func (a Action) StyleF(f func(s string) string) Action {
func (a Action) StyleF(f func(s string, sc style.Context) string) Action {
return ActionCallback(func(c Context) Action {
invoked := a.Invoke(c)
for index, v := range invoked.rawValues {
invoked.rawValues[index].Style = f(v.Value)
path, err := c.Abs(v.Value)
if err != nil {
return ActionMessage(err.Error())
}
invoked.rawValues[index].Style = f(path, c)
}
return invoked.ToA()
})
Expand Down
6 changes: 3 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func newContext(args []string) Context {
}

// LookupEnv retrieves the value of the environment variable named by the key.
func (c *Context) LookupEnv(key string) (string, bool) {
func (c Context) LookupEnv(key string) (string, bool) {
prefix := key + "="
for i := len(c.Env) - 1; i >= 0; i-- {
if env := c.Env[i]; strings.HasPrefix(env, prefix) {
Expand All @@ -50,7 +50,7 @@ func (c *Context) LookupEnv(key string) (string, bool) {
}

// Getenv retrieves the value of the environment variable named by the key.
func (c *Context) Getenv(key string) string {
func (c Context) Getenv(key string) string {
v, _ := c.LookupEnv(key)
return v
}
Expand All @@ -63,7 +63,7 @@ func (c *Context) Setenv(key, value string) {
c.Env = append(c.Env, fmt.Sprintf("%v=%v", key, value))
}

func (c *Context) Envsubst(s string) (string, error) {
func (c Context) Envsubst(s string) (string, error) {
return envsubst.Eval(s, c.Getenv)
}

Expand Down
8 changes: 4 additions & 4 deletions defaultActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ func ActionExecute(cmd *cobra.Command) Action {
// ActionDirectories completes directories.
func ActionDirectories() Action {
return ActionCallback(func(c Context) Action {
return actionPath([]string{""}, true).Invoke(c).ToMultiPartsA("/").StyleF(func(s string) string {
return actionPath([]string{""}, true).Invoke(c).ToMultiPartsA("/").StyleF(func(s string, sc style.Context) string {
if abs, err := c.Abs(s); err == nil {
return style.ForPath(abs)
return style.ForPath(abs, c)
}
return ""
})
Expand All @@ -112,9 +112,9 @@ func ActionDirectories() Action {
// ActionFiles completes files with optional suffix filtering.
func ActionFiles(suffix ...string) Action {
return ActionCallback(func(c Context) Action {
return actionPath(suffix, false).Invoke(c).ToMultiPartsA("/").StyleF(func(s string) string {
return actionPath(suffix, false).Invoke(c).ToMultiPartsA("/").StyleF(func(s string, sc style.Context) string {
if abs, err := c.Abs(s); err == nil {
return style.ForPath(abs)
return style.ForPath(abs, c)
}
return ""
})
Expand Down
4 changes: 2 additions & 2 deletions internalActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ func actionPath(fileSuffixes []string, dirOnly bool) Action {
}

if resolvedFile.IsDir() {
vals = append(vals, displayFolder+file.Name()+"/", style.ForPath(filepath.Clean(actualFolder+"/"+file.Name()+"/")))
vals = append(vals, displayFolder+file.Name()+"/", style.ForPath(filepath.Clean(actualFolder+"/"+file.Name()+"/"), c))
} else if !dirOnly {
if len(fileSuffixes) == 0 {
fileSuffixes = []string{""}
}
for _, suffix := range fileSuffixes {
if strings.HasSuffix(file.Name(), suffix) {
vals = append(vals, displayFolder+file.Name(), style.ForPath(filepath.Clean(actualFolder+"/"+file.Name())))
vals = append(vals, displayFolder+file.Name(), style.ForPath(filepath.Clean(actualFolder+"/"+file.Name()), c))
break
}
}
Expand Down
14 changes: 12 additions & 2 deletions pkg/style/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@ import (
"github.com/rsteube/carapace/third_party/github.com/elves/elvish/pkg/ui"
)

type Context interface {
Abs(s string) (string, error)
Getenv(key string) string
LookupEnv(key string) (string, bool)
}

// ForPath returns the style for given path
//
// /tmp/locally/reachable/file.txt
func ForPath(path string) string { return fromSGR(lscolors.GetColorist().GetStyle(path)) }
func ForPath(path string, sc Context) string {
return fromSGR(lscolors.GetColorist(sc.Getenv("LS_COLORS")).GetStyle(path))
}

// ForPath returns the style for given path by extension only
//
// /tmp/non/existing/file.txt
func ForPathExt(path string) string { return fromSGR(lscolors.GetColorist().GetStyleExt(path)) }
func ForPathExt(path string, sc Context) string {
return fromSGR(lscolors.GetColorist(sc.Getenv("LS_COLORS")).GetStyleExt(path))
}

func fromSGR(sgr string) string {
s := ui.StyleFromSGR(sgr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package lscolors

import (
"os"
"path"
"strings"
"sync"
Expand Down Expand Up @@ -37,20 +36,19 @@ func init() {
lastColorist = parseLsColor(defaultLsColorString)
}

func GetColorist() Colorist {
func GetColorist(lsColorString string) Colorist {
lastColoristMutex.Lock()
defer lastColoristMutex.Unlock()

s := getLsColors()
s := getLsColors(lsColorString)
if lastLsColors != s {
lastLsColors = s
lastColorist = parseLsColor(s)
}
return lastColorist
}

func getLsColors() string {
lsColorString := os.Getenv("LS_COLORS")
func getLsColors(lsColorString string) string {
if len(lsColorString) == 0 {
return defaultLsColorString
}
Expand Down

0 comments on commit 2596913

Please sign in to comment.