Skip to content

Commit

Permalink
Merge pull request #608 from rsteube/move-internal-actions
Browse files Browse the repository at this point in the history
moved internal actions to internalActions.go
  • Loading branch information
rsteube authored Nov 23, 2022
2 parents 6ddeb5f + 7d22516 commit cdaadd2
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 cdaadd2

Please sign in to comment.