Fast file completer class.
| 71 | |
| 72 | |
| 73 | class FastFilesCompleter: |
| 74 | """Fast file completer class.""" |
| 75 | |
| 76 | def __init__(self, directories: bool = True) -> None: |
| 77 | self.directories = directories |
| 78 | |
| 79 | def __call__(self, prefix: str, **kwargs: Any) -> List[str]: |
| 80 | # Only called on non option completions. |
| 81 | if os.path.sep in prefix[1:]: |
| 82 | prefix_dir = len(os.path.dirname(prefix) + os.path.sep) |
| 83 | else: |
| 84 | prefix_dir = 0 |
| 85 | completion = [] |
| 86 | globbed = [] |
| 87 | if "*" not in prefix and "?" not in prefix: |
| 88 | # We are on unix, otherwise no bash. |
| 89 | if not prefix or prefix[-1] == os.path.sep: |
| 90 | globbed.extend(glob(prefix + ".*")) |
| 91 | prefix += "*" |
| 92 | globbed.extend(glob(prefix)) |
| 93 | for x in sorted(globbed): |
| 94 | if os.path.isdir(x): |
| 95 | x += "/" |
| 96 | # Append stripping the prefix (like bash, not like compgen). |
| 97 | completion.append(x[prefix_dir:]) |
| 98 | return completion |
| 99 | |
| 100 | |
| 101 | if os.environ.get("_ARGCOMPLETE"): |
no outgoing calls