Skip to content

Commit d7b2b70

Browse files
authored
Merge pull request #41 from DannyBen/add/test-pretty
Improve test command output and allow multiple complines in one run
2 parents 67b597e + e924571 commit d7b2b70

File tree

20 files changed

+283
-64
lines changed

20 files changed

+283
-64
lines changed

.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
--require spec_helper
12
--color
23
--format documentation
34
--fail-fast

.rubocop.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,3 @@ AllCops:
1818
Naming/AccessorMethodName:
1919
Exclude:
2020
- 'lib/completely/tester.rb'
21-
22-
# Allow long lines in specs and commands
23-
Layout/LineLength:
24-
Exclude:
25-
- 'spec/**/*'
26-
- 'lib/completely/commands/**/*'

lib/completely/commands/base.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,26 @@ module Commands
55
class Base < MisterBin::Command
66
class << self
77
def param_config_path
8-
param 'CONFIG_PATH', "Path to the YAML configuration file [default: completely.yaml]\nCan also be set by an environment variable"
8+
param 'CONFIG_PATH', <<~USAGE
9+
Path to the YAML configuration file [default: completely.yaml].
10+
Can also be set by an environment variable.
11+
USAGE
912
end
1013

1114
def option_function
12-
option '-f --function NAME', 'Modify the name of the function in the generated script'
15+
option '-f --function NAME',
16+
'Modify the name of the function in the generated script.'
1317
end
1418

1519
def environment_config_path
16-
environment 'COMPLETELY_CONFIG_PATH', 'Path to a completely configuration file [default: completely.yaml]'
20+
environment 'COMPLETELY_CONFIG_PATH',
21+
'Path to a completely configuration file [default: completely.yaml].'
1722
end
1823

1924
def environment_debug
20-
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'
25+
environment 'COMPLETELY_DEBUG', 'If not empty, the generated script will include ' \
26+
'an additional debugging snippet that outputs the compline and current word to ' \
27+
'a text file when a completion is requested.'
2128
end
2229
end
2330

lib/completely/commands/generate.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ class Generate < Base
99
usage 'completely generate (-h|--help)'
1010

1111
option_function
12-
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'
12+
option '-w --wrap NAME', 'Wrap the completion script inside a function that echos the ' \
13+
'script. This is useful if you wish to embed it directly in your script.'
1314

1415
param_config_path
15-
param 'OUTPUT_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"
16+
param 'OUTPUT_PATH', <<~USAGE
17+
Path to the output bash script.
18+
When not provided, the name of the input file will be used with a .bash extension.
19+
Can also be set by an environment variable.
20+
USAGE
1621

1722
environment_config_path
18-
environment 'COMPLETELY_OUTPUT_PATH', 'Path to the output bash script'
23+
environment 'COMPLETELY_OUTPUT_PATH', 'Path to the output bash script.'
1924
environment_debug
2025

2126
def run

lib/completely/commands/test.rb

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,55 @@ module Commands
55
class Test < Base
66
summary 'Test completions'
77

8-
help 'This command can be used to test that your completions script responds with the right completions. It works by reading your completely.yaml file, generating a completions script, and generating a temporary testing script.'
8+
help 'This command can be used to test that your completions script responds with ' \
9+
'the right completions. It works by reading your completely.yaml file, generating ' \
10+
'a completions script, and generating a temporary testing script.'
911

10-
usage 'completely test [--keep] COMPLINE'
12+
usage 'completely test [--keep] COMPLINE...'
1113
usage 'completely test (-h|--help)'
1214

13-
option '-k --keep', 'Keep the temporary testing script in the current directory'
15+
option '-k --keep', 'Keep the temporary testing script in the current directory.'
1416

15-
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.'
17+
param 'COMPLINE', 'One or more commands to test completions for. ' \
18+
'This will be handled as if a TAB was pressed immediately at the end of it, ' \
19+
'so the last word is considered the active cursor. ' \
20+
'If you wish to complete for the next word instead, end your command with a space.'
1621

1722
environment_config_path
1823
environment_debug
1924

20-
example 'completely test "mygit pu"'
21-
example 'completely test "mygit pull "'
25+
example 'completely test "mygit "'
26+
example 'completely test --keep "mygit status "'
27+
example 'completely test "mygit status --" "mygit init "'
2228

2329
def run
24-
puts tester.test(compline).join "\n"
25-
26-
if args['--keep']
27-
File.write 'completely-tester.sh', tester_script
28-
puts 'saved completely-tester.sh'
30+
complines.each_with_index do |compline, i|
31+
show_compline compline, filename: "completely-tester-#{i + 1}.sh"
2932
end
3033

3134
syntax_warning unless completions.valid?
3235
end
3336

3437
private
3538

36-
def compline
37-
args['COMPLINE']
39+
def show_compline(compline, filename: nil)
40+
filename ||= 'completely-tester.sh'
41+
say "!txtblu!$ !txtgrn!#{compline}!txtrst!<tab>"
42+
puts tester.test(compline).join "\n"
43+
puts
44+
45+
return unless keep
46+
47+
File.write filename, tester_script(compline)
48+
say "Saved !txtpur!#{filename}"
49+
end
50+
51+
def complines
52+
@complines ||= args['COMPLINE']
53+
end
54+
55+
def keep
56+
@keep ||= args['--keep']
3857
end
3958

4059
def completions
@@ -45,8 +64,8 @@ def tester
4564
@tester ||= completions.tester
4665
end
4766

48-
def tester_script
49-
@tester_script ||= tester.tester_script compline
67+
def tester_script(compline)
68+
tester.tester_script compline
5069
end
5170
end
5271
end

spec/approvals/cli/generate/help

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,34 @@ Usage:
66

77
Options:
88
-f --function NAME
9-
Modify the name of the function in the generated script
9+
Modify the name of the function in the generated script.
1010

1111
-w --wrap NAME
1212
Wrap the completion script inside a function that echos the script. This is
13-
useful if you wish to embed it directly in your script
13+
useful if you wish to embed it directly in your script.
1414

1515
-h --help
1616
Show this help
1717

1818
Parameters:
1919
CONFIG_PATH
20-
Path to the YAML configuration file [default: completely.yaml]
21-
Can also be set by an environment variable
20+
Path to the YAML configuration file [default: completely.yaml].
21+
Can also be set by an environment variable.
2222

2323
OUTPUT_PATH
24-
Path to the output bash script. When not provided, the name of the input
25-
file will be used with a .bash extension
26-
Can also be set by an environment variable
24+
Path to the output bash script.
25+
When not provided, the name of the input file will be used with a .bash
26+
extension.
27+
Can also be set by an environment variable.
2728

2829
Environment Variables:
2930
COMPLETELY_CONFIG_PATH
30-
Path to a completely configuration file [default: completely.yaml]
31+
Path to a completely configuration file [default: completely.yaml].
3132

3233
COMPLETELY_OUTPUT_PATH
33-
Path to the output bash script
34+
Path to the output bash script.
3435

3536
COMPLETELY_DEBUG
36-
It not empty, the generated script will include an additional debugging
37+
If not empty, the generated script will include an additional debugging
3738
snippet that outputs the compline and current word to a text file when a
38-
completion is requested
39+
completion is requested.

spec/approvals/cli/init/help

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ Options:
1010

1111
Parameters:
1212
CONFIG_PATH
13-
Path to the YAML configuration file [default: completely.yaml]
14-
Can also be set by an environment variable
13+
Path to the YAML configuration file [default: completely.yaml].
14+
Can also be set by an environment variable.
1515

1616
Environment Variables:
1717
COMPLETELY_CONFIG_PATH
18-
Path to a completely configuration file [default: completely.yaml]
18+
Path to a completely configuration file [default: completely.yaml].

spec/approvals/cli/preview/help

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ Usage:
66

77
Options:
88
-f --function NAME
9-
Modify the name of the function in the generated script
9+
Modify the name of the function in the generated script.
1010

1111
-h --help
1212
Show this help
1313

1414
Parameters:
1515
CONFIG_PATH
16-
Path to the YAML configuration file [default: completely.yaml]
17-
Can also be set by an environment variable
16+
Path to the YAML configuration file [default: completely.yaml].
17+
Can also be set by an environment variable.
1818

1919
Environment Variables:
2020
COMPLETELY_CONFIG_PATH
21-
Path to a completely configuration file [default: completely.yaml]
21+
Path to a completely configuration file [default: completely.yaml].
2222

2323
COMPLETELY_DEBUG
24-
It not empty, the generated script will include an additional debugging
24+
If not empty, the generated script will include an additional debugging
2525
snippet that outputs the compline and current word to a text file when a
26-
completion is requested
26+
completion is requested.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env bash
2+
if [[ -n $ZSH_VERSION ]]; then
3+
autoload -U +X bashcompinit && bashcompinit
4+
autoload -U +X compinit && compinit
5+
fi
6+
7+
# === COMPLETION SCRIPT START ===
8+
9+
# mygit completion -*- shell-script -*-
10+
11+
# This bash completions script was generated by
12+
# completely (https://github.com/dannyben/completely)
13+
# Modifying it manually is not recommended
14+
15+
_mygit_completions_filter() {
16+
local words="$1"
17+
local cur=${COMP_WORDS[COMP_CWORD]}
18+
local result=()
19+
20+
if [[ "${cur:0:1}" == "-" ]]; then
21+
echo "$words"
22+
23+
else
24+
for word in $words; do
25+
[[ "${word:0:1}" != "-" ]] && result+=("$word")
26+
done
27+
28+
echo "${result[*]}"
29+
30+
fi
31+
}
32+
33+
_mygit_completions() {
34+
local cur=${COMP_WORDS[COMP_CWORD]}
35+
local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
36+
local compline="${compwords[*]}"
37+
38+
case "$compline" in
39+
'status'*)
40+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_mygit_completions_filter "--help --verbose --branch $(git branch 2> /dev/null)")" -- "$cur" )
41+
;;
42+
43+
'commit'*)
44+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A file -W "$(_mygit_completions_filter "--help --message --all -a --quiet -q")" -- "$cur" )
45+
;;
46+
47+
'init'*)
48+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "$(_mygit_completions_filter "--bare")" -- "$cur" )
49+
;;
50+
51+
*)
52+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_mygit_completions_filter "--help --version status init commit")" -- "$cur" )
53+
;;
54+
55+
esac
56+
} &&
57+
complete -F _mygit_completions mygit
58+
59+
# ex: filetype=sh
60+
61+
62+
# === COMPLETION SCRIPT END ===
63+
64+
COMP_WORDS=( mygit -- )
65+
COMP_LINE="mygit --"
66+
COMP_POINT=${#COMP_LINE}
67+
COMP_CWORD=1
68+
69+
_mygit_completions
70+
for suggestion in "${COMPREPLY[@]}"; do
71+
echo "$suggestion"
72+
done
73+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env bash
2+
if [[ -n $ZSH_VERSION ]]; then
3+
autoload -U +X bashcompinit && bashcompinit
4+
autoload -U +X compinit && compinit
5+
fi
6+
7+
# === COMPLETION SCRIPT START ===
8+
9+
# mygit completion -*- shell-script -*-
10+
11+
# This bash completions script was generated by
12+
# completely (https://github.com/dannyben/completely)
13+
# Modifying it manually is not recommended
14+
15+
_mygit_completions_filter() {
16+
local words="$1"
17+
local cur=${COMP_WORDS[COMP_CWORD]}
18+
local result=()
19+
20+
if [[ "${cur:0:1}" == "-" ]]; then
21+
echo "$words"
22+
23+
else
24+
for word in $words; do
25+
[[ "${word:0:1}" != "-" ]] && result+=("$word")
26+
done
27+
28+
echo "${result[*]}"
29+
30+
fi
31+
}
32+
33+
_mygit_completions() {
34+
local cur=${COMP_WORDS[COMP_CWORD]}
35+
local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
36+
local compline="${compwords[*]}"
37+
38+
case "$compline" in
39+
'status'*)
40+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_mygit_completions_filter "--help --verbose --branch $(git branch 2> /dev/null)")" -- "$cur" )
41+
;;
42+
43+
'commit'*)
44+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A file -W "$(_mygit_completions_filter "--help --message --all -a --quiet -q")" -- "$cur" )
45+
;;
46+
47+
'init'*)
48+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "$(_mygit_completions_filter "--bare")" -- "$cur" )
49+
;;
50+
51+
*)
52+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_mygit_completions_filter "--help --version status init commit")" -- "$cur" )
53+
;;
54+
55+
esac
56+
} &&
57+
complete -F _mygit_completions mygit
58+
59+
# ex: filetype=sh
60+
61+
62+
# === COMPLETION SCRIPT END ===
63+
64+
COMP_WORDS=( mygit st )
65+
COMP_LINE="mygit st"
66+
COMP_POINT=${#COMP_LINE}
67+
COMP_CWORD=1
68+
69+
_mygit_completions
70+
for suggestion in "${COMPREPLY[@]}"; do
71+
echo "$suggestion"
72+
done
73+

0 commit comments

Comments
 (0)