Skip to content

Instantly share code, notes, and snippets.

@PotatoesMaster
Created January 29, 2015 18:43
Show Gist options
  • Save PotatoesMaster/6537f565265e03495ed7 to your computer and use it in GitHub Desktop.
Save PotatoesMaster/6537f565265e03495ed7 to your computer and use it in GitHub Desktop.
Cd command hack for case-insensitive completion in ranger 1.6.1
from ranger.config.commands import cd
class cd(cd):
def tab(self):
import os
from os.path import dirname, basename, expanduser, join
cwd = self.fm.thisdir.path
rel_dest = self.rest(1)
bookmarks = [v.path for v in self.fm.bookmarks.dct.values()
if rel_dest in v.path ]
# expand the tilde into the user directory
if rel_dest.startswith('~'):
rel_dest = expanduser(rel_dest)
# define some shortcuts
abs_dest = join(cwd, rel_dest)
abs_dirname = dirname(abs_dest)
rel_basename = basename(rel_dest)
rel_dirname = dirname(rel_dest)
try:
# are we at the end of a directory?
if rel_dest.endswith('/') or rel_dest == '':
_, dirnames, _ = next(os.walk(abs_dest))
# are we in the middle of the filename?
else:
# hackish option: remove the 5 hashes below to keep case-insensitive completion
# as a fallback (use it only when no other completions give results)
# Keep the spaces. :)
#
# |
# v
#_, dirnames, _ = next(os.walk(abs_dirname))
#dirnames = [dn for dn in dirnames \
# if dn.startswith(rel_basename)]
## nothing found, try case insensitive matching
#if len(dirnames) == 0:
_, dirnames, _ = next(os.walk(abs_dirname))
lower_rel_basename = rel_basename.lower()
dirnames = [dn for dn in dirnames \
if dn.lower().startswith(lower_rel_basename)]
except (OSError, StopIteration):
# os.walk found nothing
pass
else:
dirnames.sort()
dirnames = bookmarks + dirnames
# no results, return None
if len(dirnames) == 0:
return
# one result. since it must be a directory, append a slash.
if len(dirnames) == 1:
return self.start(1) + join(rel_dirname, dirnames[0]) + '/'
# more than one result. append no slash, so the user can
# manually type in the slash to advance into that directory
return (self.start(1) + join(rel_dirname, dirname) for dirname in dirnames)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment