Skip to content

Commit

Permalink
Match: added TrimPrefix
Browse files Browse the repository at this point in the history
- fixed `Action.Prefix`
- fixed `actiondirectories`
  • Loading branch information
rsteube committed Aug 13, 2023
1 parent 14c6e31 commit 2f02c33
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
7 changes: 4 additions & 3 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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/match"
"github.com/rsteube/carapace/pkg/style"
pkgtraverse "github.com/rsteube/carapace/pkg/traverse"
)
Expand Down Expand Up @@ -229,9 +230,9 @@ func (a Action) NoSpace(suffixes ...rune) Action {
func (a Action) Prefix(prefix string) Action {
return ActionCallback(func(c Context) Action {
switch {
case strings.HasPrefix(c.Value, prefix):
c.Value = strings.TrimPrefix(c.Value, prefix)
case strings.HasPrefix(prefix, c.Value):
case match.HasPrefix(c.Value, prefix):
c.Value = match.TrimPrefix(c.Value, prefix)
case match.HasPrefix(prefix, c.Value):
c.Value = ""
default:
return ActionValues()
Expand Down
3 changes: 2 additions & 1 deletion defaultActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/rsteube/carapace/internal/config"
"github.com/rsteube/carapace/internal/export"
"github.com/rsteube/carapace/internal/man"
"github.com/rsteube/carapace/pkg/match"
"github.com/rsteube/carapace/pkg/style"
"github.com/rsteube/carapace/third_party/github.com/acarl005/stripansi"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -441,7 +442,7 @@ func actionDirectoryExecutables(dir string, prefix string, manDescriptions map[s
if files, err := os.ReadDir(dir); err == nil {
vals := make([]string, 0)
for _, f := range files {
if strings.HasPrefix(f.Name(), prefix) {
if match.HasPrefix(f.Name(), prefix) {
if info, err := f.Info(); err == nil && !f.IsDir() && isExecAny(info.Mode()) {
vals = append(vals, f.Name(), manDescriptions[f.Name()], style.ForPath(dir+"/"+f.Name(), c))
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/match/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,10 @@ func Equal(s, t string) bool {
func HasPrefix(s, prefix string) bool {
return match.HasPrefix(s, prefix)
}

func TrimPrefix(s, prefix string) string {
if HasPrefix(s, prefix) {
return s[len(prefix):]
}
return s
}

0 comments on commit 2f02c33

Please sign in to comment.