diff options
author | Andrew MacFie <amacfie@sent.com> | 2021-01-19 13:39:16 -0500 |
---|---|---|
committer | Andrew MacFie <amacfie@sent.com> | 2021-01-19 13:39:16 -0500 |
commit | 62d627757f60c5a9d58a01a0fd472d8322190954 (patch) | |
tree | 4365e548f5d3856737681d655928cccccae2c972 /qutebrowser/completion/models/filepathcategory.py | |
parent | dd80110dd4f856e9db1bc5d683117eebdf7f5a04 (diff) | |
download | qutebrowser-62d627757f60c5a9d58a01a0fd472d8322190954.tar.gz qutebrowser-62d627757f60c5a9d58a01a0fd472d8322190954.zip |
Leave prefix as user typed it
Diffstat (limited to 'qutebrowser/completion/models/filepathcategory.py')
-rw-r--r-- | qutebrowser/completion/models/filepathcategory.py | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/qutebrowser/completion/models/filepathcategory.py b/qutebrowser/completion/models/filepathcategory.py index 98fc81a35..e2caae60f 100644 --- a/qutebrowser/completion/models/filepathcategory.py +++ b/qutebrowser/completion/models/filepathcategory.py @@ -19,6 +19,7 @@ import glob import os +from pathlib import Path from typing import List from PyQt5.QtCore import QAbstractListModel, QModelIndex, QObject, QUrl @@ -39,16 +40,31 @@ class FilePathCategory(QAbstractListModel): Args: val: The user's partially typed URL/path. """ + def _contractuser(path: str, head: str) -> str: + return str(head / Path(path).relative_to(Path(head).expanduser())) + if not val: - self._paths = [os.path.expanduser('~')] + # TODO: give list of favorite paths from config + self._paths = [] elif val.startswith('file:///'): - glob_str = QUrl(val).path() + '*' - self._paths = sorted(glob.glob(glob_str)) - elif os.path.isabs(os.path.expanduser(val)): - glob_str = glob.escape(os.path.expanduser(val)) + '*' - self._paths = sorted(glob.glob(glob_str)) + glob_str = QUrl(val).toLocalFile() + '*' + self._paths = sorted([QUrl.fromLocalFile(path).toString() + for path in glob.glob(glob_str)]) else: - self._paths = [] + expanded = os.path.expanduser(val) + if os.path.isabs(expanded): + glob_str = glob.escape(expanded) + '*' + expanded_paths = sorted(glob.glob(glob_str)) + + head = Path(val).parts[0] + # if ~ or ~user was expanded, contract it in _paths + if head.startswith('~'): + self._paths = [_contractuser(expanded_path, head) for + expanded_path in expanded_paths] + else: + self._paths = expanded_paths + else: + self._paths = [] def data(self, index: QModelIndex) -> str: """Implement abstract method in QAbstractListModel.""" @@ -57,7 +73,7 @@ class FilePathCategory(QAbstractListModel): else: return '' - def rowCount(self, parent: QModelIndex=QModelIndex()) -> int: + def rowCount(self, parent: QModelIndex = QModelIndex()) -> int: """Implement abstract method in QAbstractListModel.""" if parent.isValid(): return 0 |