about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorWojciech Siewierski <wojciech.siewierski@onet.pl>2018-09-08 23:33:01 +0200
committerWojciech Siewierski <wojciech.siewierski@onet.pl>2018-09-08 23:33:01 +0200
commit2485dd705e84fb64e3f04f5b4f6956968a5b9708 (patch)
tree0a78dbf70d4a33c8b32e406ba28d485b892e41f1
parent4a931634cbbe497796c195be3e3e214cd7f0f7c4 (diff)
downloadranger-2485dd705e84fb64e3f04f5b4f6956968a5b9708.tar.gz
New filter type for matching mimetypes
-rw-r--r--doc/ranger.pod4
-rw-r--r--ranger/config/rc.conf1
-rw-r--r--ranger/core/filter_stack.py17
3 files changed, 22 insertions, 0 deletions
diff --git a/doc/ranger.pod b/doc/ranger.pod
index 4fac5d50..772107e0 100644
--- a/doc/ranger.pod
+++ b/doc/ranger.pod
@@ -628,6 +628,10 @@ the current directory.  For a more permanent solution, use the command
 
 Apply a new filename filter.
 
+=item .m
+
+Apply a new mimetype filter.
+
 =item .d
 
 Apply the typefilter "directory".
diff --git a/ranger/config/rc.conf b/ranger/config/rc.conf
index 4da1ffc0..a32ab494 100644
--- a/ranger/config/rc.conf
+++ b/ranger/config/rc.conf
@@ -566,6 +566,7 @@ copymap zf zz
 
 # Filter stack
 map .n console filter_stack add name%space
+map .m console filter_stack add mime%space
 map .d filter_stack add type d
 map .f filter_stack add type f
 map .l filter_stack add type l
diff --git a/ranger/core/filter_stack.py b/ranger/core/filter_stack.py
index ff9f4080..2ca2b1c5 100644
--- a/ranger/core/filter_stack.py
+++ b/ranger/core/filter_stack.py
@@ -6,6 +6,7 @@
 from __future__ import (absolute_import, division, print_function)
 
 import re
+import mimetypes
 
 from ranger.container.directory import accept_file, InodeFilterConstants
 
@@ -48,6 +49,22 @@ class NameFilter(BaseFilter):
         return "<Filter: name =~ /{}/>".format(self.pattern)
 
 
+@stack_filter("mime")
+class MimeFilter(BaseFilter):
+    def __init__(self, pattern):
+        self.pattern = pattern
+        self.regex = re.compile(pattern)
+
+    def __call__(self, fobj):
+        mimetype, _ = mimetypes.guess_type(fobj.relative_path)
+        if mimetype is None:
+            return False
+        return self.regex.search(mimetype)
+
+    def __str__(self):
+        return "<Filter: mimetype =~ /{}/>".format(self.pattern)
+
+
 @stack_filter("type")
 class TypeFilter(BaseFilter):
     type_to_function = {