summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorMilan Svoboda <msvoboda@ra.rockwell.com>2014-11-25 21:43:18 +0100
committerMilan Svoboda <msvoboda@ra.rockwell.com>2014-11-25 21:43:18 +0100
commitf9b4e8c0a31d38fb48ee0e61e17a9534f00fdd82 (patch)
tree1b0b5c6f86efdae9e0316458067ff93d84d8e4e1 /ranger
parent5c97716aa0e260ec2b0cd6cffee1ea74fd9ff8f8 (diff)
downloadranger-f9b4e8c0a31d38fb48ee0e61e17a9534f00fdd82.tar.gz
implement filter_inode_type
Diffstat (limited to 'ranger')
-rw-r--r--ranger/config/commands.py26
-rw-r--r--ranger/container/directory.py26
2 files changed, 43 insertions, 9 deletions
diff --git a/ranger/config/commands.py b/ranger/config/commands.py
index 953e2cc3..6e641219 100644
--- a/ranger/config/commands.py
+++ b/ranger/config/commands.py
@@ -1281,3 +1281,29 @@ class flat(Command):
         self.fm.thisdir.flat = level
         self.fm.thisdir.load_content()
 
+
+class filter_inode_type(Command):
+    """
+    :filter_inode_type [dfl]
+
+    Displays only the files of specified inode type. Parameters
+    can be combined.
+
+        d display directories
+        f display files
+        l display links
+    """
+
+    FILTER_DIRS  = 'd'
+    FILTER_FILES = 'f'
+    FILTER_LINKS = 'l'
+
+    def execute(self):
+        if not self.arg(1):
+            self.fm.thisdir.inode_type_filter = None
+        else:
+            self.fm.thisdir.inode_type_filter = lambda file: (
+                    True if ((self.FILTER_DIRS  in self.arg(1) and file.is_directory) or
+                             (self.FILTER_FILES in self.arg(1) and file.is_file and not file.is_link) or
+                             (self.FILTER_LINKS in self.arg(1) and file.is_link)) else False)
+        self.fm.thisdir.refilter()
diff --git a/ranger/container/directory.py b/ranger/container/directory.py
index c6987a76..17cc90b7 100644
--- a/ranger/container/directory.py
+++ b/ranger/container/directory.py
@@ -36,13 +36,18 @@ def sort_naturally(path):
 def sort_naturally_icase(path):
     return path.basename_natural_lower
 
-def accept_file(fname, directory, hidden_filter, name_filter):
-    if hidden_filter and hidden_filter.search(fname):
-        return False
-    if name_filter and not name_filter.search(fname):
-        return False
-    if directory.temporary_filter and not directory.temporary_filter.search(fname):
-        return False
+def accept_file(file, filters):
+    """
+    Returns True if file shall be shown, otherwise False.
+    Parameters:
+        file - an instance of FileSystemObject
+        filters - an array of lambdas, each expects a file and
+                  returns True if file shall be shown,
+                  otherwise False.
+    """
+    for filter in filters:
+        if filter and not filter(file):
+            return False
     return True
 
 class Directory(FileSystemObject, Accumulator, Loadable):
@@ -59,6 +64,7 @@ class Directory(FileSystemObject, Accumulator, Loadable):
     files_all = None
     filter = None
     temporary_filter = None
+    inode_type_filter = None
     marked_items = None
     scroll_begin = 0
 
@@ -178,8 +184,10 @@ class Directory(FileSystemObject, Accumulator, Loadable):
         else:
             hidden_filter = None
 
-        self.files = [f for f in self.files_all if accept_file(
-            f.basename, self, hidden_filter, self.filter)]
+        filters = [(lambda file: not hidden_filter.search(file.basename)) if hidden_filter else None,
+                   (lambda file: self.filter.search(file.basename)) if self.filter else None,
+                   self.inode_type_filter]
+        self.files = [f for f in self.files_all if accept_file(f, filters)]
         self.move_to_obj(self.pointed_obj)
 
     # XXX: Check for possible race conditions