Created
February 16, 2013 22:33
-
-
Save anonymous/4969023 to your computer and use it in GitHub Desktop.
An alternative to Regex for Ranger's :travel and :narrow commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Matcher(str): | |
def search(self, other): | |
if not self: | |
return True | |
if self.startswith("^"): | |
try: | |
if self[1] == other[0]: | |
if Matcher.search(self[2:], other[1:]): | |
return True | |
except IndexError: | |
return True | |
if self.endswith("$"): | |
try: | |
if self[-2] == other[-1]: | |
if Matcher.search(self[:-2], other[:-1]): | |
return True | |
except IndexError: | |
return True | |
try: | |
iother = iter(other) | |
for letter in self: | |
# Scan across iother for letter | |
while True: | |
# if lowercase, don't be case sensitive | |
if letter.islower(): | |
if letter == next(iother).lower(): | |
break | |
else: | |
if letter == next(iother): | |
break | |
# tmp_filter has ended before iother, so is contained sequentially in iother | |
return True | |
# iother has ended before tmp_filter, so doesn't contain all of tmp_filter sequentially | |
except StopIteration: | |
return False | |
class narrow(Command): | |
""" | |
:narrow <string> | |
Displays only the files which contain <string> in their basename. | |
Unlike :filter, this command executes the selection and removes the filter | |
again when run. | |
""" | |
def execute(self): | |
self.cancel() # Clean up | |
if self.rest(1) == "..": | |
self.fm.move(left=1) | |
else: | |
self.fm.move(right=1) | |
def cancel(self): | |
self.fm.thisdir.temporary_filter = None | |
self.fm.thisdir.load_content(schedule=False) | |
def quick(self): | |
self.fm.thisdir.temporary_filter = Matcher(self.rest(1)) | |
self.fm.thisdir.load_content(schedule=False) | |
def tab(self): | |
if self.fm.env.cwd.files[-1] is not self.fm.env.cf: | |
self.fm.move(down=1) | |
else: | |
# We're at the bottom, so wrap | |
self.fm.move(to=0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment