Skip to content

Commit 9b41c34

Browse files
committed
move scripts back to files
1 parent e27feb4 commit 9b41c34

File tree

4 files changed

+69
-106
lines changed

4 files changed

+69
-106
lines changed

autocomplete/bash_autocomplete

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
#! /bin/bash
1+
#!/usr/env/bin bash
22

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

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

12-
_cli_bash_autocomplete() {
12+
__%[1]s_bash_autocomplete() {
1313
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
1414
local cur opts base words
1515
COMPREPLY=()
1616
cur="${COMP_WORDS[COMP_CWORD]}"
1717
if declare -F _init_completion >/dev/null 2>&1; then
1818
_init_completion -n "=:" || return
1919
else
20-
_cli_init_completion -n "=:" || return
20+
__%[1]s_init_completion -n "=:" || return
2121
fi
2222
words=("${words[@]:0:$cword}")
2323
if [[ "$cur" == "-"* ]]; then
@@ -31,5 +31,4 @@ _cli_bash_autocomplete() {
3131
fi
3232
}
3333

34-
complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete $PROG
35-
unset PROG
34+
complete -o bashdefault -o default -o nospace -F __%[1]s_bash_autocomplete %[1]s

autocomplete/powershell_autocomplete.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ Register-ArgumentCompleter -Native -CommandName $name -ScriptBlock {
66
Invoke-Expression $other | ForEach-Object {
77
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
88
}
9-
}
9+
}

autocomplete/zsh_autocomplete

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#compdef %[1]s
2+
compdef _%[1]s %[1]s
3+
4+
# This is a shell completion script auto-generated by https://github.com/urfave/cli for zsh.
5+
6+
_%[1]s() {
7+
local -a opts # Declare a local array
8+
local current
9+
current=${words[-1]} # -1 means "the last element"
10+
if [[ "$current" == "-"* ]]; then
11+
# Current word starts with a hyphen, so complete flags/options
12+
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${current} --generate-shell-completion)}")
13+
else
14+
# Current word does not start with a hyphen, so complete subcommands
15+
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-shell-completion)}")
16+
fi
17+
18+
if [[ "${opts[1]}" != "" ]]; then
19+
_describe 'values' opts
20+
else
21+
_files
22+
fi
23+
}
24+
25+
# Don't run the completion function when being source-ed or eval-ed.
26+
# See https://github.com/urfave/cli/issues/1874 for discussion.
27+
if [ "$funcstack[1]" = "_%[1]s" ]; then
28+
_%[1]s
29+
fi

completion.go

Lines changed: 33 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cli
22

33
import (
44
"context"
5+
"embed"
56
"fmt"
67
"sort"
78
)
@@ -13,22 +14,39 @@ const (
1314
completionFlag = "--generate-shell-completion"
1415
)
1516

16-
var shellCompletions = map[string]renderCompletionFunc{
17-
"bash": func(cmd *Command, appName string) (string, error) {
18-
return genBashCompletion(appName), nil
19-
},
20-
"zsh": func(cmd *Command, appName string) (string, error) {
21-
return genZshCompletion(appName), nil
22-
},
23-
"fish": func(c *Command, appName string) (string, error) {
24-
return c.ToFishCompletion()
25-
},
26-
"pwsh": func(cmd *Command, appName string) (string, error) {
27-
return genPwshCompletion(), nil
28-
},
29-
}
17+
type renderCompletion func(cmd *Command, appName string) (string, error)
3018

