From 2485dd705e84fb64e3f04f5b4f6956968a5b9708 Mon Sep 17 00:00:00 2001 From: Wojciech Siewierski Date: Sat, 8 Sep 2018 23:33:01 +0200 Subject: New filter type for matching mimetypes --- doc/ranger.pod | 4 ++++ ranger/config/rc.conf | 1 + ranger/core/filter_stack.py | 17 +++++++++++++++++ 3 files changed, 22 insertions(+) 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 "".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 "".format(self.pattern) + + @stack_filter("type") class TypeFilter(BaseFilter): type_to_function = { -- cgit 1.4.1-2-gfad0