trash-cli icon indicating copy to clipboard operation
trash-cli copied to clipboard

ZSH completion does not allow to specify multiple files to trash

Open ohquero opened this issue 1 year ago • 7 comments

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.

ohquero avatar Mar 28 '24 09:03 ohquero

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.

DMaroo avatar Apr 08 '24 11:04 DMaroo

@DMaroo just wondering why this shouldn't be upstreamed? Just curious as to whether users should patch this downstream.

protist avatar Oct 31 '24 03:10 protist

any update on this ?

DrummyFloyd avatar Jan 24 '25 23:01 DrummyFloyd

@DMaroo just wondering why this shouldn't be upstreamed? Just curious as to whether users should patch this downstream.

Ideally, shtab should have generated that code, but it did not. Patching the auto-generated file is not the right way to go IMO.

DMaroo avatar Jan 25 '25 18:01 DMaroo

Thanks @DMaroo . I had a look in my system, and I can't find the file to patch. Could you please explain what we should be patching?

protist avatar Jan 27 '25 01:01 protist

@protist it's one of the directories where ZSH stores completions. You may have to look it up for the OS you're running.

I run Arch, and the completions for trash are stored in /usr/share/zsh/site-functions. In there, the completion for the trash command is named _trash, so I'm applying said patch to this file. Again with my OS, my command to patch the file is patch _trash <patch file name>.

I had to update the patch for it to work on my system. Here's my gist in case it also helps you.

dnswrsrx avatar Jan 27 '25 07:01 dnswrsrx

Thanks @dnswrsrx. That works great on my system (also Arch). I'm considering opening a task on the Arch tracker to patch it there, since there hasn't been much movement here.

protist avatar Jan 28 '25 07:01 protist