Advertisement
Guest User

FireFish5000 perl6/panda/rakudobrew completion

a guest
Sep 6th, 2014
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.08 KB | None | 0 0
  1. # bash completion for (rakudo?) perl6
  2. # Usefull looking things that may or may not exist
  3. # _filedir
  4.  
  5. # Prepending with _perl6_ so there are no conflicts when sourcing multiple files
  6. _perl6_match() { # Succeeds if argument is repeated. Must be an EXACT MATCH.
  7.         #usage,  _perl6_match 'str to match' a list of str #returns fale/1;
  8.         #usage,  _perl6_match 'matched str' a 'matched str' was found  #returns true/0;
  9.     repcur="$1"
  10.     shift
  11.     for val in "$@"; do
  12.         [[ "${repcur}" == "${val}" ]] && return 0
  13.     done
  14.     return 1
  15. }
  16. _perl6_assoc() { # Removes associated arguments. usage, from _perl6_rem: _perl6_assoc $i -h --help
  17.     asscur=$1; shift
  18.     akv=($@)
  19.     for (( k=0; k<$((${#akv[@]}-1)); k+=2)); do
  20.         v=$(($k+1))
  21.         [[ "$asscur" == "${akv[$k]}" ]] && {
  22.             COMPREPLY=" ${COMPREPLY[@]} "
  23.             COMPREPLY=(${COMPREPLY/ ${akv[$v]%% *} / })
  24.         }
  25.         [[ "$asscur" == "${akv[$v]}" ]] && {
  26.             COMPREPLY=" ${COMPREPLY[@]} "
  27.             COMPREPLY=(${COMPREPLY/ ${akv[$k]%% *} / })
  28.         }
  29.     done
  30.     printf "%s\n" "${COMPREPLY[@]}"
  31. }
  32. _perl6_seper() { # Prints the arguments that are deliminated by 'cnt' number of 'sep's. ie:
  33.         # _perl6_seper 1 '--' -a -1 -- -2 -b -- c 3 ; #yeilds -2 -b; _perl6_seper 0 yeilds -a -1
  34.     cnt=$1; shift
  35.     sep=$1; shift
  36.     args=("$@")
  37.     idx=0
  38.     zarg=0
  39.     for ((iter=0; iter<=$cnt; iter++)); do
  40.         zarg=1
  41.         while [[ "$idx" -lt "${#args[@]}" ]]; do
  42.             zarg=0
  43.             arg="${args[$idx]}"; ((idx++))
  44.             [[ "$arg" == "$sep" ]] && {
  45.                 [ "$iter" -eq "$cnt" ] && return 0
  46.                 break
  47.             }
  48.             [[ "$iter" == "$cnt" ]] && printf "%s\n" "${arg}" # Im not sure how this will handle spaces
  49.         done
  50.         [ "$zarg" -ne "0" ] && return 1
  51.         iterend=$iter
  52.     done
  53.     [ "$iterend" -eq "$cnt" ] && return 0 # may chose to do 3 way in future
  54.     return 1
  55. }
  56. # NOTE: This slows things down!
  57. # Taken from gentoo's adaption of Ian Macdonald's bash_completion.
  58. # (Adapted from bash_completion by Ian Macdonald <ian@caliban.org>)
  59. # This removes any options from the list of completions that have
  60. # already been specified on the command line.
  61. _perl6_rem() {  # removes arguments previously used.
  62.             #usage: _perl6_rem repeatable words -- associated pairs -- words we shouldn't add spaces after
  63.     rep_words=($(_perl6_seper '0' '--' "$@")) # It looks like we can quote all of these without error. consider doing so.
  64.     assoc_words=($(_perl6_seper '1' '--' "$@"))
  65.     nospace_words=($(_perl6_seper '2' '--' "$@"))
  66.         COMPREPLY=($(echo "${COMP_WORDS[@]}" | \
  67.         (while read -d ' ' i; do
  68.             [[ -z ${i} ]] && continue
  69.         _perl6_match "${i}" "${rep_words[@]}" && continue
  70.         COMPREPLY=($(_perl6_assoc "${i}" "${assoc_words[@]}"))
  71.         # flatten array with spaces on either side,
  72.             # otherwise we cannot grep on word boundaries of
  73.             # first and last word
  74.             COMPREPLY=" ${COMPREPLY[@]} "
  75.             # remove word from list of completions
  76.             COMPREPLY=("${COMPREPLY/ ${i%% *} / }") # FIXME doesnt work for --target\= and alike
  77.         done
  78.         echo ${COMPREPLY[@]})))
  79.     [[ "${#COMPREPLY[@]}" -eq "1" ]] && [[ "${#nospace_words[@]}" -ge '1' ]] &&  _perl6_match "$COMPREPLY" "${nospace_words[@]}" && compopt -o nospace
  80.         return 0
  81. }
  82. _panda_read_json_perl() { # FIXME If we cannot do it all in perl5, we should do it all in perl6.
  83. perl <<'HEREPERL'
  84.     use feature qw{say};
  85.     use utf8;
  86.     binmode STDOUT, q{:utf8};
  87.     use Module::Load::Conditional qw[can_load]; # builtin, so safe?
  88.     my $json;
  89.     my %Mods;
  90.     my @CompUnits = qx{perl6 -e 'for @*INC { say $_.Str if $_.^name === "CompUnitRepo::Local::Installation" ;}'};
  91.     chomp (@CompUnits);
  92.     COMP_UNIT:
  93.     foreach my $CompUnit (@CompUnits) {
  94.         {
  95.             local $/;
  96.             next COMP_UNIT unless (-f $CompUnit . q{/panda/projects.json});
  97.             open my $fh, q{<}, $CompUnit . q{/panda/projects.json};
  98.             $json = <$fh>;
  99.             close $fh;
  100.         }
  101.         # NOTE! Module::Load::Conditional::can_load return false if it can load the module, and true if it cannot. (is this a bug?)
  102.         unless ( can_load(modules => q{JSON}) ) {
  103.             require JSON; JSON->import(qw{decode_json});
  104.    
  105.             foreach ( @{decode_json($json)} ){
  106.                     $Mods{"$_->{name}"}=1; # consider getting installation status
  107.             }
  108.         } else { # because requiring perl5 mods wont fly
  109.             while ($json =~ m/ \" name \" \: \" ([^\"]*) \" /gx) {
  110.                     $Mods{"$1"}=1;
  111.             }
  112.         }
  113.     }
  114.     foreach my $key (keys %Mods) {
  115.         say "$key";
  116.     }
  117. HEREPERL
  118. }
  119.  
  120. _panda_modules() { # TIMTOWTDI. (actualy, I think the container may change in future versions(or implimintations).. but feel free to try seding/greping/whatever instead)
  121.     _panda_read_json_perl
  122. }
  123.  
  124.  
  125. _panda() {
  126.     local cur prev words cword
  127.     _init_completion || return
  128.     list_args="--verbrose --installed"
  129.     install_args="--notests --nodeps"
  130.     case $prev in
  131.         install|--notests|--nodeps)
  132.             #COMPREPLY=( $( compgen -W 'install $install_args $( _panda_modules )'  -- "$cur" ) )
  133.             COMPREPLY=( $( compgen -W 'install $install_args'  -- "$cur" ) )
  134.             _perl6_match 'install' "${COMP_WORDS[@]}" && COMPREPLY+=( $( compgen -W '$( _panda_modules )'  -- "$cur" ) )
  135.             _perl6_rem;
  136.             return 0
  137.             ;;
  138.         list|--verbrose|--installed)
  139.             #COMPREPLY=( $( compgen -W 'list $list_args $( _panda_modules )' -- "$cur" ) )
  140.             COMPREPLY=( $( compgen -W 'list $list_args' -- "$cur" ) )
  141.             _perl6_match 'list' "${COMP_WORDS[@]}" && COMPREPLY+=( $( compgen -W '$( _panda_modules )'  -- "$cur" ) )
  142.             _perl6_rem;
  143.             return 0
  144.             ;;
  145.         search|info|update)
  146.             COMPREPLY=( $( compgen -W '$( _panda_modules )'  -- "$cur" ) )
  147.             _perl6_rem;
  148.             return 0
  149.             ;;
  150.     esac
  151.     if [[ "$cur" == * ]]; then
  152.         COMPREPLY=( $( compgen -W '$list_args $install_args update info search install list' -- "$cur" ) )
  153.     fi
  154. }
  155. _rakudobrew() {
  156.     local cur prev words cword
  157.     _init_completion || return
  158.     backends="parrot jvm moar moar_jit"
  159.     case $prev in
  160.             switch)
  161.                 COMPREPLY=( $( compgen -W '$backends'  -- "$cur" ) )
  162.                 _perl6_rem
  163.                 return 0
  164.                 ;;
  165.             build)
  166.                 COMPREPLY=( $( compgen -W 'all $backends'  -- "$cur" ) )
  167.                 _perl6_rem
  168.                 return 0
  169.                 ;;
  170.     esac
  171.     if  ( [[ "${#COMP_WORDS[@]}" -ge '3' ]] && [[ "${COMP_WORDS[-3]}" == "build" ]] && _perl6_match "$prev" $backends ); then
  172.         COMPREPLY=( $( compgen -W '2014,08 2014.07 2014.06 HEAD'  -- "$cur" ) ) # TODO version
  173.     elif [[ "$cur" == * ]]; then
  174.         COMPREPLY=( $( compgen -W 'switch rehash list current build build-panda' -- "$cur" ) )
  175.         _perl6_rem
  176.     fi
  177. }
  178. _perl6() {
  179.     local cur prev words cword
  180.     _init_completion || return
  181.     case $prev in
  182.             --encoding)
  183.                 COMPREPLY=( $( compgen -W 'utf8'  -- "$cur" ) )
  184.                 return 0
  185.                 ;;
  186.     esac
  187.     longargs="--doc --help --stagestats --ll-exception --profile"
  188.     unlong="--target\= --trace\= --encoding\= --output\="
  189.     shortargs="-c -h -e -n -p -t -o -v"
  190.     unshort=''
  191.     repargs="--output --encoding -e -n -p -o" # Repeatable Arguments
  192.     assocargs="-h --help"   # Assosiated Arguments (only ones that cannot repeat)
  193.     if [[ "$cur" == --* ]]; then
  194.         COMPREPLY=( $( compgen -W '$longargs $unlong' -- "$cur" ) )
  195.         _perl6_rem $repargs '--' $assocargs -- $unlong
  196.     elif [[ "$cur" == -* ]]; then
  197.         COMPREPLY=( $( compgen -W '$shortargs' -- "$cur" ) )
  198.         _perl6_rem $repargs '--' $assocargs
  199.     fi
  200. } # &&
  201. complete -F _perl6 -o default perl6{,m,j,p} perl6-debug-{m,j,p}
  202. complete -F _panda -o default panda
  203. complete -F _rakudobrew -o default rakudobrew
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement