Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Upgrade posener/complete to v2 #13

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 13 additions & 25 deletions completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import (
"os"
"runtime/debug"

"github.com/pkg/errors"
"github.com/posener/complete"
"github.com/rs/zerolog"
"github.com/symfony-cli/terminal"
"github.com/posener/complete/v2"
)

func init() {
Expand Down Expand Up @@ -50,50 +47,41 @@ func registerAutocompleteCommands(a *Application) {
}

func AutocompleteAppAction(c *Context) error {
// connect posener/complete logger to our logging facilities
logger := terminal.Logger.WithLevel(zerolog.DebugLevel)
complete.Log = func(format string, args ...interface{}) {
logger.Msgf("completion | "+format, args...)
}

cmd := complete.Command{
GlobalFlags: make(complete.Flags),
Sub: make(complete.Commands),
Flags: map[string]complete.Predictor{},
Sub: map[string]*complete.Command{},
}

// transpose registered commands and flags to posener/complete equivalence
for _, command := range c.App.VisibleCommands() {
subCmd := command.convertToPosenerCompleteCommand(c)

for _, name := range command.Names() {
cmd.Sub[name] = subCmd
cmd.Sub[name] = &subCmd
}
}

for _, f := range c.App.VisibleFlags() {
if vf, ok := f.(*verbosityFlag); ok {
vf.addToPosenerFlags(c, cmd.GlobalFlags)
vf.addToPosenerFlags(c, cmd.Flags)
continue
}

predictor := ContextPredictor{f, c}

for _, name := range f.Names() {
name = fmt.Sprintf("%s%s", prefixFor(name), name)
cmd.GlobalFlags[name] = predictor
cmd.Flags[name] = predictor
}
}

if !complete.New(c.App.HelpName, cmd).Complete() {
return errors.New("Could not run auto-completion")
}

cmd.Complete(c.App.HelpName)
return nil
}

func (c *Command) convertToPosenerCompleteCommand(ctx *Context) complete.Command {
command := complete.Command{
Flags: make(complete.Flags, 0),
Flags: map[string]complete.Predictor{},
}

for _, f := range c.VisibleFlags() {
Expand All @@ -110,16 +98,16 @@ func (c *Command) convertToPosenerCompleteCommand(ctx *Context) complete.Command
return command
}

func (c *Command) PredictArgs(ctx *Context, a complete.Args) []string {
func (c *Command) PredictArgs(ctx *Context, prefix string) []string {
if c.ShellComplete != nil {
return c.ShellComplete(ctx, a)
return c.ShellComplete(ctx, prefix)
}

return nil
}

type Predictor interface {
PredictArgs(*Context, complete.Args) []string
PredictArgs(*Context, string) []string
}

// ContextPredictor determines what terms can follow a command or a flag
Expand All @@ -131,6 +119,6 @@ type ContextPredictor struct {
}

// Predict invokes the predict function and implements the Predictor interface
func (p ContextPredictor) Predict(a complete.Args) []string {
return p.predictor.PredictArgs(p.ctx, a)
func (p ContextPredictor) Predict(prefix string) []string {
return p.predictor.PredictArgs(p.ctx, prefix)
}
3 changes: 1 addition & 2 deletions completion_installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"text/template"

"github.com/pkg/errors"
"github.com/posener/complete"
"github.com/symfony-cli/terminal"
)

Expand All @@ -28,7 +27,7 @@ var shellAutoCompleteInstallCommand = &Command{
{Name: "completion"},
},
Usage: "Dumps the completion script for the current shell",
ShellComplete: func(context *Context, c complete.Args) []string {
ShellComplete: func(*Context, string) []string {
return []string{"bash", "zsh", "fish"}
},
Description: `The <info>{{.HelpName}}</> command dumps the shell completion script required
Expand Down
3 changes: 1 addition & 2 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"time"

"github.com/pkg/errors"
"github.com/posener/complete"
"github.com/symfony-cli/terminal"
)

Expand Down Expand Up @@ -94,7 +93,7 @@ func (f FlagsByName) Swap(i, j int) {
type Flag interface {
fmt.Stringer

PredictArgs(*Context, complete.Args) []string
PredictArgs(*Context, string) []string
Validate(*Context) error
// Apply Flag settings to the given flag set
Apply(*flag.FlagSet)
Expand Down
Loading
Loading