mirror of
				https://github.com/wting/autojump
				synced 2025-06-13 12:54:07 +00:00 
			
		
		
		
	minor reorganization
This commit is contained in:
		
							parent
							
								
									a96d1bca9d
								
							
						
					
					
						commit
						73d18eecf2
					
				
							
								
								
									
										77
									
								
								bin/autojump
									
									
									
									
									
								
							
							
						
						
									
										77
									
								
								bin/autojump
									
									
									
									
									
								
							@ -170,7 +170,7 @@ def options():
 | 
				
			|||||||
            db = Database(DB_FILE)
 | 
					            db = Database(DB_FILE)
 | 
				
			||||||
            db.add(decode(ARGS.add))
 | 
					            db.add(decode(ARGS.add))
 | 
				
			||||||
            db.save()
 | 
					            db.save()
 | 
				
			||||||
        sys.exit(0)
 | 
					        return True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (ARGS.stat):
 | 
					    if (ARGS.stat):
 | 
				
			||||||
        db = Database(DB_FILE)
 | 
					        db = Database(DB_FILE)
 | 
				
			||||||
@ -180,7 +180,8 @@ def options():
 | 
				
			|||||||
            output(unico("%.1f:\t%s") % (count, path))
 | 
					            output(unico("%.1f:\t%s") % (count, path))
 | 
				
			||||||
        print("Total key weight: %d. Number of stored dirs: %d" %
 | 
					        print("Total key weight: %d. Number of stored dirs: %d" %
 | 
				
			||||||
                (sum(db.data.values()), len(dirs)))
 | 
					                (sum(db.data.values()), len(dirs)))
 | 
				
			||||||
        sys.exit(0)
 | 
					        return True
 | 
				
			||||||
 | 
					    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. """
 | 
				
			||||||
@ -240,8 +241,17 @@ def match(path, pattern, ignore_case=False, only_end=False):
 | 
				
			|||||||
        eaten_path = path
 | 
					        eaten_path = path
 | 
				
			||||||
    return (does_match, eaten_path)
 | 
					    return (does_match, eaten_path)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def find_matches(dirs, patterns, result_list, ignore_case, max_matches, current_dir):
 | 
					def find_matches(db, patterns, max_matches, ignore_case):
 | 
				
			||||||
    """ 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. """
 | 
				
			||||||
 | 
					    try:
 | 
				
			||||||
 | 
					        current_dir = decode(os.path.realpath(os.curdir))
 | 
				
			||||||
 | 
					    except OSError:
 | 
				
			||||||
 | 
					        current_dir = None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    dirs = list(db.data.items())
 | 
				
			||||||
 | 
					    dirs.sort(key=itemgetter(1), reverse=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    results = []
 | 
				
			||||||
    for path, _ in dirs:
 | 
					    for path, _ in dirs:
 | 
				
			||||||
        # Don't jump to where we alread are
 | 
					        # Don't jump to where we alread are
 | 
				
			||||||
        if current_dir == path :
 | 
					        if current_dir == path :
 | 
				
			||||||
@ -254,14 +264,16 @@ def find_matches(dirs, patterns, result_list, ignore_case, max_matches, current_
 | 
				
			|||||||
        #If a path doesn't exist, don't jump there
 | 
					        #If a path doesn't exist, don't jump there
 | 
				
			||||||
        #We still keep it in db in case it's from a removable drive
 | 
					        #We still keep it in db in case it's from a removable drive
 | 
				
			||||||
        if does_match and os.path.exists(path):
 | 
					        if does_match and os.path.exists(path):
 | 
				
			||||||
            if path not in result_list:
 | 
					            if path not in results:
 | 
				
			||||||
                result_list.append(path)
 | 
					                results.append(path)
 | 
				
			||||||
            if len(result_list) >= max_matches :
 | 
					            if len(results) >= max_matches :
 | 
				
			||||||
                break
 | 
					                break
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    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. """
 | 
				
			||||||
    options()
 | 
					    if options(): return True
 | 
				
			||||||
    db = Database(DB_FILE)
 | 
					    db = Database(DB_FILE)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # if no directories, add empty string
 | 
					    # if no directories, add empty string
 | 
				
			||||||
