Skip to content

Commit

Permalink
include current word index for bash completion
Browse files Browse the repository at this point in the history
  • Loading branch information
stempler committed Jul 5, 2016
1 parent 94c1fdb commit e2c44f7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
7 changes: 6 additions & 1 deletion etc/bash_completion.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
_hale_join() { local d=$1; shift; echo -n "$1"; shift; printf "%s" "${@/#/$d}"; }

_hale()
{
compopt +o default

local cur="${COMP_WORDS[COMP_CWORD]}"

# delegate to hale command
local hale_result=$(hale --complete $COMP_CWORD ${COMP_WORDS[@]})
#local joined=$(_hale_join "' '" "${COMP_WORDS[@]}")
#local joined="'$joined'"
#local hale_result=$(hale --complete $COMP_CWORD $joined)
local hale_result=$(hale --complete $COMP_CWORD "${COMP_WORDS[@]}")

case "${hale_result}" in
# Reserved word FILE
Expand Down
5 changes: 3 additions & 2 deletions src/main/groovy/to/wetransform/halecli/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ public interface Command {
/**
* Return a Unix command to use to generate bash completions.
*
* @param args the list of arguments (ending with the proposal to complete)
* @param args the list of arguments
* @param current the index of the current argument to be completed
* @return the Unix command or <code>null</code>
*/
@Nullable
default String bashCompletion(List<String> args) {
default String bashCompletion(List<String> args, int current) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,22 @@ abstract class DelegatingCommand implements Command {
}
}

String bashCompletion(List<String> args) {
if (args.size() > 1) {
@Override
String bashCompletion(List<String> args, int current) {
if (current > 0) {
// delegate to command
String commandName = args[0]
Command command = subCommands[commandName]
if (command) {
command.bashCompletion(args[1..-1])
command.bashCompletion(args[1..-1], current - 1)
}
else {
null
}
}
else {
// complete subcommand
'compgen -W "help ' + subCommands.keySet().join(' ') + '" -- ' + args[-1]
'compgen -W "help ' + subCommands.keySet().join(' ') + '" -- ' + args[current]
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/groovy/to/wetransform/halecli/Runner.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Runner extends DelegatingCommand {
// strip first word (which is the base command)
words = words.size() > 1 ? words[1..-1] : []

String completion = bashCompletion(words)
String completion = bashCompletion(words, currentWord - 1)
if (completion) {
println completion
return 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ abstract class AbstractProjectCommand implements Command {
abstract boolean runForProject(ProjectTransformationEnvironment projectEnv, URI projectLocation,
OptionAccessor options, CommandContext context)

String bashCompletion(List<String> args) {
@Override
String bashCompletion(List<String> args, int current) {
//TODO handling for options? how to adapt in subclasses?

if (args) {
Expand Down

0 comments on commit e2c44f7

Please sign in to comment.