-
Notifications
You must be signed in to change notification settings - Fork 7
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
Adding tab completion functionality for Bash and Zsh #56
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,21 @@ | ||
package stim | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
func (stim *Stim) GetCompletion(shell string) error { | ||
switch shell { | ||
case `bash`: | ||
stim.rootCmd.GenBashCompletion(os.Stdout) | ||
case `zsh`: | ||
stim.rootCmd.GenZshCompletion(os.Stdout) | ||
io.WriteString(os.Stdout, "\ncompdef _stim stim\n") | ||
default: | ||
return fmt.Errorf("Unknown shell: %s", shell) | ||
} | ||
|
||
return nil | ||
} |
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,51 @@ | ||
package completion | ||
|
||
import ( | ||
"fmt" | ||
"github.com/PremiereGlobal/stim/stim" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
type Completion struct { | ||
name string | ||
stim *stim.Stim | ||
} | ||
|
||
func New() *Completion { | ||
return &Completion{name: "completion"} | ||
} | ||
|
||
func (c *Completion) Name() string { | ||
return c.name | ||
} | ||
|
||
func (c *Completion) BindStim(s *stim.Stim) { | ||
c.stim = s | ||
} | ||
|
||
func (c *Completion) Command(viper *viper.Viper) *cobra.Command { | ||
|
||
var cmd = &cobra.Command{ | ||
Use: "completion SHELL", | ||
Short: "Output shell completion for the given shell (bash or zsh)", | ||
Long: `Output shell completion for the given shell (bash or zsh) | ||
The following ought to suffice for loading the Bash completions: | ||
source <(stim completion bash) | ||
|
||
Zsh is more complicated because there is more than one completion engine for | ||
Zsh. Try putting the completion output into a script (in your $fpath) and | ||
loading it with compinit. | ||
stim completion zsh > /path/to/script | ||
|
||
`, | ||
ValidArgs: []string{"bash", "zsh"}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if err := c.stim.GetCompletion(args[0]); err != nil { | ||
fmt.Println(err) | ||
} | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to check that
len(args) > 0
before hitting this line.stim completion
causes a stack trace w/o useful messages. There might be a way to have cobra/viper do that for you.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did try to do the arg validation a more extravagant way, but I think this will suffice. It will fail gracefully & exit for
len(args) != 1
and in the switch statement instim/completion.go
I am logging out "unknown shells".