From b8901586cbfc23eac64b0bc8deee3b23172a8c7d Mon Sep 17 00:00:00 2001 From: Xavier Lepaul Date: Sun, 24 Feb 2013 20:52:09 -0800 Subject: [PATCH] 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. --- bin/autojump | 23 ++++++++++++++++++++--- tests/runtests.py | 9 +++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/bin/autojump b/bin/autojump index 829ff3a..f8fd5ef 100755 --- a/bin/autojump +++ b/bin/autojump @@ -83,7 +83,7 @@ class Database: 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: self.data[path] = increment @@ -92,6 +92,15 @@ class Database: self.data[path] = math.sqrt((self.data[path]**2)+(increment**2)) 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): """ Decay database entries. @@ -221,8 +230,10 @@ def options(): epilog="Please see autojump(1) man pages for full documentation.") parser.add_argument('directory', metavar='DIR', nargs='*', default='', help='directory to jump to') - parser.add_argument('-a', '--add', metavar='DIR', - help='manually add path to database') + parser.add_argument('-a', '--add', '--increase', metavar='DIR', + 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, help='enclose directory quotes to prevent errors') parser.add_argument('--complete', action="store_true", default=False, @@ -243,6 +254,12 @@ def options(): db.add(decode(ARGS.add)) return True + if (ARGS.decrease): + if(ARGS.decrease != os.path.expanduser("~")): + db = Database(DB_FILE) + db.decrease(ARGS.decrease) + return True + if (ARGS.purge): db = Database(DB_FILE) removed = db.purge() diff --git a/tests/runtests.py b/tests/runtests.py index 94f592c..b80f314 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -53,6 +53,15 @@ class TestAutojump(unittest.TestCase): self.db.add('/2', 10) 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): self.assertEqual(self.db.get_weight('/'), 0)