-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from IdlePhysicist/completion
Adding tab completion functionality for Bash and Zsh
- Loading branch information
Showing
3 changed files
with
75 additions
and
0 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
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,52 @@ | ||
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"}, | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if err := c.stim.GetCompletion(args[0]); err != nil { | ||
fmt.Println(err) | ||
} | ||
}, | ||
} | ||
|
||
return cmd | ||
} |