restructure gcd

This commit is contained in:
ville rantanen
2018-11-08 12:39:12 +02:00
parent 5e1dd8fa52
commit 1a7855242c

View File

@@ -12,19 +12,28 @@ key_list="1234567890qwertyuiop"
class getch: class getch:
def get(self): def get(self):
import termios, tty
fd = sys.stdin.fileno() fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd) old_settings = termios.tcgetattr(fd)
try: try:
tty.setraw(sys.stdin.fileno()) tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1) ch = sys.stdin.read(1)
finally: finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) termios.tcsetattr(
fd,
termios.TCSADRAIN,
old_settings
)
return ch return ch
def printerr(s): def printerr(s):
sys.stderr.write(s + "\n") sys.stderr.write(s + "\n")
sys.stderr.flush() sys.stderr.flush()
def errexit(s):
printerr(s)
sys.exit(1)
def colorize_matches(directories, repl): def colorize_matches(directories, repl):
return [repl.sub("%s\g<1>%s"%(Green, Reset), d) for d in directories] return [repl.sub("%s\g<1>%s"%(Green, Reset), d) for d in directories]
@@ -33,18 +42,29 @@ def search_close_match(directories, key):
match = [] match = []
key = key.lower() key = key.lower()
for name in directories: for name in directories:
s=dl.SequenceMatcher(None, name.lower(), key) s = dl.SequenceMatcher(
s_ratio=s.ratio() None,
if s_ratio > 0: name.lower(),
match.append(( s_ratio, name )) key
)
if s.ratio() > 0:
match.append(( s.ratio(), name ))
match.sort(key = lambda x: x[0], reverse = True) match.sort(key = lambda x: x[0], reverse = True)
best_matches=[x[1] for x in match[0:10]] best_matches=[x[1] for x in match[0:10]]
return best_matches return best_matches
def get_dirs(pat_dir):
# list sub-folders in the folder
current_folders = [d for d in os.listdir(pat_dir) if os.path.isdir(os.path.join(pat_dir, d))]
current_folders = [d for d in current_folders if not d.startswith(".")]
current_folders.sort()
return current_folders
def main():
# Print help # Print help
if len(sys.argv) == 1 or sys.argv[-1] == "-h": if len(sys.argv) == 1 or sys.argv[-1] == "-h":
printerr("Guess cd: Find first match in folder, interactive if multiple matches\n Arguments: [dir/]pattern_to_match_folders") errexit("Guess cd: Find first match in folder, interactive if multiple matches\n Arguments: [dir/]pattern_to_match_folders")
sys.exit(0)
pattern = ".".join(sys.argv[1:]) pattern = ".".join(sys.argv[1:])
pat_base = os.path.basename(pattern) pat_base = os.path.basename(pattern)
@@ -54,18 +74,14 @@ if pat_dir=="":
pat_dir = "." pat_dir = "."
if not os.path.exists(pat_dir): if not os.path.exists(pat_dir):
printerr("%s: %sNo such folder%s"%(pat_dir,Red,Reset)) errexit("%s: %sNo such folder%s"%(pat_dir, Red, Reset))
sys.exit(1)
# list sub-folders in the folder # list sub-folders in the folder
current_folders=[d for d in os.listdir(pat_dir) if os.path.isdir(os.path.join(pat_dir,d))] current_folders = get_dirs(pat_dir)
current_folders=[d for d in current_folders if not d.startswith(".")]
current_folders.sort()
# no sub-folders # no sub-folders
if len(current_folders) == 0: if len(current_folders) == 0:
printerr("There are no subfolders in %s"%(pat_dir,)) errexit("There are no subfolders in %s"%( pat_dir,))
sys.exit(0)
# find matches # find matches
match_case = re.compile(".*" + pat_base + ".*") match_case = re.compile(".*" + pat_base + ".*")
@@ -95,15 +111,18 @@ if len(matching_folders)==1:
# Many matches, ask the user for folder index # Many matches, ask the user for folder index
for i,m in enumerate(matches[0:len(key_list)]): for i,m in enumerate(matches[0:len(key_list)]):
printerr(key_list[i] + ": " + m) printerr(key_list[i] + ": " + m)
if len(matches) > len(key_list): if len(matches) > len(key_list):
printerr("Skipping the rest...") printerr("Skipping the rest...")
import termios,tty
ch=getch() key_in = getch().get()
key_in=ch.get()
try: try:
key_index = key_list.index(key_in) key_index = key_list.index(key_in)
key_match = matching_folders[key_index] key_match = matching_folders[key_index]
except (ValueError, IndexError) as err: except (ValueError, IndexError) as err:
printerr("%s'%s' Not in the list%s"%(Red,key_in,Reset)) errexit("%s'%s' Not in the list%s"%(Red, key_in, Reset))
sys.exit(1)
print(os.path.join(pat_dir, key_match)) print(os.path.join(pat_dir, key_match))
if __name__ == "__main__":
main()