Skip to content

Commit 6eb0041

Browse files
authored
Merge pull request #393 from DannyBen/add/completions
Add completions for bashly itself
2 parents b7030dd + 0e42d3f commit 6eb0041

File tree

14 files changed

+588
-20
lines changed

14 files changed

+588
-20
lines changed

lib/bashly/cli.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ def self.runner
99
header: 'Bashly - Bash CLI Generator',
1010
footer: "Help: m`bashly COMMAND --help`\nDocs: bu`https://bashly.dannyb.co`"
1111

12-
runner.route 'init', to: Commands::Init
13-
runner.route 'preview', to: Commands::Preview
14-
runner.route 'validate', to: Commands::Validate
15-
runner.route 'generate', to: Commands::Generate
16-
runner.route 'add', to: Commands::Add
17-
runner.route 'doc', to: Commands::Doc
18-
runner.route 'shell', to: Commands::Shell unless ENV['BASHLY_SHELL']
12+
runner.route 'init', to: Commands::Init
13+
runner.route 'preview', to: Commands::Preview
14+
runner.route 'validate', to: Commands::Validate
15+
runner.route 'generate', to: Commands::Generate
16+
runner.route 'add', to: Commands::Add
17+
runner.route 'doc', to: Commands::Doc
18+
runner.route 'completions', to: Commands::Completions
19+
runner.route 'shell', to: Commands::Shell unless ENV['BASHLY_SHELL']
1920

2021
runner
2122
end

lib/bashly/commands/completions.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
module Bashly
2+
module Commands
3+
class Completions < Base
4+
summary 'Install bash completions for bashly itself'
5+
help 'Display the bash completions script or install it directly to your bash completions directory'
6+
7+
usage 'bashly completions [--install]'
8+
usage 'bashly completions (-h|--help)'
9+
10+
option '-i --install', 'Install the completions script to your bash completions directory'
11+
12+
def run
13+
if args['--install']
14+
install_completions
15+
else
16+
show_completions
17+
end
18+
end
19+
20+
private
21+
22+
def install_completions
23+
raise Error, 'Cannot find completions directory' unless compdir
24+
25+
target = "#{compdir}/bashly"
26+
27+
say "Installing completions to m`#{target}`"
28+
command = %[cp "#{completions_path}" "#{target}"]
29+
command = "sudo #{command}" unless root_user?
30+
system command
31+
32+
say 'Restart your session for the changes to take effect.'
33+
end
34+
35+
def show_completions
36+
puts completions_script
37+
end
38+
39+
def completions_path
40+
@completions_path ||= asset('completions/bashly-completions.bash')
41+
end
42+
43+
def completions_script
44+
@completions_script ||= asset_content('completions/bashly-completions.bash')
45+
end
46+
47+
def compdir
48+
@compdir ||= compdir!
49+
end
50+
51+
def compdir!
52+
compdir_candidates.each { |dir| return dir if Dir.exist? dir }
53+
nil
54+
end
55+
56+
def compdir_candidates
57+
@compdir_candidates ||= [
58+
'/usr/share/bash-completion/completions',
59+
'/usr/local/etc/bash_completion.d',
60+
]
61+
end
62+
63+
def root_user?
64+
Process.uid.zero?
65+
end
66+
end
67+
end
68+
end

