Skip to content

Commit

Permalink
move scripts back to files
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekpacia committed Nov 23, 2024
1 parent e27feb4 commit 9b41c34
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 106 deletions.
13 changes: 6 additions & 7 deletions autocomplete/bash_autocomplete
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#! /bin/bash
#!/usr/env/bin bash

: ${PROG:=$(basename ${BASH_SOURCE})}
# This is a shell completion script auto-generated by https://github.com/urfave/cli for bash.

# Macs have bash3 for which the bash-completion package doesn't include
# _init_completion. This is a minimal version of that function.
_cli_init_completion() {
__%[1]s_init_completion() {
COMPREPLY=()
_get_comp_words_by_ref "$@" cur prev words cword
}

_cli_bash_autocomplete() {
__%[1]s_bash_autocomplete() {
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
local cur opts base words
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
if declare -F _init_completion >/dev/null 2>&1; then
_init_completion -n "=:" || return
else
_cli_init_completion -n "=:" || return
__%[1]s_init_completion -n "=:" || return
fi
words=("${words[@]:0:$cword}")
if [[ "$cur" == "-"* ]]; then
Expand All @@ -31,5 +31,4 @@ _cli_bash_autocomplete() {
fi
}

complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete $PROG
unset PROG
complete -o bashdefault -o default -o nospace -F __%[1]s_bash_autocomplete %[1]s
2 changes: 1 addition & 1 deletion autocomplete/powershell_autocomplete.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ Register-ArgumentCompleter -Native -CommandName $name -ScriptBlock {
Invoke-Expression $other | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
}
29 changes: 29 additions & 0 deletions autocomplete/zsh_autocomplete
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#compdef %[1]s
compdef _%[1]s %[1]s

# This is a shell completion script auto-generated by https://github.com/urfave/cli for zsh.

_%[1]s() {
local -a opts # Declare a local array
local current
current=${words[-1]} # -1 means "the last element"
if [[ "$current" == "-"* ]]; then
# Current word starts with a hyphen, so complete flags/options
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${current} --generate-shell-completion)}")
else
# Current word does not start with a hyphen, so complete subcommands
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-shell-completion)}")
fi

if [[ "${opts[1]}" != "" ]]; then
_describe 'values' opts
else
_files
fi
}

# Don't run the completion function when being source-ed or eval-ed.
# See https://github.com/urfave/cli/issues/1874 for discussion.
if [ "$funcstack[1]" = "_%[1]s" ]; then
_%[1]s
fi
131 changes: 33 additions & 98 deletions completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"context"
"embed"
"fmt"
"sort"
)
Expand All @@ -13,22 +14,39 @@ const (
completionFlag = "--generate-shell-completion"
)

var shellCompletions = map[string]renderCompletionFunc{
"bash": func(cmd *Command, appName string) (string, error) {
return genBashCompletion(appName), nil
},
"zsh": func(cmd *Command, appName string) (string, error) {
return genZshCompletion(appName), nil
},
"fish": func(c *Command, appName string) (string, error) {
return c.ToFishCompletion()
},
"pwsh": func(cmd *Command, appName string) (string, error) {
return genPwshCompletion(), nil
},
}
type renderCompletion func(cmd *Command, appName string) (string, error)

type renderCompletionFunc func(cmd *Command, appName string) (string, error)
var (
//go:embed autocomplete
autoCompleteFS embed.FS

shellCompletions = map[string]renderCompletion{
"bash": func(c *Command, appName string) (string, error) {
b, err := autoCompleteFS.ReadFile("autocomplete/bash_autocomplete")
if err != nil {
return "", fmt.Errorf("read file: %w", err)
}

Check warning on line 28 in completion.go

View check run for this annotation

Codecov / codecov/patch

completion.go#L27-L28

Added lines #L27 - L28 were not covered by tests
return fmt.Sprintf(string(b), appName), nil
},
"zsh": func(c *Command, appName string) (string, error) {
b, err := autoCompleteFS.ReadFile("autocomplete/zsh_autocomplete")
if err != nil {
return "", fmt.Errorf("read file: %w", err)
}

Check warning on line 35 in completion.go

View check run for this annotation

Codecov / codecov/patch

completion.go#L34-L35

Added lines #L34 - L35 were not covered by tests
return fmt.Sprintf(string(b), appName), nil
},
"fish": func(c *Command, appName string) (string, error) {
return c.ToFishCompletion()
},
"pwsh": func(c *Command, appName string) (string, error) {
b, err := autoCompleteFS.ReadFile("autocomplete/powershell_autocomplete.ps1")
if err != nil {
return "", fmt.Errorf("read file: %w", err)
}

Check warning on line 45 in completion.go

View check run for this annotation

Codecov / codecov/patch

completion.go#L44-L45

Added lines #L44 - L45 were not covered by tests
return string(b), nil
},
}
)

func buildCompletionCommand(appName string) *Command {
return &Command{
Expand Down Expand Up @@ -70,86 +88,3 @@ func printShellCompletion(_ context.Context, cmd *Command, appName string) error

return nil
}

func genBashCompletion(appName string) string {
return fmt.Sprintf(`#!/usr/env/bin bash
# This is a shell completion script auto-generated by https://github.com/urfave/cli for bash.
# Macs have bash3 for which the bash-completion package doesn't include
# _init_completion. This is a minimal version of that function.
__%[1]s_init_completion() {
COMPREPLY=()
_get_comp_words_by_ref "$@" cur prev words cword
}
__%[1]s_bash_autocomplete() {
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
local cur opts base words
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
if declare -F _init_completion >/dev/null 2>&1; then
_init_completion -n "=:" || return
else
__%[1]s_init_completion -n "=:" || return
fi
words=("${words[@]:0:$cword}")
if [[ "$cur" == "-"* ]]; then
requestComp="${words[*]} ${cur} --generate-shell-completion"
else
requestComp="${words[*]} --generate-shell-completion"
fi
opts=$(eval "${requestComp}" 2>/dev/null)
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
return 0
fi
}
complete -o bashdefault -o default -o nospace -F __%[1]s_bash_autocomplete %[1]s
`, appName)
}

func genZshCompletion(appName string) string {
return fmt.Sprintf(`#compdef %[1]s
compdef _%[1]s %[1]s
# This is a shell completion script auto-generated by https://github.com/urfave/cli for zsh.
_%[1]s() {
local -a opts # Declare a local array
local current
current=${words[-1]} # -1 means "the last element"
if [[ "$current" == "-"* ]]; then
# Current word starts with a hyphen, so complete flags/options
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${current} --generate-shell-completion)}")
else
# Current word does not start with a hyphen, so complete subcommands
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-shell-completion)}")
fi
if [[ "${opts[1]}" != "" ]]; then
_describe 'values' opts
else
_files
fi
}
# Don't run the completion function when being source-ed or eval-ed.
# See https://github.com/urfave/cli/issues/1874 for discussion.
if [ "$funcstack[1]" = "_%[1]s" ]; then
_%[1]s
fi
`, appName)
}

func genPwshCompletion() string {
return `$fn = $($MyInvocation.MyCommand.Name)
$name = $fn -replace "(.*)\.ps1$", '$1'
Register-ArgumentCompleter -Native -CommandName $name -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
$other = "$wordToComplete --generate-shell-completion"
Invoke-Expression $other | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}`
}

0 comments on commit 9b41c34

Please sign in to comment.