Skip to content

Commit

Permalink
moved internal actions to internalActions.go
Browse files Browse the repository at this point in the history
Co-authored-by: maxlandon <maximelandon@gmail.com>
  • Loading branch information
rsteube and maxlandon committed Nov 23, 2022
1 parent 6ddeb5f commit 7d22516
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 133 deletions.
133 changes: 0 additions & 133 deletions defaultActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/rsteube/carapace/internal/common"
"github.com/rsteube/carapace/internal/config"
"github.com/rsteube/carapace/internal/pflagfork"
"github.com/rsteube/carapace/internal/shell/export"
"github.com/rsteube/carapace/pkg/style"
"github.com/rsteube/carapace/third_party/github.com/acarl005/stripansi"
Expand Down Expand Up @@ -126,62 +121,6 @@ func ActionFiles(suffix ...string) Action {
})
}

func actionPath(fileSuffixes []string, dirOnly bool) Action {
return ActionCallback(func(c Context) Action {
abs, err := c.Abs(c.CallbackValue)
if err != nil {
return ActionMessage(err.Error())
}

displayFolder := filepath.Dir(c.CallbackValue)
if displayFolder == "." {
displayFolder = ""
} else if !strings.HasSuffix(displayFolder, "/") {
displayFolder = displayFolder + "/"
}

actualFolder := filepath.Dir(abs)
files, err := ioutil.ReadDir(actualFolder)
if err != nil {
return ActionMessage(err.Error())
}

showHidden := !strings.HasSuffix(abs, "/") && strings.HasPrefix(filepath.Base(abs), ".")

vals := make([]string, 0, len(files)*2)
for _, file := range files {
if !showHidden && strings.HasPrefix(file.Name(), ".") {
continue
}

resolvedFile := file
if resolved, err := filepath.EvalSymlinks(actualFolder + file.Name()); err == nil {
if stat, err := os.Stat(resolved); err == nil {
resolvedFile = stat
}
}

if resolvedFile.IsDir() {
vals = append(vals, displayFolder+file.Name()+"/", style.ForPath(filepath.Clean(actualFolder+"/"+file.Name()+"/")))
} 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())))
break
}
}
}
}
if strings.HasPrefix(c.CallbackValue, "./") {
return ActionStyledValues(vals...).Invoke(Context{}).Prefix("./").ToA()
}
return ActionStyledValues(vals...)
})
}

// ActionValues completes arbitrary keywords (values)
func ActionValues(values ...string) Action {
return ActionCallback(func(c Context) Action {
Expand Down Expand Up @@ -238,12 +177,6 @@ func ActionStyledValuesDescribed(values ...string) Action {
})
}

func actionRawValues(rawValues ...common.RawValue) Action {
return Action{
rawValues: rawValues,
}
}

// ActionMessage displays a help messages in places where no completions can be generated
func ActionMessage(msg string, a ...interface{}) Action {
return ActionCallback(func(c Context) Action {
Expand Down Expand Up @@ -282,72 +215,6 @@ func ActionMultiParts(divider string, callback func(c Context) Action) Action {
})
}

func actionSubcommands(cmd *cobra.Command) Action {
vals := make([]string, 0)
for _, subcommand := range cmd.Commands() {
if !subcommand.Hidden && subcommand.Deprecated == "" {
vals = append(vals, subcommand.Name(), subcommand.Short)
for _, alias := range subcommand.Aliases {
vals = append(vals, alias, subcommand.Short)
}
}
}
return ActionValuesDescribed(vals...)
}

func actionFlags(cmd *cobra.Command) Action {
return ActionCallback(func(c Context) Action {
re := regexp.MustCompile("^-(?P<shorthand>[^-=]+)")
isShorthandSeries := re.MatchString(c.CallbackValue) && pflagfork.FlagSet(cmd.Flags()).IsPosix()

vals := make([]string, 0)
cmd.Flags().VisitAll(func(f *pflag.Flag) {
if f.Deprecated != "" {
return // skip deprecated flags
}

if f.Changed &&
!strings.Contains(f.Value.Type(), "Slice") &&
!strings.Contains(f.Value.Type(), "Array") &&
f.Value.Type() != "count" {
return // don't repeat flag
}

if isShorthandSeries {
if f.Shorthand != "" && f.ShorthandDeprecated == "" {
for _, shorthand := range c.CallbackValue[1:] {
if shorthandFlag := cmd.Flags().ShorthandLookup(string(shorthand)); shorthandFlag != nil && shorthandFlag.Value.Type() != "bool" && shorthandFlag.Value.Type() != "count" && shorthandFlag.NoOptDefVal == "" {
return // abort shorthand flag series if a previous one is not bool or count and requires an argument (no default value)
}
}
vals = append(vals, f.Shorthand, f.Usage)
}
} else {
if flagstyle := pflagfork.Flag(f).Style(); flagstyle != pflagfork.ShorthandOnly {
if flagstyle == pflagfork.NameAsShorthand {
vals = append(vals, "-"+f.Name, f.Usage)
} else {
vals = append(vals, "--"+f.Name, f.Usage)
}
}
if f.Shorthand != "" && f.ShorthandDeprecated == "" {
vals = append(vals, "-"+f.Shorthand, f.Usage)
}
}
})

if isShorthandSeries {
return ActionValuesDescribed(vals...).Invoke(c).Prefix(c.CallbackValue).ToA().noSpace("*")
}
for i := 0; i < len(vals); i = i + 2 { // TODO experimental - hardcoded multiparts completion if flags are "grouped" with `.`
if strings.Contains(vals[i], ".") {
return ActionValuesDescribed(vals...).Invoke(c).ToMultiPartsA(".")
}
}
return ActionValuesDescribed(vals...)
})
}

// ActionStyleConfig completes style configuration
//
// carapace.Value=blue
Expand Down
143 changes: 143 additions & 0 deletions internalActions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package carapace

import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/rsteube/carapace/internal/common"
"github.com/rsteube/carapace/internal/pflagfork"
"github.com/rsteube/carapace/pkg/style"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func actionPath(fileSuffixes []string, dirOnly bool) Action {
return ActionCallback(func(c Context) Action {
abs, err := c.Abs(c.CallbackValue)
if err != nil {
return ActionMessage(err.Error())
}

displayFolder := filepath.Dir(c.CallbackValue)
if displayFolder == "." {
displayFolder = ""
} else if !strings.HasSuffix(displayFolder, "/") {
displayFolder = displayFolder + "/"
}

actualFolder := filepath.Dir(abs)
files, err := ioutil.ReadDir(actualFolder)
if err != nil {
return ActionMessage(err.Error())
}

showHidden := !strings.HasSuffix(abs, "/") && strings.HasPrefix(filepath.Base(abs), ".")

vals := make([]string, 0, len(files)*2)
for _, file := range files {
if !showHidden && strings.HasPrefix(file.Name(), ".") {
continue
}

resolvedFile := file
if resolved, err := filepath.EvalSymlinks(actualFolder + file.Name()); err == nil {
if stat, err := os.Stat(resolved); err == nil {
resolvedFile = stat
}
}

if resolvedFile.IsDir() {
vals = append(vals, displayFolder+file.Name()+"/", style.ForPath(filepath.Clean(actualFolder+"/"+file.Name()+"/")))
} 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())))
break
}
}
}
}
if strings.HasPrefix(c.CallbackValue, "./") {
return ActionStyledValues(vals...).Invoke(Context{}).Prefix("./").ToA()
}
return ActionStyledValues(vals...)
})
}

