andreafrancia / trash-cli

Command line interface to the freedesktop.org trashcan.
GNU General Public License v2.0
3.62k stars 177 forks source link

ZSH completion does not allow to specify multiple files to trash #338

Open ohquero opened 6 months ago

ohquero commented 6 months ago

Describe the bug Commands provided by this utility allow to specify multiple files to manipulate, but ZSH autocompletions suggest filenames only for one parameter.

trash-cli version 0.23.11.10

Are you using the latest version of trash-cli? Yes

Have you tried if the bug is present in the latest version of trash-cli? Yes

Operating system: Arch Linux

To Reproduce

$ touch bar foo
$ trash-put bar f#Here ZSH autocompletion does not complete with "foo" file
$ trash-put bar foo #But the command works!
$ trash-list
2024-03-28 09:57:28 /home/quero/bar
2024-03-28 09:57:28 /home/quero/foo

Expected behavior The completions should have completed the command the "foo" filename.

DMaroo commented 6 months ago

Patch

The following patch, when applied to the generated completions, fixes the issue.

23c23
<   "(*)::files:_trash_files"
---
>   "*:: :->file"
28c28
<   local context state line curcontext="$curcontext" one_or_more='(-)*' remainder='(*)'
---
>   local context state line curcontext="$curcontext"
30,33c30
<   if ((${_shtab_trash_put_options[(I)${(q)one_or_more}*]} + ${_shtab_trash_put_options[(I)${(q)remainder}*]} == 0)); then  # noqa: E501
<     _shtab_trash_put_options+=(': :_shtab_trash_put_commands' '*::: :->trash-put')
<   fi
<   _arguments -C -s $_shtab_trash_put_options
---
>   _arguments -C -s -S -A -* $_shtab_trash_put_options
42a40,45
>       ;;
>     file)
>       (( CURRENT > 0 )) && line[CURRENT]=()
>       line=( ${line//(#m)[\[\]()\\*?#<>~\^\|]/\\$MATCH} )
>       _files -F line && ret=0
>       ;;

Of course, this is a hacky fix and it should not be upstreamed. I believe that either there is something wrong in the way shtab is used to generate completions, or shtab has a bug (unlikely).

Behavior

When using -<TAB> without files as any previous arguments, all the flags get auto-completed.

$ trash -<TAB>
--force             -f                       -- silently ignore nonexistent files
--help                                       -- show this help message and exit
--interactive       -i                       -- prompt before every removal
--print-completion                           -- print shell completion script
--recursive         --directory  -R  -r  -d  -- ignored (for GNU rm compatibility)
--trash-dir                                  -- use TRASHDIR as trash folder
--verbose           -v                       -- explain what is being done
--version                                    -- show program's version number and exit

When using just <TAB> (with no - in front of it) with or without files as any previous arguments, the files get autocompleted. If you have a file as one of the previous arguments, and then you use -<TAB>, it will try autocompleting files and will not autocomplete flags.

This behavior is completely identical to rm behavior.