Skip to content
Merged
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
22 changes: 17 additions & 5 deletions lib/completely/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ module Commands
class Base < MisterBin::Command

class << self
def config_path_usage
param "CONFIG_PATH", "Path to the YAML configuration file [default: completely.yaml]"
def param_config_path
param "CONFIG_PATH", "Path to the YAML configuration file [default: completely.yaml]\nCan also be set by an environment variable"
end

def function_usage
def option_function
option "-f --function NAME", "Modify the name of the function in the generated script"
end

def debug_usage
def environment_config_path
environment "COMPLETELY_CONFIG_PATH", "Path to a completely configuration file [default: completely.yaml]"
end

def environment_debug
environment "COMPLETELY_DEBUG", "It not empty, the generated script will include an additional debugging snippet that outputs the compline and current word to a text file when a completion is requested"
end
end
Expand All @@ -29,7 +33,15 @@ def completions
end

def config_path
args['CONFIG_PATH'] || 'completely.yaml'
@config_path ||= args['CONFIG_PATH'] || ENV['COMPLETELY_CONFIG_PATH'] || 'completely.yaml'
end

def script_path
@script_path ||= args['SCRIPT_PATH'] || ENV['COMPLETELY_SCRIPT_PATH'] || "#{config_basename}.bash"
end

def config_basename
File.basename config_path, File.extname(config_path)
end

def syntax_warning
Expand Down
21 changes: 10 additions & 11 deletions lib/completely/commands/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@ module Commands
class Generate < Base
help "Generate the bash completion script to a file"

usage "completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]"
usage "completely generate [CONFIG_PATH SCRIPT_PATH --function NAME --wrap NAME]"
usage "completely generate (-h|--help)"

function_usage
option_function
option "-w --wrap NAME", "Wrap the completion script inside a function that echos the script. This is useful if you wish to embed it directly in your script"

config_path_usage
param "OUTPUT_PATH", "Path to the output bash script [default: completely.bash]"
debug_usage
param_config_path
param "SCRIPT_PATH", "Path to the output bash script. When not provided, the name of the input file will be used with a .bash extension\nCan also be set by an environment variable"

environment_config_path
environment "COMPLETELY_SCRIPT_PATH", "Path to the output bash script"
environment_debug

def run
wrap = args['--wrap']
output = wrap ? wrapper_function(wrap) : script
File.write output_path, output
say "Saved !txtpur!#{output_path}"
File.write script_path, output
say "Saved !txtpur!#{script_path}"
syntax_warning unless completions.valid?
end

Expand All @@ -29,10 +32,6 @@ def wrapper_function(wrapper_name)
completions.wrapper_function wrapper_name
end

def output_path
@output_path ||= args['OUTPUT_PATH'] || "completely.bash"
end

end
end
end
3 changes: 2 additions & 1 deletion lib/completely/commands/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class Init < Base
usage "completely init [CONFIG_PATH]"
usage "completely init (-h|--help)"

config_path_usage
param_config_path
environment_config_path

def run
if File.exist? config_path
Expand Down
7 changes: 4 additions & 3 deletions lib/completely/commands/preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ class Preview < Base
usage "completely preview [CONFIG_PATH --function NAME]"
usage "completely preview (-h|--help)"

function_usage
config_path_usage
debug_usage
option_function
param_config_path
environment_config_path
environment_debug

def run
puts script
Expand Down
4 changes: 2 additions & 2 deletions lib/completely/commands/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class Test < Base

param "COMPLINE", "The command to test completions for. This will be handled as if a TAB was pressed immediately at the end of it, so the last word is considered the active cursor. If you wish to complete for the next word instead, end your command with a space."

environment "COMPLETELY_CONFIG_PATH", "Path to a completely configuration file [default: completely.yaml]"
environment_config_path
environment "COMPLETELY_SCRIPT_PATH", "Path to a completions script. When set, this script will be tested instead of the completely configuration file"
environment "COMPLETELY_SCRIPT_FUNCTION", "The main completion function to call when using a custom script. If not set, the basename of the script path will be used, prefixed by an underscore"
debug_usage
environment_debug

example %q[completely test "mygit pu"]
example %q[completely test "mygit pull "]
Expand Down
6 changes: 5 additions & 1 deletion lib/completely/templates/template.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<%= function_name %>() {
local cur compline
_init_completion -s || return

cur=${COMP_WORDS[COMP_CWORD]}
compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"

Expand All @@ -20,7 +21,10 @@
case "$compline" in
% patterns.each do |pattern|
% next if pattern.empty?
<%= pattern.case_string %>) COMPREPLY=($(compgen <%= pattern.compgen %> -- "$cur")) ;;
<%= pattern.case_string %>)
COMPREPLY=($(compgen <%= pattern.compgen %> -- "$cur"))
;;

% end
esac
} &&
Expand Down
1 change: 1 addition & 0 deletions spec/approvals/cli/generate/custom-path-env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Saved hello.bash
1 change: 1 addition & 0 deletions spec/approvals/cli/generate/custom-path-env2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Saved spec/tmp/tada.bash
15 changes: 12 additions & 3 deletions spec/approvals/cli/generate/help
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Generate the bash completion script to a file

Usage:
completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]
completely generate [CONFIG_PATH SCRIPT_PATH --function NAME --wrap NAME]
completely generate (-h|--help)

Options:
Expand All @@ -18,11 +18,20 @@ Options:
Parameters:
CONFIG_PATH
Path to the YAML configuration file [default: completely.yaml]
Can also be set by an environment variable

OUTPUT_PATH
Path to the output bash script [default: completely.bash]
SCRIPT_PATH
Path to the output bash script. When not provided, the name of the input
file will be used with a .bash extension
Can also be set by an environment variable

Environment Variables:
COMPLETELY_CONFIG_PATH
Path to a completely configuration file [default: completely.yaml]

COMPLETELY_SCRIPT_PATH
Path to the output bash script

COMPLETELY_DEBUG
It not empty, the generated script will include an additional debugging
snippet that outputs the compline and current word to a text file when a
Expand Down
21 changes: 17 additions & 4 deletions spec/approvals/cli/generated-script
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,27 @@
_mygit_completions() {
local cur compline
_init_completion -s || return

cur=${COMP_WORDS[COMP_CWORD]}
compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"

case "$compline" in
'status'*) COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur")) ;;
'commit'*) COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur")) ;;
'init'*) COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur")) ;;
*) COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur")) ;;
'status'*)
COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur"))
;;

'commit'*)
COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur"))
;;

'init'*)
COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur"))
;;

*)
COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur"))
;;

esac
} &&
complete -F _mygit_completions mygit
Expand Down
21 changes: 17 additions & 4 deletions spec/approvals/cli/generated-script-alt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,27 @@
_mycomps() {
local cur compline
_init_completion -s || return

cur=${COMP_WORDS[COMP_CWORD]}
compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"

case "$compline" in
'status'*) COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur")) ;;
'commit'*) COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur")) ;;
'init'*) COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur")) ;;
*) COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur")) ;;
'status'*)
COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur"))
;;

'commit'*)
COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur"))
;;

'init'*)
COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur"))
;;

*)
COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur"))
;;

esac
} &&
complete -F _mycomps mygit
Expand Down
21 changes: 17 additions & 4 deletions spec/approvals/cli/generated-wrapped-script
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,27 @@ give_comps() {
echo $'_mygit_completions() {'
echo $' local cur compline'
echo $' _init_completion -s || return'
echo $''
echo $' cur=${COMP_WORDS[COMP_CWORD]}'
echo $' compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"'
echo $''
echo $' case "$compline" in'
echo $' \'status\'*) COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur")) ;;'
echo $' \'commit\'*) COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur")) ;;'
echo $' \'init\'*) COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur")) ;;'
echo $' *) COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur")) ;;'
echo $' \'status\'*)'
echo $' COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur"))'
echo $' ;;'
echo $''
echo $' \'commit\'*)'
echo $' COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur"))'
echo $' ;;'
echo $''
echo $' \'init\'*)'
echo $' COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur"))'
echo $' ;;'
echo $''
echo $' *)'
echo $' COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur"))'
echo $' ;;'
echo $''
echo $' esac'
echo $'} &&'
echo $'complete -F _mygit_completions mygit'
Expand Down
1 change: 1 addition & 0 deletions spec/approvals/cli/init/custom-path-env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Saved spec/tmp/hello.yml
5 changes: 5 additions & 0 deletions spec/approvals/cli/init/help
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ Options:
Parameters:
CONFIG_PATH
Path to the YAML configuration file [default: completely.yaml]
Can also be set by an environment variable

Environment Variables:
COMPLETELY_CONFIG_PATH
Path to a completely configuration file [default: completely.yaml]
4 changes: 4 additions & 0 deletions spec/approvals/cli/preview/help
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ Options:
Parameters:
CONFIG_PATH
Path to the YAML configuration file [default: completely.yaml]
Can also be set by an environment variable

Environment Variables:
COMPLETELY_CONFIG_PATH
Path to a completely configuration file [default: completely.yaml]

COMPLETELY_DEBUG
It not empty, the generated script will include an additional debugging
snippet that outputs the compline and current word to a text file when a
Expand Down
16 changes: 13 additions & 3 deletions spec/approvals/completions/function
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,23 @@ send_completions() {
echo $'_completely_completions() {'
echo $' local cur compline'
echo $' _init_completion -s || return'
echo $''
echo $' cur=${COMP_WORDS[COMP_CWORD]}'
echo $' compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"'
echo $''
echo $' case "$compline" in'
echo $' \'generate\'*) COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur")) ;;'
echo $' \'init\'*) COMPREPLY=($(compgen -W "--help" -- "$cur")) ;;'
echo $' *) COMPREPLY=($(compgen -W "--help --version init generate" -- "$cur")) ;;'
echo $' \'generate\'*)'
echo $' COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur"))'
echo $' ;;'
echo $''
echo $' \'init\'*)'
echo $' COMPREPLY=($(compgen -W "--help" -- "$cur"))'
echo $' ;;'
echo $''
echo $' *)'
echo $' COMPREPLY=($(compgen -W "--help --version init generate" -- "$cur"))'
echo $' ;;'
echo $''
echo $' esac'
echo $'} &&'
echo $'complete -F _completely_completions completely'
Expand Down
16 changes: 13 additions & 3 deletions spec/approvals/completions/script
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@
_completely_completions() {
local cur compline
_init_completion -s || return

cur=${COMP_WORDS[COMP_CWORD]}
compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"

case "$compline" in
'generate'*) COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur")) ;;
'init'*) COMPREPLY=($(compgen -W "--help" -- "$cur")) ;;
*) COMPREPLY=($(compgen -W "--help --version init generate" -- "$cur")) ;;
'generate'*)
COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur"))
;;

'init'*)
COMPREPLY=($(compgen -W "--help" -- "$cur"))
;;

*)
COMPREPLY=($(compgen -W "--help --version init generate" -- "$cur"))
;;

esac
} &&
complete -F _completely_completions completely
Expand Down
11 changes: 9 additions & 2 deletions spec/approvals/completions/script-only-spaces
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@
_completely_completions() {
local cur compline
_init_completion -s || return

cur=${COMP_WORDS[COMP_CWORD]}
compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"

case "$compline" in
'generate'*) COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur")) ;;
'init'*) COMPREPLY=($(compgen -W "--help" -- "$cur")) ;;
'generate'*)
COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur"))
;;

'init'*)
COMPREPLY=($(compgen -W "--help" -- "$cur"))
;;

esac
} &&
complete -F _completely_completions completely
Expand Down
1 change: 1 addition & 0 deletions spec/approvals/completions/script-with-debug
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
_completely_completions() {
local cur compline
_init_completion -s || return

cur=${COMP_WORDS[COMP_CWORD]}
compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"

Expand Down
Loading