mirror of
https://github.com/wting/autojump
synced 2025-06-13 12:54:07 +00:00
Merge 2a499232ed
into 1ab78ae91c
This commit is contained in:
commit
a75e075252
38
bin/autojump
38
bin/autojump
@ -43,7 +43,8 @@ TESTING = False
|
|||||||
if 'AUTOJUMP_DATA_DIR' in os.environ:
|
if 'AUTOJUMP_DATA_DIR' in os.environ:
|
||||||
CONFIG_DIR = os.environ.get('AUTOJUMP_DATA_DIR')
|
CONFIG_DIR = os.environ.get('AUTOJUMP_DATA_DIR')
|
||||||
else:
|
else:
|
||||||
xdg_data_dir = os.environ.get('XDG_DATA_HOME') or os.path.join(os.environ['HOME'], '.local', 'share')
|
xdg_data_dir = os.environ.get('XDG_DATA_HOME') or os.path.join(
|
||||||
|
os.environ['HOME'], '.local', 'share')
|
||||||
CONFIG_DIR = os.path.join(xdg_data_dir, 'autojump')
|
CONFIG_DIR = os.path.join(xdg_data_dir, 'autojump')
|
||||||
|
|
||||||
KEEP_ALL_ENTRIES = False
|
KEEP_ALL_ENTRIES = False
|
||||||
@ -63,6 +64,7 @@ if CONFIG_DIR == os.path.expanduser('~'):
|
|||||||
else:
|
else:
|
||||||
DB_FILE = CONFIG_DIR + '/autojump.txt'
|
DB_FILE = CONFIG_DIR + '/autojump.txt'
|
||||||
|
|
||||||
|
|
||||||
class Database:
|
class Database:
|
||||||
"""
|
"""
|
||||||
Object for interfacing with autojump database.
|
Object for interfacing with autojump database.
|
||||||
@ -84,7 +86,8 @@ class Database:
|
|||||||
self.data[path] = increment
|
self.data[path] = increment
|
||||||
else:
|
else:
|
||||||
import math
|
import math
|
||||||
self.data[path] = math.sqrt((self.data[path]**2)+(increment**2))
|
self.data[path] = math.sqrt((self.data[path] ** 2)
|
||||||
|
+ (increment ** 2))
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
def decay(self):
|
def decay(self):
|
||||||
@ -165,7 +168,8 @@ class Database:
|
|||||||
for path, weight in sorted(self.data.items(),
|
for path, weight in sorted(self.data.items(),
|
||||||
key=itemgetter(1),
|
key=itemgetter(1),
|
||||||
reverse=True):
|
reverse=True):
|
||||||
temp.write((unico("%s\t%s\n")%(weight, path)).encode("utf-8"))
|
temp.write(
|
||||||
|
(unico("%s\t%s\n") % (weight, path)).encode("utf-8"))
|
||||||
|
|
||||||
# catching disk errors and skipping save when file handle can't be closed.
|
# catching disk errors and skipping save when file handle can't be closed.
|
||||||
try:
|
try:
|
||||||
@ -205,7 +209,8 @@ def options():
|
|||||||
"""
|
"""
|
||||||
global ARGS
|
global ARGS
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description='Automatically jump to directory passed as an argument.',
|
parser = argparse.ArgumentParser(
|
||||||
|
description='Automatically jump to directory passed as an argument.',
|
||||||
epilog="Please see autojump(1) man pages for full documentation.")
|
epilog="Please see autojump(1) man pages for full documentation.")
|
||||||
parser.add_argument('directory', metavar='DIR', nargs='*', default='',
|
parser.add_argument('directory', metavar='DIR', nargs='*', default='',
|
||||||
help='directory to jump to')
|
help='directory to jump to')
|
||||||
@ -219,7 +224,8 @@ def options():
|
|||||||
help='delete all database entries that no longer exist on system')
|
help='delete all database entries that no longer exist on system')
|
||||||
parser.add_argument('-s', '--stat', action="store_true", default=False,
|
parser.add_argument('-s', '--stat', action="store_true", default=False,
|
||||||
help='show database entries and their key weights')
|
help='show database entries and their key weights')
|
||||||
parser.add_argument('--version', action="version", version="%(prog)s " + VERSION,
|
parser.add_argument(
|
||||||
|
'--version', action="version", version="%(prog)s " + VERSION,
|
||||||
help='show version information and exit')
|
help='show version information and exit')
|
||||||
|
|
||||||
ARGS = parser.parse_args()
|
ARGS = parser.parse_args()
|
||||||
@ -251,6 +257,7 @@ def options():
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def decode(text, encoding=None, errors="strict"):
|
def decode(text, encoding=None, errors="strict"):
|
||||||
"""
|
"""
|
||||||
Decoding step for Python 2 which does not default to unicode.
|
Decoding step for Python 2 which does not default to unicode.
|
||||||
@ -262,6 +269,7 @@ def decode(text, encoding=None, errors="strict"):
|
|||||||
encoding = sys.getfilesystemencoding()
|
encoding = sys.getfilesystemencoding()
|
||||||
return text.decode(encoding, errors)
|
return text.decode(encoding, errors)
|
||||||
|
|
||||||
|
|
||||||
def output(unicode_text, encoding=None):
|
def output(unicode_text, encoding=None):
|
||||||
"""
|
"""
|
||||||
Wrapper for the print function, using the filesystem encoding by default
|
Wrapper for the print function, using the filesystem encoding by default
|
||||||
@ -274,6 +282,7 @@ def output(unicode_text, encoding=None):
|
|||||||
encoding = sys.getfilesystemencoding()
|
encoding = sys.getfilesystemencoding()
|
||||||
print(unicode_text.encode(encoding))
|
print(unicode_text.encode(encoding))
|
||||||
|
|
||||||
|
|
||||||
def unico(text):
|
def unico(text):
|
||||||
"""
|
"""
|
||||||
If Python 2, convert to a unicode object.
|
If Python 2, convert to a unicode object.
|
||||||
@ -283,6 +292,7 @@ def unico(text):
|
|||||||
else:
|
else:
|
||||||
return unicode(text)
|
return unicode(text)
|
||||||
|
|
||||||
|
|
||||||
def match_last(pattern):
|
def match_last(pattern):
|
||||||
"""
|
"""
|
||||||
If the last pattern contains a full path, jump there.
|
If the last pattern contains a full path, jump there.
|
||||||
@ -298,6 +308,7 @@ def match_last(pattern):
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def match(path, pattern, only_end=False, ignore_case=False):
|
def match(path, pattern, only_end=False, ignore_case=False):
|
||||||
"""
|
"""
|
||||||
Check whether a path matches a particular pattern, and return
|
Check whether a path matches a particular pattern, and return
|
||||||
@ -319,6 +330,7 @@ def match(path, pattern, only_end=False, ignore_case=False):
|
|||||||
else:
|
else:
|
||||||
return (False, path[find_idx + len(pattern):])
|
return (False, path[find_idx + len(pattern):])
|
||||||
|
|
||||||
|
|
||||||
def find_matches(db, patterns, max_matches=1, ignore_case=False, fuzzy=False):
|
def find_matches(db, patterns, max_matches=1, ignore_case=False, fuzzy=False):
|
||||||
"""
|
"""
|
||||||
Find max_matches paths that match the pattern, and add them to the result_list.
|
Find max_matches paths that match the pattern, and add them to the result_list.
|
||||||
@ -370,7 +382,8 @@ def find_matches(db, patterns, max_matches=1, ignore_case=False, fuzzy=False):
|
|||||||
found, tmp = match(tmp, p, True, ignore_case)
|
found, tmp = match(tmp, p, True, ignore_case)
|
||||||
else:
|
else:
|
||||||
found, tmp = match(tmp, p, False, ignore_case)
|
found, tmp = match(tmp, p, False, ignore_case)
|
||||||
if not found: break
|
if not found:
|
||||||
|
break
|
||||||
|
|
||||||
if found and (os.path.exists(path) or TESTING):
|
if found and (os.path.exists(path) or TESTING):
|
||||||
if path not in results:
|
if path not in results:
|
||||||
@ -379,11 +392,13 @@ def find_matches(db, patterns, max_matches=1, ignore_case=False, fuzzy=False):
|
|||||||
break
|
break
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
def shell_utility():
|
def shell_utility():
|
||||||
"""
|
"""
|
||||||
Run this when autojump is called as a shell utility.
|
Run this when autojump is called as a shell utility.
|
||||||
"""
|
"""
|
||||||
if options(): return True
|
if options():
|
||||||
|
return True
|
||||||
db = Database(DB_FILE)
|
db = Database(DB_FILE)
|
||||||
|
|
||||||
# if no directories, add empty string
|
# if no directories, add empty string
|
||||||
@ -401,7 +416,8 @@ def shell_utility():
|
|||||||
tab_match = re.search(COMPLETION_SEPARATOR + "([0-9]+)", patterns[-1])
|
tab_match = re.search(COMPLETION_SEPARATOR + "([0-9]+)", patterns[-1])
|
||||||
if tab_match: # user has selected a tab completion entry
|
if tab_match: # user has selected a tab completion entry
|
||||||
tab_choice = int(tab_match.group(1))
|
tab_choice = int(tab_match.group(1))
|
||||||
patterns[-1] = re.sub(COMPLETION_SEPARATOR+"[0-9]+.*", "", patterns[-1])
|
patterns[-1] = re.sub(COMPLETION_SEPARATOR + "[0-9]+.*",
|
||||||
|
"", patterns[-1])
|
||||||
else: # user hasn't selected a tab completion, display choices again
|
else: # user hasn't selected a tab completion, display choices again
|
||||||
tab_match = re.match("(.*)" + COMPLETION_SEPARATOR, patterns[-1])
|
tab_match = re.match("(.*)" + COMPLETION_SEPARATOR, patterns[-1])
|
||||||
if tab_match:
|
if tab_match:
|
||||||
@ -426,7 +442,8 @@ def shell_utility():
|
|||||||
results = find_matches(db, patterns, max_matches, True, True)
|
results = find_matches(db, patterns, max_matches, True, True)
|
||||||
|
|
||||||
quotes = ""
|
quotes = ""
|
||||||
if ARGS.complete and ARGS.bash: quotes = "'"
|
if ARGS.complete and ARGS.bash:
|
||||||
|
quotes = "'"
|
||||||
|
|
||||||
if tab_choice != -1:
|
if tab_choice != -1:
|
||||||
if len(results) > tab_choice - 1:
|
if len(results) > tab_choice - 1:
|
||||||
@ -446,4 +463,5 @@ def shell_utility():
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
if not shell_utility(): sys.exit(1)
|
if not shell_utility():
|
||||||
|
sys.exit(1)
|
||||||
|
@ -14,16 +14,21 @@ import sys
|
|||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def no_stderr():
|
def no_stderr():
|
||||||
savestderr = sys.stderr
|
savestderr = sys.stderr
|
||||||
|
|
||||||
class DevNull(object):
|
class DevNull(object):
|
||||||
def write(self, _): pass
|
def write(self, _):
|
||||||
|
pass
|
||||||
sys.stderr = DevNull()
|
sys.stderr = DevNull()
|
||||||
yield
|
yield
|
||||||
sys.stderr = savestderr
|
sys.stderr = savestderr
|
||||||
|
|
||||||
# test suite
|
# test suite
|
||||||
|
|
||||||
|
|
||||||
class TestAutojump(unittest.TestCase):
|
class TestAutojump(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@ -79,7 +84,8 @@ class TestAutojump(unittest.TestCase):
|
|||||||
|
|
||||||
def test_db_load_backup(self):
|
def test_db_load_backup(self):
|
||||||
# setup
|
# setup
|
||||||
fname = '/tmp/autojump_test_db_load_backup_' + str(random.randint(0,32678))
|
fname = '/tmp/autojump_test_db_load_backup_' + str(
|
||||||
|
random.randint(0, 32678))
|
||||||
db = autojump.Database(fname)
|
db = autojump.Database(fname)
|
||||||
db.add('/1')
|
db.add('/1')
|
||||||
os.rename(fname, fname + '.bak')
|
os.rename(fname, fname + '.bak')
|
||||||
@ -102,7 +108,8 @@ class TestAutojump(unittest.TestCase):
|
|||||||
|
|
||||||
def test_db_save(self):
|
def test_db_save(self):
|
||||||
# setup
|
# setup
|
||||||
fname = '/tmp/autojump_test_db_save_' + str(random.randint(0,32678)) + '.txt'
|
fname = '/tmp/autojump_test_db_save_' + str(random.randint(
|
||||||
|
0, 32678)) + '.txt'
|
||||||
db = autojump.Database(fname)
|
db = autojump.Database(fname)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -182,8 +189,10 @@ class TestAutojump(unittest.TestCase):
|
|||||||
self.db.add('/9')
|
self.db.add('/9')
|
||||||
|
|
||||||
patterns = [u'']
|
patterns = [u'']
|
||||||
results = autojump.find_matches(self.db, patterns, max_matches, ignore_case)
|
results = autojump.find_matches(
|
||||||
self.assertEquals(results, ['/5', '/6', '/9', '/8', '/7', '/4', '/3', '/2', '/1'])
|
self.db, patterns, max_matches, ignore_case)
|
||||||
|
self.assertEquals(
|
||||||
|
results, ['/5', '/6', '/9', '/8', '/7', '/4', '/3', '/2', '/1'])
|
||||||
|
|
||||||
def test_match_case_insensitive(self):
|
def test_match_case_insensitive(self):
|
||||||
max_matches = 1
|
max_matches = 1
|
||||||
@ -192,7 +201,8 @@ class TestAutojump(unittest.TestCase):
|
|||||||
self.db.add('/foo', 10)
|
self.db.add('/foo', 10)
|
||||||
|
|
||||||
patterns = [u'fo']
|
patterns = [u'fo']
|
||||||
results = autojump.find_matches(self.db, patterns, max_matches, ignore_case)
|
results = autojump.find_matches(
|
||||||
|
self.db, patterns, max_matches, ignore_case)
|
||||||
self.assertEquals(results[0], '/FOO')
|
self.assertEquals(results[0], '/FOO')
|
||||||
|
|
||||||
def test_match_fuzzy(self):
|
def test_match_fuzzy(self):
|
||||||
@ -204,19 +214,23 @@ class TestAutojump(unittest.TestCase):
|
|||||||
self.db.add('/abcdefg', 10)
|
self.db.add('/abcdefg', 10)
|
||||||
|
|
||||||
patterns = [u'random']
|
patterns = [u'random']
|
||||||
results = autojump.find_matches(self.db, patterns, max_matches, ignore_case, fuzzy_search)
|
results = autojump.find_matches(
|
||||||
|
self.db, patterns, max_matches, ignore_case, fuzzy_search)
|
||||||
self.assertTrue(len(results) == 0)
|
self.assertTrue(len(results) == 0)
|
||||||
|
|
||||||
patterns = [u'abcdefg']
|
patterns = [u'abcdefg']
|
||||||
results = autojump.find_matches(self.db, patterns, max_matches, ignore_case, fuzzy_search)
|
results = autojump.find_matches(
|
||||||
|
self.db, patterns, max_matches, ignore_case, fuzzy_search)
|
||||||
self.assertEquals(results[0], '/abcdefg')
|
self.assertEquals(results[0], '/abcdefg')
|
||||||
|
|
||||||
patterns = [u'abcefg']
|
patterns = [u'abcefg']
|
||||||
results = autojump.find_matches(self.db, patterns, max_matches, ignore_case, fuzzy_search)
|
results = autojump.find_matches(
|
||||||
|
self.db, patterns, max_matches, ignore_case, fuzzy_search)
|
||||||
self.assertEquals(results[0], '/abcdefg')
|
self.assertEquals(results[0], '/abcdefg')
|
||||||
|
|
||||||
patterns = [u'bacef']
|
patterns = [u'bacef']
|
||||||
results = autojump.find_matches(self.db, patterns, max_matches, ignore_case, fuzzy_search)
|
results = autojump.find_matches(
|
||||||
|
self.db, patterns, max_matches, ignore_case, fuzzy_search)
|
||||||
self.assertEquals(results[0], '/abcdefg')
|
self.assertEquals(results[0], '/abcdefg')
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -13,14 +13,17 @@ from IPython.iplib import InteractiveShell
|
|||||||
|
|
||||||
ip = get()
|
ip = get()
|
||||||
|
|
||||||
|
|
||||||
def magic_j(self, parameter_s=''):
|
def magic_j(self, parameter_s=''):
|
||||||
cmd = ['autojump'] + parameter_s.split()
|
cmd = ['autojump'] + parameter_s.split()
|
||||||
# print 'executing autojump with args %s' % str(cmd)
|
# print 'executing autojump with args %s' % str(cmd)
|
||||||
newpath=sub.Popen(cmd,stdout=sub.PIPE,shell=False).communicate()[0][:-1] # delete last '\n'
|
newpath = sub.Popen(cmd, stdout=sub.PIPE, shell=False).communicate(
|
||||||
|
)[0][:-1] # delete last '\n'
|
||||||
# print 'Autojump answer: \'%s\'' % newpath
|
# print 'Autojump answer: \'%s\'' % newpath
|
||||||
if newpath:
|
if newpath:
|
||||||
ip.magic('cd \'%s\'' % newpath)
|
ip.magic('cd \'%s\'' % newpath)
|
||||||
|
|
||||||
|
|
||||||
def cd_decorator(f):
|
def cd_decorator(f):
|
||||||
def autojump_cd_monitor(self, parameter_s=''):
|
def autojump_cd_monitor(self, parameter_s=''):
|
||||||
f(self, parameter_s)
|
f(self, parameter_s)
|
||||||
|
Loading…
Reference in New Issue
Block a user