@ -271,52 +283,43 @@ def shell_utility():
 | 
				
			|||||||
        patterns = [decode(a) for a in ARGS.directory]
 | 
					        patterns = [decode(a) for a in ARGS.directory]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # check last pattern for full path
 | 
					    # check last pattern for full path
 | 
				
			||||||
    # FIXME: disabled until tab completion is fixed on the shell side
 | 
					    # FIXME: disabled until zsh tab completion is fixed on the shell side
 | 
				
			||||||
    # if match_last(patterns): return True
 | 
					    # if match_last(patterns): return True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    #check for ongoing completion, and act accordingly
 | 
					    # check for tab completion
 | 
				
			||||||
    userchoice = -1
 | 
					    tab_choice = -1
 | 
				
			||||||
    endmatch = re.search(COMPLETION_SEPARATOR+"([0-9]+)", patterns[-1])
 | 
					    tab_match = re.search(COMPLETION_SEPARATOR+"([0-9]+)", patterns[-1])
 | 
				
			||||||
    if endmatch:  #user has selected a completion
 | 
					    if tab_match: # user has selected a tab completion entry
 | 
				
			||||||
        userchoice = int(endmatch.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 completion, display the same choices again
 | 
					    else: # user hasn't selected a tab completion, display choices again
 | 
				
			||||||
        endmatch = re.match("(.*)"+COMPLETION_SEPARATOR, patterns[-1])
 | 
					        tab_match = re.match("(.*)"+COMPLETION_SEPARATOR, patterns[-1])
 | 
				
			||||||
        if endmatch: patterns[-1] = endmatch.group(1)
 | 
					        if tab_match:
 | 
				
			||||||
 | 
					            patterns[-1] = tab_match.group(1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if ARGS.complete or userchoice != -1:
 | 
					    # on tab completion always show all results
 | 
				
			||||||
 | 
					    if ARGS.complete or tab_choice != -1:
 | 
				
			||||||
        max_matches = 9
 | 
					        max_matches = 9
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        max_matches = 1
 | 
					        max_matches = 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    try:
 | 
					    results = find_matches(db, patterns, max_matches, False)
 | 
				
			||||||
        current_dir = decode(os.path.realpath(os.curdir))
 | 
					    # if no results, try ignoring case
 | 
				
			||||||
    except OSError:
 | 
					 | 
				
			||||||
        current_dir = None
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    results = []
 | 
					 | 
				
			||||||
    dirs = list(db.data.items())
 | 
					 | 
				
			||||||
    dirs.sort(key=itemgetter(1), reverse=True)
 | 
					 | 
				
			||||||
    find_matches(dirs, patterns, results, False, max_matches, current_dir)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # If not found, try ignoring case.
 | 
					 | 
				
			||||||
    # On completion always show all results
 | 
					 | 
				
			||||||
    if ARGS.complete or not results:
 | 
					    if ARGS.complete or not results:
 | 
				
			||||||
        find_matches(dirs, patterns, results,
 | 
					        results = find_matches(db, patterns, max_matches, True)
 | 
				
			||||||
                ignore_case=True,
 | 
					 | 
				
			||||||
                max_matches=max_matches, current_dir=current_dir)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    quotes = ""
 | 
				
			||||||
    if ARGS.complete and ARGS.bash: quotes = "'"
 | 
					    if ARGS.complete and ARGS.bash: quotes = "'"
 | 
				
			||||||
    else: quotes = ""
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if userchoice != -1:
 | 
					    if tab_choice != -1:
 | 
				
			||||||
        if len(results) > userchoice-1 :
 | 
					        if len(results) > tab_choice-1:
 | 
				
			||||||
            output(unico("%s%s%s") % (quotes,results[userchoice-1],quotes))
 | 
					            output(unico("%s%s%s") % (quotes,results[tab_choice-1],quotes))
 | 
				
			||||||
    elif len(results) > 1 and ARGS.complete:
 | 
					    elif len(results) > 1 and ARGS.complete:
 | 
				
			||||||
        output("\n".join(("%s%s%d%s%s" % (patterns[-1],
 | 
					        output("\n".join(("%s%s%d%s%s" % (patterns[-1],
 | 
				
			||||||
            COMPLETION_SEPARATOR, n+1, COMPLETION_SEPARATOR, r)
 | 
					            COMPLETION_SEPARATOR, n+1, COMPLETION_SEPARATOR, r)
 | 
				
			||||||
            for n, r in enumerate(results[:8]))))
 | 
					            for n, r in enumerate(results[:8]))))
 | 
				
			||||||
    elif results: output(unico("%s%s%s")%(quotes,results[0],quotes))
 | 
					    elif results:
 | 
				
			||||||
 | 
					        output(unico("%s%s%s")%(quotes,results[0],quotes))
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        return False
 | 
					        return False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user