Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions examples/prompts/auto-completion/autocomplete-file-path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from prompt_toolkit import prompt
from prompt_toolkit.completion import PathCompleter


def main():
text = prompt(
"shell: ",
completer=PathCompleter(use_word=True),
complete_while_typing=False,
)
print("You said: %s" % text)


if __name__ == "__main__":
main()
19 changes: 17 additions & 2 deletions prompt_toolkit/completion/filesystem.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
from typing import Callable, Iterable, List, Optional

from prompt_toolkit.completion import CompleteEvent, Completer, Completion
Expand All @@ -9,6 +10,8 @@
"ExecutableCompleter",
]

_FILE_PATH_RE = re.compile(r"[^\s\"'\t]+")


class PathCompleter(Completer):
"""
Expand All @@ -20,6 +23,7 @@ class PathCompleter(Completer):
this file should show up in the completion. ``None``
when no filtering has to be done.
:param min_input_len: Don't do autocompletion when the input string is shorter.
:param use_word: Search up to the last white space instead of the whole input.
"""

def __init__(
Expand All @@ -29,18 +33,25 @@ def __init__(
file_filter: Optional[Callable[[str], bool]] = None,
min_input_len: int = 0,
expanduser: bool = False,
use_word: bool = False,
) -> None:

self.only_directories = only_directories
self.get_paths = get_paths or (lambda: ["."])
self.file_filter = file_filter or (lambda _: True)
self.min_input_len = min_input_len
self.expanduser = expanduser
self.use_word = use_word

def get_completions(
self, document: Document, complete_event: CompleteEvent
) -> Iterable[Completion]:
text = document.text_before_cursor

text = (
document.text_before_cursor
if not self.use_word
else document.get_word_before_cursor(pattern=_FILE_PATH_RE)
)

# Complete only when we have at least the minimal input length,
# otherwise, we can too many results and autocompletion will become too
Expand Down Expand Up @@ -93,7 +104,11 @@ def get_completions(
if not self.file_filter(full_name):
continue

yield Completion(completion, 0, display=filename)
yield Completion(
completion,
0,
display=filename,
)
except OSError:
pass

Expand Down