Skip to content
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
68 changes: 68 additions & 0 deletions cmd/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cmd

import (
"fmt"
"os"
)

// GetZshCompletion returns the Zsh completion script as a string
func GetZshCompletion() string {
return `#compdef stew
_stew_installed_binaries() {
local -a binaries
binaries=($(stew list 2>/dev/null | tail -n +2 | awk '{print $1}'))
_describe 'installed binaries' binaries
}

_stew() {
local context state state_descr line
_arguments -C \
'1: :->command' \
'*: :->args'

case $state in
command)
local -a commands
commands=(
'install:Install a binary from GitHub or URL'
'i:Install a binary from GitHub or URL'
'search:Search GitHub repos'
's:Search GitHub repos'
'browse:Browse releases and assets'
'b:Browse releases and assets'
'upgrade:Upgrade an installed binary'
'up:Upgrade an installed binary'
'uninstall:Uninstall a binary'
'un:Uninstall a binary'
'rename:Rename an installed binary'
're:Rename an installed binary'
'list:List installed binaries'
'ls:List installed binaries'
'config:Configure stew'
'help:Show help'
'h:Show help'
)
_describe -t commands 'stew command' commands
;;
args)
case $line[1] in
upgrade|up|uninstall|un|rename|re)
_stew_installed_binaries
;;
esac
;;
esac
}
#_stew "$@"`
}

// RunCompletion prints the completion script to stdout
func RunCompletion(shell string) error {
switch shell {
case "zsh":
fmt.Fprintln(os.Stdout, GetZshCompletion())
default:
return fmt.Errorf("unsupported shell: %s (only zsh is supported currently)", shell)
}
return nil
}
13 changes: 12 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"log"
"os"

"github.com/marwanhawari/stew/cmd"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -103,6 +102,18 @@ func main() {
return nil
},
},
{
Name: "completion",
Usage: "Generate completion script for a specified shell. [Ex: stew completion zsh]",
Action: func(c *cli.Context) error {
shell := "zsh"
if len(os.Args) > 2 {
shell = os.Args[2]
}
cmd.RunCompletion(shell)
return nil
},
},
},
}

Expand Down