-
Notifications
You must be signed in to change notification settings - Fork 1
/
command.py
59 lines (46 loc) · 1.61 KB
/
command.py
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
import ranger.api.commands
from .filter import FzfFilter
# noinspection PyUnreachableCode
# This is done to enhance auto-completion and inference in the editor.
if False:
import ranger.core.fm
KEY_FZF_FILTER = 'fzf_filter'
# noinspection PyPep8Naming,PyUnresolvedReferences
class fzf_filter(ranger.api.commands.Command):
"""
:fzf_filter <query>
This command allows you to use fzf fuzzy search to filter files and directories in the ranger.
"""
def execute(self):
fm = self.fm # type: ranger.core.fm.FM
# Check if a filter is already set
_filter = fm.thisdir.__dict__.get(KEY_FZF_FILTER, None)
if isinstance(_filter, FzfFilter):
# If a filter is set, just update the query
_filter.set_query(self._get_query())
else:
# If no filter is set, build a new one
fm.thisdir.__dict__[KEY_FZF_FILTER] = self._build_filter()
fm.thisdir.refilter()
if self.quickly_executed:
fm.open_console(self.line)
def cancel(self):
fm = self.fm # type: ranger.core.fm.FM
fm.thisdir.__dict__[KEY_FZF_FILTER] = None
fm.thisdir.refilter()
def quick(self):
return True
def _get_query(self):
"""
Get the search query.
Returns:
str: The search query.
"""
return self.rest(1)
def _build_filter(self):
"""
Build a new FzfFilter.
Returns:
FzfFilter: A new FzfFilter object with the current directory and search query.
"""
return FzfFilter(self.fm.thisdir, self._get_query())