func actionFlags(cmd *cobra.Command) Action {
return ActionCallback(func(c Context) Action {
re := regexp.MustCompile("^-(?P<shorthand>[^-=]+)")
isShorthandSeries := re.MatchString(c.CallbackValue) && pflagfork.FlagSet(cmd.Flags()).IsPosix()

vals := make([]string, 0)
cmd.Flags().VisitAll(func(f *pflag.Flag) {
if f.Deprecated != "" {
return // skip deprecated flags
}

if f.Changed &&
!strings.Contains(f.Value.Type(), "Slice") &&
!strings.Contains(f.Value.Type(), "Array") &&
f.Value.Type() != "count" {
return // don't repeat flag
}

if isShorthandSeries {
if f.Shorthand != "" && f.ShorthandDeprecated == "" {
for _, shorthand := range c.CallbackValue[1:] {
if shorthandFlag := cmd.Flags().ShorthandLookup(string(shorthand)); shorthandFlag != nil && shorthandFlag.Value.Type() != "bool" && shorthandFlag.Value.Type() != "count" && shorthandFlag.NoOptDefVal == "" {
return // abort shorthand flag series if a previous one is not bool or count and requires an argument (no default value)
}
}
vals = append(vals, f.Shorthand, f.Usage)
}
} else {
if flagstyle := pflagfork.Flag(f).Style(); flagstyle != pflagfork.ShorthandOnly {
if flagstyle == pflagfork.NameAsShorthand {
vals = append(vals, "-"+f.Name, f.Usage)
} else {
vals = append(vals, "--"+f.Name, f.Usage)
}
}
if f.Shorthand != "" && f.ShorthandDeprecated == "" {
vals = append(vals, "-"+f.Shorthand, f.Usage)
}
}
})

if isShorthandSeries {
return ActionValuesDescribed(vals...).Invoke(c).Prefix(c.CallbackValue).ToA().noSpace("*")
}
for i := 0; i < len(vals); i = i + 2 { // TODO experimental - hardcoded multiparts completion if flags are "grouped" with `.`
if strings.Contains(vals[i], ".") {
return ActionValuesDescribed(vals...).Invoke(c).ToMultiPartsA(".")
}
}
return ActionValuesDescribed(vals...)
})
}

func actionSubcommands(cmd *cobra.Command) Action {
vals := make([]string, 0)
for _, subcommand := range cmd.Commands() {
if !subcommand.Hidden && subcommand.Deprecated == "" {
vals = append(vals, subcommand.Name(), subcommand.Short)
for _, alias := range subcommand.Aliases {
vals = append(vals, alias, subcommand.Short)
}
}
}
return ActionValuesDescribed(vals...)
}

func actionRawValues(rawValues ...common.RawValue) Action {
return Action{
rawValues: rawValues,
}
}

0 comments on commit 7d22516

Please sign in to comment.