lib/bashly/completions/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Completions for bashly executable
2+
3+
This directory contains templates for generating bash completions.
4+
5+
## For developers
6+
7+
From the root directory of the repository, run `run completions`. This will:
8+
9+
1. Read the `completely.yaml.gtx` template
10+
2. Write the `completely.yaml` file (to allow testing with completely)
11+
3. Generate the `bashly-completions.bash` file
12+
4. Copy it to the completions directory with `sudo`
13+
14+
Note that for production use, only the `bashly-completions.bash` is used.
15+
16+
## For users
17+
18+
Install completions in one of two ways:
19+
20+
1. Run `bashly completions --install`. This will make a best effort to copy
21+
the completions script to your completions directory.
22+
2. If the above fails, run `bashly completions > out.bash`, then copy the file
23+
manually to your completions directory (or simply get the
24+
`bashly-completions.bash` from this directory).
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# bashly completion -*- shell-script -*-
2+
3+
# This bash completions script was generated by
4+
# completely (https://github.com/dannyben/completely)
5+
# Modifying it manually is not recommended
6+
7+
_bashly_completions_filter() {
8+
local words="$1"
9+
local cur=${COMP_WORDS[COMP_CWORD]}
10+
local result=()
11+
12+
if [[ "${cur:0:1}" == "-" ]]; then
13+
echo "$words"
14+
15+
else
16+
for word in $words; do
17+
[[ "${word:0:1}" != "-" ]] && result+=("$word")
18+
done
19+
20+
echo "${result[*]}"
21+
22+
fi
23+
}
24+
25+
_bashly_completions() {
26+
local cur=${COMP_WORDS[COMP_CWORD]}
27+
local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
28+
local compline="${compwords[*]}"
29+
30+
case "$compline" in
31+
'generate'*'--env')
32+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "development production")" -- "$cur" )
33+
;;
34+
35+
'generate'*'-e')
36+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "development production")" -- "$cur" )
37+
;;
38+
39+
'completions'*)
40+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "--help -h --install -i")" -- "$cur" )
41+
;;
42+
43+
'validate'*)
44+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "--help -h --verbose -v")" -- "$cur" )
45+
;;
46+
47+
'generate'*)
48+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "--help -h --env --force --quiet --upgrade --watch --wrap -e -f -q -r -u -w")" -- "$cur" )
49+
;;
50+
51+
'preview'*)
52+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "--help -h")" -- "$cur" )
53+
;;
54+
55+
'g'*'--env')
56+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "development production")" -- "$cur" )
57+
;;
58+
59+
'shell'*)
60+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "--help -h")" -- "$cur" )
61+
;;
62+
63+
'init'*)
64+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "--help -h --minimal -m")" -- "$cur" )
65+
;;
66+
67+
'g'*'-e')
68+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "development production")" -- "$cur" )
69+
;;
70+
71+
'add'*)
72+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "--help -h --force --list --source -f -l -s colors completions completions_script completions_yaml config help hooks lib settings strings test validations yaml")" -- "$cur" )
73+
;;
74+
75+
'doc'*)
76+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "--help -h --index -i arg arg.allowed arg.default arg.help arg.name arg.repeatable arg.required arg.validate command command.alias command.args command.catch_all command.commands command.completions command.default command.dependencies command.environment_variables command.examples command.expose command.extensible command.filename command.filters command.flags command.footer command.function command.group command.help command.name command.private command.version environment_variable environment_variable.default environment_variable.help environment_variable.name environment_variable.private environment_variable.required flag flag.allowed flag.arg flag.completions flag.conflicts flag.default flag.help flag.long flag.private flag.repeatable flag.required flag.short flag.validate")" -- "$cur" )
77+
;;
78+
79+
'i'*)
80+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "--help -h --minimal -m")" -- "$cur" )
81+
;;
82+
83+
'p'*)
84+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "--help -h")" -- "$cur" )
85+
;;
86+
87+
'v'*)
88+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "--help -h --verbose -v")" -- "$cur" )
89+
;;
90+
91+
'g'*)
92+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "--help -h --env --force --quiet --upgrade --watch --wrap -e -f -q -r -u -w")" -- "$cur" )
93+
;;
94+
95+
'a'*)
96+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "--help -h --force --list --source -f -l -s colors completions completions_script completions_yaml config help hooks lib settings strings test validations yaml")" -- "$cur" )
97+
;;
98+
99+
'c'*)
100+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "--help -h --install -i")" -- "$cur" )
101+
;;
102+
103+
's'*)
104+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "--help -h")" -- "$cur" )
105+
;;
106+
107+
*)
108+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "--help -h --version -v init preview validate generate add doc completions shell")" -- "$cur" )
109+
;;
110+
111+
esac
112+
} &&
113+
complete -F _bashly_completions bashly
114+
115+
# ex: filetype=sh
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
bashly:
2+
- --help
3+
- -h
4+
- --version
5+
- -v
6+
- init
7+
- preview
8+
- validate
9+
- generate
10+
- add
11+
- doc
12+
- completions
13+
- shell
14+
15+
bashly init: &init
16+
- --help
17+
- -h
18+
- --minimal
19+
- -m
20+
21+
bashly i: *init
22+
23+
bashly preview: &preview
24+
- --help
25+
- -h
26+
27+
bashly p: *preview
28+
29+
bashly validate: &validate
30+
- --help
31+
- -h
32+
- --verbose
33+
- -v
34+
35+
bashly v: *validate
36+
37+
bashly generate: &generate
38+
- --help
39+
- -h
40+
- --env
41+
- --force
42+
- --quiet
43+
- --upgrade
44+
- --watch
45+
- --wrap
46+
- -e
47+
- -f
48+
- -q
49+
- -r
50+
- -u
51+
- -w
52+
53+
bashly g: *generate
54+
55+
bashly generate*--env: &env
56+
- development
57+
- production
58+
59+
bashly generate*-e: *env
60+
bashly g*--env: *env
61+
bashly g*-e: *env
62+
63+
bashly add: &add
64+
- --help
65+
- -h
66+
- --force
67+
- --list
68+
- --source
69+
- -f
70+
- -l
71+
- -s
72+
- colors
73+
- completions
74+
- completions_script
75+
- completions_yaml
76+
- config
77+
- help
78+
- hooks
79+
- lib
80+
- settings
81+
- strings
82+
- test
83+
- validations
84+
- yaml
85+
86+
bashly a: *add
87+
88+
bashly doc: &doc
89+
- --help
90+
- -h
91+
- --index
92+
- -i
93+
- arg
94+
- arg.allowed
95+
- arg.default
96+
- arg.help
97+
- arg.name
98+
- arg.repeatable
99+
- arg.required
100+
- arg.validate
101+
- command
102+
- command.alias
103+
- command.args
104+
- command.catch_all
105+
- command.commands
106+
- command.completions
107+
- command.default
108+
- command.dependencies
109+
- command.environment_variables
110+
- command.examples
111+
- command.expose
112+
- command.extensible
113+
- command.filename
114+
- command.filters
115+
- command.flags
116+
- command.footer
117+
- command.function
118+
- command.group
119+
- command.help
120+
- command.name
121+
- command.private
122+
- command.version
123+
- environment_variable
124+
- environment_variable.default
125+
- environment_variable.help
126+
- environment_variable.name
127+
- environment_variable.private
128+
- environment_variable.required
129+
- flag
130+
- flag.allowed
131+
- flag.arg
132+
- flag.completions
133+
- flag.conflicts
134+
- flag.default
135+
- flag.help
136+
- flag.long
137+
- flag.private
138+
- flag.repeatable
139+
- flag.required
140+
- flag.short
141+
- flag.validate
142+
bashly completions: &completions
143+
- --help
144+
- -h
145+
- --install
146+
- -i
147+
148+
bashly c: *completions
149+
bashly shell: &shell
150+
- --help
151+
- -h
152+
153+
bashly s: *shell

0 commit comments

Comments
 (0)