summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2013-02-13 04:04:05 +0100
committerhut <hut@lavabit.com>2013-02-13 04:04:05 +0100
commitf24817eece19aafba5dd7399c2dd3c212dacf29b (patch)
tree74e353754ae1117b299ad9e5419bbba4685e7597
parent8a516820081a317b85c6c2ced7901a4000130bf5 (diff)
downloadranger-f24817eece19aafba5dd7399c2dd3c212dacf29b.tar.gz
config/commands: added :scout command for faster ":find"ing
as requested by Miodrag Milic
-rw-r--r--ranger/config/commands.py59
1 files changed, 59 insertions, 0 deletions
diff --git a/ranger/config/commands.py b/ranger/config/commands.py
index f945ccf2..9d0f55a9 100644
--- a/ranger/config/commands.py
+++ b/ranger/config/commands.py
@@ -1058,6 +1058,65 @@ class pmap(map_):
     context = 'pager'
 
 
+class scout(Command):
+    def quick(self, cycle=False):
+        count = 0
+        cwd = self.fm.thisdir
+        arg = self.rest(1)
+        emptyline = "%s " % self.__class__.__name__
+
+        if not arg:
+            return False
+
+        if arg == '.':
+            self.fm.thistab.enter_dir("..")
+            self.updated_line = emptyline
+            return False
+
+        # build regular expression
+        regex = "%s"
+        if arg.endswith("$"):
+            arg = arg[:-1]
+            regex += "$"
+        if arg.startswith("^"):
+            arg = arg[1:]
+            regex = "^" + regex
+
+        case_insensitive = arg.lower() == arg
+        regex = re.compile(regex % ".*".join(arg), case_insensitive and re.I)
+
+        def check(name):
+            return re.search(regex, name)
+
+        # build deque of files and cycle through them
+        deq = deque(cwd.files)
+        deq.rotate(-cwd.pointer)
+        i = 0
+        if cycle:
+            # make another step forward when TAB was pressed
+            deq.rotate(-1)
+            i += 1
+        moved = False
+        for fsobj in deq:
+            if check(fsobj.basename):
+                count += 1
+                if not moved:
+                    cwd.move(to=(cwd.pointer + i) % len(cwd.files))
+                    moved = True
+            i += 1
+
+        if count == 1:
+            if self.fm.thisfile.is_directory:
+                self.fm.move(right=1)
+                self.updated_line = emptyline
+            else:
+                return True
+        return False
+
+    def tab(self):
+        self.quick(cycle=True)
+
+
 class filter(Command):
     """
     :filter <string>