diff --git a/bin/autojump b/bin/autojump index ce167bd..43a3123 100755 --- a/bin/autojump +++ b/bin/autojump @@ -57,6 +57,9 @@ class Database: """ Increase weight of existing paths or initialize new ones to 10. """ + if path == self.config['home']: + return + path = path.rstrip(os.sep) if self.data[path]: @@ -70,6 +73,9 @@ class Database: """ Decrease weight of existing path. Unknown ones are ignored. """ + if path == self.config['home']: + return + if self.data[path] < increment: self.data[path] = 0 else: @@ -205,32 +211,30 @@ class Database: def set_defaults(): config = {} - config['version'] = 'release-v21.6.0' + config['version'] = 'release-v21.6.1' config['max_weight'] = 1000 config['max_paths'] = 1000 config['separator'] = '__' + config['home'] = os.path.expanduser('HOME') config['ignore_case'] = False config['keep_entries'] = False config['keep_symlinks'] = False config['debug'] = False - home = os.path.expanduser('HOME') xdg_data = os.environ.get('XDG_DATA_HOME') or \ - os.path.join(home, '.local', 'share') + os.path.join(config['home'], '.local', 'share') config['data'] = os.path.join(xdg_data, 'autojump') config['db'] = config['data'] + '/autojump.txt' return config def parse_env(config): - home = os.path.expanduser('HOME') - if 'AUTOJUMP_DATA_DIR' in os.environ: config['data'] = os.environ.get('AUTOJUMP_DATA_DIR') config['db'] = config['data'] + '/autojump.txt' - if config['data'] == home: + if config['data'] == config['home']: config['db'] = config['data'] + '/.autojump.txt' if 'AUTOJUMP_KEEP_ALL_ENTRIES' in os.environ and \ @@ -256,9 +260,12 @@ def parse_arg(config): 'directory', metavar='DIRECTORY', nargs='*', default='', help='directory to jump to') parser.add_argument( - '-a', '--add', '--increase', metavar='DIRECTORY', - help='manually add path to database, or increase path weight for \ - existing paths') + '-a', '--add', metavar='DIRECTORY', + help='manually add path to database') + parser.add_argument( + '-i', '--increase', metavar='WEIGHT', nargs='?', type=int, + const=20, default=False, + help='manually increase path weight in database') parser.add_argument( '-d', '--decrease', metavar='WEIGHT', nargs='?', type=int, const=15, default=False, @@ -282,19 +289,23 @@ def parse_arg(config): args = parser.parse_args() db = Database(config) - if (args.add): - if (args.add != os.path.expanduser("~")): - db.add(decode(args.add)) - + if args.add: + db.add(decode(args.add)) sys.exit(0) - if (args.decrease): - if (args.decrease != os.path.expanduser("~")): - db.decrease(os.getcwd(), args.decrease) - + if args.increase: + print("%.2f:\t old directory weight" % db.get_weight(os.getcwd())) + db.add(os.getcwd(), args.increase) + print("%.2f:\t new directory weight" % db.get_weight(os.getcwd())) sys.exit(0) - if (args.purge): + if args.decrease: + print("%.2f:\t old directory weight" % db.get_weight(os.getcwd())) + db.decrease(os.getcwd(), args.decrease) + print("%.2f:\t new directory weight" % db.get_weight(os.getcwd())) + sys.exit(0) + + if args.purge: removed = db.purge() if len(removed): @@ -305,7 +316,7 @@ def parse_arg(config): sys.exit(0) - if (args.stat): + if args.stat: for path, weight in sorted(db.data.items(), key=operator.itemgetter(1))[-100:]: output(unico("%.1f:\t%s") % (weight, path)) @@ -313,8 +324,9 @@ def parse_arg(config): print("________________________________________\n") print("%d:\t total key weight" % sum(db.data.values())) print("%d:\t stored directories" % len(db.data)) - print("db file: %s" % config['db']) + print("%.2f:\t current directory weight" % db.get_weight(os.getcwd())) + print("\ndb file: %s" % config['db']) sys.exit(0) config['args'] = args @@ -352,17 +364,17 @@ def unico(text): else: return unicode(text) -def match_last(pattern): +def match_last(config, pattern): """ If the last pattern contains a full path, jump there. The regexp is because we need to support stuff like "j wo jo__3__/home/joel/workspace/joel" for zsh. """ - last_pattern_path = re.sub("(.*)"+COMPLETION_SEPARATOR, "", pattern[-1]) + last_pattern_path = re.sub("(.*)"+config['separator'], "", pattern[-1]) if (len(last_pattern_path) > 0 and last_pattern_path[0] == "/" and os.path.exists(last_pattern_path)): - if not ARGS.complete: + if not config['args'].complete: output(last_pattern_path) return True return False @@ -476,7 +488,7 @@ def main(): # check last pattern for full path # FIXME: disabled until zsh tab completion is fixed on the shell side - # if match_last(patterns): + # if match_last(config, patterns): # return True # check for tab completion