Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rmapi completions #839

Open
benneti opened this issue Jan 19, 2022 · 0 comments
Open

rmapi completions #839

benneti opened this issue Jan 19, 2022 · 0 comments

Comments

@benneti
Copy link

benneti commented Jan 19, 2022

Hi, I am not sure whether this is the right place but I spent a bit of time trying to wrap my head around how to write a completion function for rmapi https://github.com/juruen/rmapi but it does not work yet.

This is how far I came so far:

#compdef rmapi

_rmapi_remote_files() {
    local remfiles remdispf remdispd ret=1 pfx='\^/' sub='^/' suf expl
    curdir=${words[CURRENT]%/*} # current string up to before /*
    remfiles=(${(f)"$(_call_program files rmapi ls $IPREFIX${${PREFIX%%[^/]#}/#$pfx/$sub} | grep '^\[(d|f)\]' | sed -r 's|^\[d\]\s*(.+)|\1/|' | sed -r 's|^\[f\]\s*(.+)|\1|')"})
    compset -P '*/'
    compset -S '/*' || suf=file

    remdispf=(${remfiles:#*/})
    remdispd=(${(M)remfiles:#*/})
    _tags files
    while _tags; do
      while _next_label files expl ${suf:-directory}; do
        # add files, unless there is a '/' immediately to the right
        [[ -n $suf ]] &&
          compadd -S ' ' "$@" "$expl[@]" -d remdispf -- $remdispf && ret=0
        # add directories; use empty suffix if there is a '/' immediately to the right
        compadd "${suf:+-S /}" "$@" "$expl[@]" -d remdispd -- ${remdispd%/} && ret=0
      done
      (( ret )) || return 0
    done
    return 1
}

_rmapi() {
  local context state line
  typeset -A opt_args
  # -C allows to change curcontext
  _arguments -C \
    '1:cmd:->cmds' \
    '*:: :->args' \
  && ret=0

  case "$state" in
    (cmds)
       local commands; commands=(
         'put:upload'
         'get:download'
         'mget:download directory'
         'ls:list'
       )

       _describe -t commands 'command' commands && ret=0
  ;;
  (args)
    case $words[1] in
      ls)
        _rmapi_remote_files
      ;;
      get)
        _alternative "files:file:_rmapi_remote_files"
      ;;
      mget)
        _arguments "-o[output directory]:file:_files -/" "*:: :_rmapi_remote_files"
      ;;
      put)
        _files -g "*.(pdf|epub)"
      ;;
    esac
esac
}

_rmapi "$@"

Now I struggle with getting suggestions for the remote files, the output of rmapi ls some/directory/ is of this form

[f]	some_file
[f]	A File with Spaces in Name
[d]	directory

which should then result in the correct remdispf and remdispd.
But I do not really wrap my head around what happens after this (I simply tried to copy it from svn and or _remote_files) or does not as only completion of the commands works (and local files) but not remote.
If someone could help with that it would be amazing (also it would be nice to be able to use "-/" to only get directories like with files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant