-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
107 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/rsteube/carapace" | ||
"github.com/rsteube/carapace-bin/cmd/carapace/cmd/completers" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var invokeCmd = &cobra.Command{ | ||
Use: "invoke", | ||
Short: "", | ||
Args: cobra.MinimumNArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if overlayPath, err := overlayPath(args[0]); err == nil && len(args) > 2 { // and arg[1] is a known shell | ||
cmd := &cobra.Command{ | ||
DisableFlagParsing: true, | ||
CompletionOptions: cobra.CompletionOptions{ | ||
DisableDefaultCmd: true, | ||
}, | ||
} | ||
|
||
// TODO yuck | ||
command := args[0] | ||
shell := args[1] | ||
args[0] = "_carapace" | ||
args[1] = "export" | ||
os.Args[1] = "_carapace" | ||
os.Args[2] = "export" | ||
os.Setenv("CARAPACE_LENIENT", "1") | ||
|
||
carapace.Gen(cmd).PositionalAnyCompletion( | ||
carapace.ActionCallback(func(c carapace.Context) carapace.Action { | ||
batch := carapace.Batch() | ||
specPath, err := completers.SpecPath(command) | ||
if err != nil { | ||
batch = append(batch, carapace.ActionImport([]byte(invokeCompleter(command)))) | ||
} else { | ||
out, err := specCompletion(specPath, args[1:]...) | ||
if err != nil { | ||
return carapace.ActionMessage(err.Error()) | ||
} | ||
|
||
batch = append(batch, carapace.ActionImport([]byte(out))) | ||
} | ||
|
||
batch = append(batch, overlayCompletion(overlayPath, args[1:]...)) | ||
return batch.ToA() | ||
}), | ||
) | ||
|
||
cmd.SetArgs(append([]string{"_carapace", shell}, args[2:]...)) | ||
cmd.Execute() | ||
} else { | ||
if specPath, err := completers.SpecPath(args[0]); err == nil { | ||
out, err := specCompletion(specPath, args[1:]...) | ||
if err != nil { | ||
fmt.Fprintln(cmd.ErrOrStderr(), err.Error()) | ||
return | ||
} | ||
|
||
// TODO revert the patching from specCompletion to use the integrated version for overlay to work (should move this somewhere else - best in specCompletion) | ||
// TODO only patch completion script | ||
out = strings.Replace(out, fmt.Sprintf("--spec '%v'", specPath), args[0], -1) | ||
out = strings.Replace(out, fmt.Sprintf("'--spec', '%v'", specPath), fmt.Sprintf("'%v'", args[0]), -1) // xonsh callback | ||
fmt.Fprint(cmd.OutOrStdout(), out) | ||
} else { | ||
fmt.Print(invokeCompleter(args[0])) | ||
} | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
carapace.Gen(invokeCmd).Standalone() | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters