File: shell_completion.py

package info (click to toggle)
trash-cli 0.24.5.26-0.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,972 kB
  • sloc: python: 9,789; sh: 121; makefile: 11
file content (65 lines) | stat: -rw-r--r-- 1,775 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import argparse
from copy import copy

from typing import Dict

try:
    def convert_to_list(tuple):
        return [item for item in tuple]


    from shtab import add_argument_to, FILE, DIR  # type: ignore

    defaults = convert_to_list(add_argument_to.__defaults__)
    defaults[-1] = {
        "zsh": r"""
# https://github.com/zsh-users/zsh/blob/19390a1ba8dc983b0a1379058e90cd51ce156815/Completion/Unix/Command/_rm#L72-L74
_trash_files() {
  (( CURRENT > 0 )) && line[CURRENT]=()
  line=( ${line//(#m)[\[\]()\\*?#<>~\^\|]/\\$MATCH} )
  _files -F line
}
""",
    }
    add_argument_to.__defaults__ = tuple(defaults)
    TRASH_FILES = copy(FILE)
    TRASH_DIRS = copy(DIR)


    def complete_with(completion,  # type: Dict[str, str]
                      action,  # type: argparse.Action
                      ):
        action.complete = completion  # type: ignore



except ImportError:
    from argparse import Action

    TRASH_FILES = TRASH_DIRS = {}


    class PrintCompletionAction(Action):
        def __call__(self, parser, namespace, values, option_string=None):
            print('Please install shtab firstly!')
            parser.exit(0)


    def add_argument_to(parser, *args, **kwargs):  # type: ignore
        Action.complete = None  # type: ignore
        parser.add_argument(
            '--print-completion',
            choices=['bash', 'zsh', 'tcsh'],
            action=PrintCompletionAction,
            help='print shell completion script',
        )
        return parser


    def complete_with(completion,  # type: Dict[str, str]
                      action,  # type: argparse.Action
                      ):
        pass

TRASH_FILES.update({"zsh": "_trash_files"})
TRASH_DIRS.update({"zsh": "(\\${\\$(trash-list --trash-dirs)#parent_*})"})