Skip to content

Instantly share code, notes, and snippets.

Created February 16, 2013 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4969023 to your computer and use it in GitHub Desktop.
Save anonymous/4969023 to your computer and use it in GitHub Desktop.
An alternative to Regex for Ranger's :travel and :narrow commands
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