31-
type renderCompletionFunc func(cmd *Command, appName string) (string, error)
19+
var (
20+
//go:embed autocomplete
21+
autoCompleteFS embed.FS
22+
23+
shellCompletions = map[string]renderCompletion{
24+
"bash": func(c *Command, appName string) (string, error) {
25+
b, err := autoCompleteFS.ReadFile("autocomplete/bash_autocomplete")
26+
if err != nil {
27+
return "", fmt.Errorf("read file: %w", err)
28+
}
29+
return fmt.Sprintf(string(b), appName), nil
30+
},
31+
"zsh": func(c *Command, appName string) (string, error) {
32+
b, err := autoCompleteFS.ReadFile("autocomplete/zsh_autocomplete")
33+
if err != nil {
34+
return "", fmt.Errorf("read file: %w", err)
35+
}
36+
return fmt.Sprintf(string(b), appName), nil
37+
},
38+
"fish": func(c *Command, appName string) (string, error) {
39+
return c.ToFishCompletion()
40+
},
41+
"pwsh": func(c *Command, appName string) (string, error) {
42+
b, err := autoCompleteFS.ReadFile("autocomplete/powershell_autocomplete.ps1")
43+
if err != nil {
44+
return "", fmt.Errorf("read file: %w", err)
45+
}
46+
return string(b), nil
47+
},
48+
}
49+
)
3250

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

7189
return nil
7290
}
73-
74-
func genBashCompletion(appName string) string {
75-
return fmt.Sprintf(`#!/usr/env/bin bash
76-
77-
# This is a shell completion script auto-generated by https://github.com/urfave/cli for bash.
78-
79-
# Macs have bash3 for which the bash-completion package doesn't include
80-
# _init_completion. This is a minimal version of that function.
81-
__%[1]s_init_completion() {
82-
COMPREPLY=()
83-
_get_comp_words_by_ref "$@" cur prev words cword
84-
}
85-
86-
__%[1]s_bash_autocomplete() {
87-
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
88-
local cur opts base words
89-
COMPREPLY=()
90-
cur="${COMP_WORDS[COMP_CWORD]}"
91-
if declare -F _init_completion >/dev/null 2>&1; then
92-
_init_completion -n "=:" || return
93-
else
94-
__%[1]s_init_completion -n "=:" || return
95-
fi
96-
words=("${words[@]:0:$cword}")
97-
if [[ "$cur" == "-"* ]]; then
98-
requestComp="${words[*]} ${cur} --generate-shell-completion"
99-
else
100-
requestComp="${words[*]} --generate-shell-completion"
101-
fi
102-
opts=$(eval "${requestComp}" 2>/dev/null)
103-
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
104-
return 0
105-
fi
106-
}
107-
108-
complete -o bashdefault -o default -o nospace -F __%[1]s_bash_autocomplete %[1]s
109-
`, appName)
110-
}
111-
112-
func genZshCompletion(appName string) string {
113-
return fmt.Sprintf(`#compdef %[1]s
114-
compdef _%[1]s %[1]s
115-
116-
# This is a shell completion script auto-generated by https://github.com/urfave/cli for zsh.
117-
118-
_%[1]s() {
119-
local -a opts # Declare a local array
120-
local current
121-
current=${words[-1]} # -1 means "the last element"
122-
if [[ "$current" == "-"* ]]; then
123-
# Current word starts with a hyphen, so complete flags/options
124-
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${current} --generate-shell-completion)}")
125-
else
126-
# Current word does not start with a hyphen, so complete subcommands
127-
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-shell-completion)}")
128-
fi
129-
130-
if [[ "${opts[1]}" != "" ]]; then
131-
_describe 'values' opts
132-
else
133-
_files
134-
fi
135-
}
136-
137-
# Don't run the completion function when being source-ed or eval-ed.
138-
# See https://github.com/urfave/cli/issues/1874 for discussion.
139-
if [ "$funcstack[1]" = "_%[1]s" ]; then
140-
_%[1]s
141-
fi
142-
`, appName)
143-
}
144-
145-
func genPwshCompletion() string {
146-
return `$fn = $($MyInvocation.MyCommand.Name)
147-
$name = $fn -replace "(.*)\.ps1$", '$1'
148-
Register-ArgumentCompleter -Native -CommandName $name -ScriptBlock {
149-
param($commandName, $wordToComplete, $cursorPosition)
150-
$other = "$wordToComplete --generate-shell-completion"
151-
Invoke-Expression $other | ForEach-Object {
152-
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
153-
}
154-
}`
155-
}

0 commit comments

Comments
 (0)