1
0
mirror of https://github.com/wting/autojump synced 2024-10-27 20:34:07 +00:00

adding option to decrease weight

added a new option, -d/--decrease to decrease the weight of a path.
added --increase as an alias to --add for symetry.
This commit is contained in:
Xavier Lepaul 2013-02-24 20:52:09 -08:00
parent 0a87804277
commit b8901586cb
2 changed files with 29 additions and 3 deletions

View File

@ -83,7 +83,7 @@ class Database:
def add(self, path, increment = 10): def add(self, path, increment = 10):
""" """
Increment existing paths or initialize new ones to 10. Increase wheight of existing paths or initialize new ones to 10.
""" """
if path not in self.data: if path not in self.data:
self.data[path] = increment self.data[path] = increment
@ -92,6 +92,15 @@ class Database:
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 decrease(self, path, increment=10):
"""
Decrease wheight of existing path. Unknown ones are ignored.
"""
if path in self.data:
import math
self.data[path] = math.sqrt((self.data[path] ** 2) - (increment ** 2))
self.save()
def decay(self): def decay(self):
""" """
Decay database entries. Decay database entries.
@ -221,8 +230,10 @@ def options():
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')
parser.add_argument('-a', '--add', metavar='DIR', parser.add_argument('-a', '--add', '--increase', metavar='DIR',
help='manually add path to database') help='manually add path to database, or increase path weight for existing paths')
parser.add_argument('-d', '--decrease', metavar='DIR',
help='manually decrease path weight in the database')
parser.add_argument('-b', '--bash', action="store_true", default=False, parser.add_argument('-b', '--bash', action="store_true", default=False,
help='enclose directory quotes to prevent errors') help='enclose directory quotes to prevent errors')
parser.add_argument('--complete', action="store_true", default=False, parser.add_argument('--complete', action="store_true", default=False,
@ -243,6 +254,12 @@ def options():
db.add(decode(ARGS.add)) db.add(decode(ARGS.add))
return True return True
if (ARGS.decrease):
if(ARGS.decrease != os.path.expanduser("~")):
db = Database(DB_FILE)
db.decrease(ARGS.decrease)
return True
if (ARGS.purge): if (ARGS.purge):
db = Database(DB_FILE) db = Database(DB_FILE)
removed = db.purge() removed = db.purge()

View File

@ -53,6 +53,15 @@ class TestAutojump(unittest.TestCase):
self.db.add('/2', 10) self.db.add('/2', 10)
self.assertEqual(self.db.get_weight('/2'), 14.142135623730951) self.assertEqual(self.db.get_weight('/2'), 14.142135623730951)
def test_db_decrease(self):
self.db.add('/1', 100)
self.db.decrease('/a', 42)
self.assertTrue(self.db.get_weight('/a') < 100)
self.db.add('/2', 100)
self.db.add('/2', 42)
self.db.decrease('/2', 42)
self.assertEquals(self.db.get_weight('/2'), 100)
def test_db_get_weight(self): def test_db_get_weight(self):
self.assertEqual(self.db.get_weight('/'), 0) self.assertEqual(self.db.get_weight('/'), 0)