Advertisement
Guest User

Perl6/Panda completion

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