Skip to content

Commit

Permalink
fix(filepath): windows completion #32
Browse files Browse the repository at this point in the history
  • Loading branch information
kazhala committed Feb 2, 2022
1 parent 1adb55b commit 473f22c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
11 changes: 6 additions & 5 deletions InquirerPy/prompts/filepath.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class FilePathCompleter(Completer):
def __init__(self, only_directories: bool = False, only_files: bool = False):
self._only_directories = only_directories
self._only_files = only_files
self._delimiter = "/" if os.name == "posix" else "\\"

def get_completions(
self, document, complete_event
Expand All @@ -51,11 +52,11 @@ def get_completions(
dirname = Path.cwd()
validation = lambda file, doc_text: True
elif document.text.startswith("~"):
dirname = Path(os.path.dirname("%s%s" % (Path.home(), document.text[1:])))
dirname = Path(os.path.dirname(f"{Path.home()}{document.text[1:]}"))
validation = lambda file, doc_text: str(file).startswith(
"%s%s" % (Path.home(), doc_text[1:])
f"{Path.home()}{doc_text[1:]}"
)
elif document.text.startswith("./"):
elif document.text.startswith(f".{self._delimiter}"):
dirname = Path(os.path.dirname(document.text))
validation = lambda file, doc_text: str(file).startswith(doc_text[2:])
else:
Expand All @@ -78,9 +79,9 @@ def _get_completion(
file_name = file.name
display_name = file_name
if file.is_dir():
display_name = "%s/" % file_name
display_name = f"{file_name}{self._delimiter}"
yield Completion(
"%s" % file.name,
file.name,
start_position=-1 * len(os.path.basename(document.text)),
display=display_name,
)
Expand Down
5 changes: 4 additions & 1 deletion examples/alternate/filepath.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import os

from InquirerPy import inquirer
from InquirerPy.validator import PathValidator


def main():
home_path = "~/" if os.name == "posix" else "C:\\"
src_path = inquirer.filepath(
message="Enter file to upload:",
default="~/",
default=home_path,
validate=PathValidator(is_file=True, message="Input is not a file"),
only_files=True,
).execute()
Expand Down
5 changes: 4 additions & 1 deletion examples/classic/filepath.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import os

from InquirerPy import prompt
from InquirerPy.validator import PathValidator


def main():
home_path = "~/" if os.name == "posix" else "C:\\"
questions = [
{
"type": "filepath",
"message": "Enter file to upload:",
"name": "location",
"default": "~/",
"default": home_path,
"validate": PathValidator(is_file=True, message="Input is not a file"),
"only_files": True,
},
Expand Down
29 changes: 29 additions & 0 deletions tests/prompts/test_filepath.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,32 @@ def _validation(_):
def test_invalid_argument(self):
self.assertRaises(InvalidArgument, FilePathPrompt, "hello", None, False, 12)
FilePathPrompt(message="hello", default=lambda _: "12")

@patch("platform.system")
def test_completer_explicit_currdir_all_win(self, mocked_platform):
with self.chdir(self.test_dir):
completer = FilePathCompleter()
doc_text = ".\\"
doc = Document(doc_text, len(doc_text))
event = CompleteEvent()
completions = [
completion.text
for completion in list(completer.get_completions(doc, event))
]
self.assertEqual(
sorted(completions),
sorted(self.dirs_to_create + self.files_to_create),
)

@patch("platform.system")
def test_completer_currdir_file_win(self, mocked_platform):
with self.chdir(self.test_dir):
completer = FilePathCompleter()
doc_text = ".\\file"
doc = Document(doc_text, len(doc_text))
event = CompleteEvent()
completions = [
completion.text
for completion in list(completer.get_completions(doc, event))
]
self.assertEqual(sorted(completions), ["file1", "file2", "file3"])

0 comments on commit 473f22c

Please sign in to comment.