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

Fixes #113, #115, refactor autojump to use argparse over optparse.

This commit is contained in:
William Ting 2012-04-07 04:21:07 -10:00
parent 9147d6cf71
commit 25d1b9cf92
2 changed files with 2 additions and 104 deletions

105
autojump
View File

@ -21,7 +21,6 @@ frequently used places."""
from __future__ import division, print_function from __future__ import division, print_function
import getopt
import argparse import argparse
from sys import argv, stderr, version_info, exit, getfilesystemencoding from sys import argv, stderr, version_info, exit, getfilesystemencoding
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
@ -29,7 +28,7 @@ from operator import itemgetter
import os import os
import shutil import shutil
AUTOJUMP_VERSION = "release-v19-78-gc91843c-dirty" AUTOJUMP_VERSION = "release-v19"
MAX_KEYWEIGHT = 1000 MAX_KEYWEIGHT = 1000
MAX_STORED_PATHS = 600 MAX_STORED_PATHS = 600
COMPLETION_SEPARATOR = '__' COMPLETION_SEPARATOR = '__'
@ -311,7 +310,7 @@ def shell_utility():
if not args.completion and clean_dict(dirs, path_dict): if not args.completion and clean_dict(dirs, path_dict):
save(path_dict, dic_file) save(path_dict, dic_file)
if args.completion and ('--bash', '') in optlist: quotes = "'" if args.completion and args.bash: quotes = "'"
else: quotes = "" else: quotes = ""
if userchoice != -1: if userchoice != -1:
@ -326,106 +325,6 @@ def shell_utility():
return False return False
return True return True
#try:
#optlist, args = getopt.getopt(argv[1:], 'a',
#['stat', 'import', 'completion', 'bash', 'version', 'help'])
#except getopt.GetoptError as ex:
#print("Unknown command line argument: %s" % ex, file=stderr)
#exit(1)
#dic_file = get_dic_file()
#path_dict = open_dic(dic_file)
#if ('-a', '') in optlist:
## The home dir can be reached quickly by "cd"
## and may interfere with other directories
#if(args[-1] != os.path.expanduser("~")):
#dicadd(path_dict, decode(args[-1]))
#save(path_dict, dic_file)
#elif ('--stat', '') in optlist:
#paths = list(path_dict.items())
#paths.sort(key=itemgetter(1))
#for path, count in paths[-100:]:
#output(unico("%.1f:\t%s") % (count, path))
#print("Total key weight: %d. Number of stored paths: %d" %
#(sum(path_dict.values()), len(paths)))
#elif ('--version', '') in optlist:
#print("autojump %s" % AUTOJUMP_VERSION)
#elif ('--help', '') in optlist:
#print("usage: j <dirspec>")
#print("where dirspec is a few characters of the directory you want to jump to.")
#else:
#import re
#completion = False
##userchoice is i if the pattern is __pattern__i, otherwise -1
#userchoice = -1
#results = []
#if ('--completion', '') in optlist:
#completion = True
#else:
#forget(path_dict, dic_file) #gradually forget about old directories
#if not args: patterns = [unico("")]
#else: patterns = [decode(a) for a in args]
## 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, "", patterns[-1])
#if (len(last_pattern_path)>0 and
#last_pattern_path[0] == "/" and
#os.path.exists(last_pattern_path)):
#if not completion: output(last_pattern_path)
#else:
##check for ongoing completion, and act accordingly
#endmatch = re.search(COMPLETION_SEPARATOR+"([0-9]+)", patterns[-1])
#if endmatch: #user has selected a completion
#userchoice = int(endmatch.group(1))
#patterns[-1] = re.sub(COMPLETION_SEPARATOR+"[0-9]+.*",
#"", patterns[-1])
#else: #user hasn't selected a completion, display the same choices again
#endmatch = re.match("(.*)"+COMPLETION_SEPARATOR, patterns[-1])
#if endmatch: patterns[-1] = endmatch.group(1)
#dirs = list(path_dict.items())
#dirs.sort(key=itemgetter(1), reverse=True)
#if completion or userchoice != -1:
#max_matches = 9
#else:
#max_matches = 1
## Don't jump to the current directory
#try:
#current_dir = decode(os.path.realpath(os.curdir))
##Sometimes the current path doesn't exist anymore.
##In that case, jump if possible.
#except OSError:
#current_dir = None
#find_matches(dirs, patterns, results, False, max_matches, current_dir)
## If not found, try ignoring case.
## On completion always show all results
#if completion or not results:
#find_matches(dirs, patterns, results,
#ignore_case=True,
#max_matches=max_matches, current_dir=current_dir)
## Keep the database to a reasonable size
#if not completion and clean_dict(dirs, path_dict):
#save(path_dict, dic_file)
#if completion and ('--bash', '') in optlist: quotes = "'"
#else: quotes = ""
#if userchoice != -1:
#if len(results) > userchoice-1 :
#output(unico("%s%s%s") % (quotes,results[userchoice-1],quotes))
#elif len(results) > 1 and completion:
#output("\n".join(("%s%s%d%s%s" % (patterns[-1],
#COMPLETION_SEPARATOR, n+1, COMPLETION_SEPARATOR, r)
#for n, r in enumerate(results[:8]))))
#elif results: output(unico("%s%s%s")%(quotes,results[0],quotes))
#else:
#return False
#return True
if __name__ == "__main__": if __name__ == "__main__":
success=shell_utility() success=shell_utility()
if not success: exit(1) if not success: exit(1)

View File

@ -59,7 +59,6 @@ function j {
echo -e "\\033[31m${new_path}\\033[0m" echo -e "\\033[31m${new_path}\\033[0m"
cd "${new_path}" cd "${new_path}"
else else
echo "autojump returns: ${new_path}"
echo "autojump: directory '${@}' not found" echo "autojump: directory '${@}' not found"
echo "Try \`autojump --help\` for more information." echo "Try \`autojump --help\` for more information."
false false