andreafrancia / trash-cli

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

Add shell completions by shtab #248

Closed Freed-Wu closed 1 year ago

Freed-Wu commented 1 year ago

shtab only support argparse, so trash-put use a manual written zsh script trash-rm use default completion

Install

sudo cp scripts/_trash /usr/share/zsh/site-functions
cmds=(empty list restore)
for cmd in $cmds; do
  trash-$cmd --print-completion bash | sudo tee /usr/share/bash-completion/completions/trash-$cmd
  trash-$cmd --print-completion zsh | sudo tee /usr/share/zsh/site-functions/_trash-$cmd
  trash-$cmd --print-completion tcsh | sudo tee /etc/profile.d/trash-$cmd.completion.csh
done

Result

❯ ./trash-list -<TAB>
option
--all-users          list trashcans of all the users
--help               show this help message and exit
-h                   show this help message and exit
--print-completion   print shell completion script
--trash-dirs         list trash dirs
...
❯ ./trash-list --trash-dir <TAB>
trash_dirs
/home/wzy/.local/share/Trash  /mnt/.Trash-1000

It is better than https://github.com/zsh-users/zsh-completions/blob/master/src/_trash (_trash_*), because the latter is out of date (cmd options).

PS: since now, shtab cannot handle nargs="+". So change it to:

#compdef trash-put

# AUTOMATCALLY GENERATED by `shtab`

_shtab_trash_put_commands() {
  local _commands=(

  )
  _describe 'trash-put commands' _commands
}

_shtab_trash_put_options=(
  "(- : *)"{-h,--help}"[show this help message and exit]"
  "(- : *)--print-completion[print shell completion script]:print_completion:(bash zsh tcsh)"
  {-d,--directory}"[ignored (for GNU rm compatibility)]"
  {-f,--force}"[silently ignore nonexistent files]"
  {-i,--interactive}"[prompt before every removal]"
  {-r,-R,--recursive}"[ignored (for GNU rm compatibility)]"
  "--trash-dir[use TRASHDIR as trash folder]:trashdir:_trash_dirs"
  "*"{-v,--verbose}"[explain what is being done]"
  "(- : *)--version[show program\'s version number and exit]"
  "(-)*::files:_trash_files"
)

_shtab_trash_put() {
  local context state line curcontext="$curcontext" one_or_more='(-)*' reminder='(*)'

  if ((${_shtab_trash_put_options[(I)${(q)one_or_more}*]} + ${_shtab_trash_put_options[(I)${(q)reminder}*]} == 0)); then  # noqa: E501
    _shtab_trash_put_options+=(': :_shtab_trash_put_commands' '*::: :->trash-put')
  fi
  _arguments -C $_shtab_trash_put_options

  case $state in
    trash-put)
      words=($line[1] "${words[@]}")
      (( CURRENT += 1 ))
      curcontext="${curcontext%:*:*}:_shtab_trash_put-$line[1]:"
      case $line[1] in

      esac
  esac
}

# Custom Preamble

# https://github.com/zsh-users/zsh/blob/19390a1ba8dc983b0a1379058e90cd51ce156815/Completion/Unix/Command/_rm#L70-L76
_trash_files() {
  (( CURRENT > 0 )) && line[CURRENT]=()
  line=( ${line//(#m)[\[\]()\\*?#<>~\^\|]/\\$MATCH} )
  _files -F line
}

_trash_dirs() {
  local trash_dir choices=()
  for trash_dir in "$(trash-list --trash-dirs)"; do
    if [[ "$trash_dir" != parent_* ]]; then
      choices+=("$trash_dir")
    fi
  done
  _arguments "*:trash_dirs:($choices)"
}

# End Custom Preamble

typeset -A opt_args
_shtab_trash_put "$@"

_trash is same as _trash-put.

andreafrancia commented 1 year ago

Your work is very interesting, and it would be a nice addition to trash-cli. This pull request is too big for me to review it and to be sure it does not change anything that should stay unchanged. Please send me first the part that move trash-put optparse to argparse, so I can review it more easily.

Freed-Wu commented 1 year ago

This PR has rebased to optparse2argparse.

andreafrancia commented 1 year ago

Thank for you contribution about the argparse. When you have time please resolve conflicts and update this pull requests.

Freed-Wu commented 1 year ago

Now, it is OK.

❯ trash -<TAB>
file
option
-d                   ignored (for GNU rm compatibility)
--directory          ignored (for GNU rm compatibility)
--force              silently ignore nonexistent files
...
❯ trash --trash-dir <TAB>
trash_dirs
/home/wzy/.local/share/Trash  /mnt/.Trash-1000              /tmp/.Trash-1000
❯ trash --trash-dir /home/wzy/.local/share/Trash <TAB>
file
bugs.txt              dist/                 man/                  README.rst            scripts/              TODO.txt              trash-empty*          trash-rm*
conftest.py           docs/                 ptt_files/            README_zh-CN.rst      setup.cfg             trash*                trash-list*           Vagrantfile
COPYING               DONE.txt              __pycache__/          requirements-dev.txt  setup.py              trashcli/             trash-put*
CREDITS.txt           HISTORY.txt           pyproject.toml        requirements.txt      tests/                trash_cli.egg-info/   trash-restore*
❯ trash --trash-dir /home/wzy/.local/share/Trash bugs.txt <TAB>
file
conftest.py           docs/                 ptt_files/            README_zh-CN.rst      setup.cfg             trash*                trash-list*           Vagrantfile
COPYING               DONE.txt              __pycache__/          requirements-dev.txt  setup.py              trashcli/             trash-put*
CREDITS.txt           HISTORY.txt           pyproject.toml        requirements.txt      tests/                trash_cli.egg-info/   trash-restore*
dist/                 man/                  README.rst            scripts/              TODO.txt              trash-empty*          trash-rm*
# Note bugs.txt has been removed from list.
Freed-Wu commented 1 year ago

What happened?

   /usr/local/bin/git -c protocol.version=2 fetch --no-tags --prune --progress --no-recurse-submodules --depth=1 origin +2bc7c83969bf7acab5de357db9c64339171ee1e0:refs/remotes/pull/248/merge
  Error: fatal: unable to access 'https://github.com/andreafrancia/trash-cli/': The requested URL returned error: 429
andreafrancia commented 1 year ago

What happened?

   /usr/local/bin/git -c protocol.version=2 fetch --no-tags --prune --progress --no-recurse-submodules --depth=1 origin +2bc7c83969bf7acab5de357db9c64339171ee1e0:refs/remotes/pull/248/merge
  Error: fatal: unable to access 'https://github.com/andreafrancia/trash-cli/': The requested URL returned error: 429

I don't know. According to mdn 429 is the status code for "Too Many Requests", maybe it is something transient.

andreafrancia commented 1 year ago

I've reran the Actions. Let see if this time it does got a 429.

Freed-Wu commented 1 year ago

OK, have finished.

andreafrancia commented 1 year ago

Merged! Thank you.