ipbus/ipbb

Tab completion for packages/components

Closed this issue · 1 comments

Several ipbb commands take as argument or option a component identifier (<pkg>:<cmp>) or a package name. Tab-completion is particularly useful in such cases to reduce the chances of typos. This is now possible thanks to Click 7.0, that supports user-defined autocompletion functions.

Preliminary thougths:
packages: The completion is straightforward. The list of packages is available from Environment object

components: Much trickier. It requires a match on the package and then scanning the package fomder for component candidates. They are identifiable as folders with a firmware subfolder.
Some examples below of the expected behaviour

  • p -> pkgA:, pkgB:
  • pkgA -> pkgA:
  • pkgA: -> pkgA:components/core, pkgA:projects/example
  • pkgA:p -> pkgA:projects
  • pkgA:projects/ -> pkgA:projects/example
  • pkgA:projects/ex -> pkgA:projects/example
  • pkgA:projects/example/ -> pkgA:projects/example

Note:
Bash considers : as a word breaker character, and when invoking the completion command splits any argument containing : in 3 (a:b -> a, :, b)
This is not straightforward to circumvent, but it's possible by patching the auto-generated click auto-complete functions to recover the original arguments with the _get_comp_words_by_ref function (part of bashcomplete).
Example here:

    local IFS=$'
'
    local cword wordsi cur
    _get_comp_words_by_ref -n : cword words cur

    COMPREPLY=( $( env COMP_WORDS="${words[*]}" \
                   COMP_CWORD=$cword \
                   _IPBB_COMPLETE=complete $1 ) )

    __ltrim_colon_completions "$cur"

    return 0
}```

All working in dev/master. Closing.