From e0c8c2237cc5ee12cc5f4b3c3086933816b8f000 Mon Sep 17 00:00:00 2001 From: ville rantanen Date: Sat, 19 Jul 2014 21:23:48 +0300 Subject: [PATCH] new function to replace gcd later --- qcd_function | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/qcd_function b/qcd_function index 6f05eac..03e4f0d 100644 --- a/qcd_function +++ b/qcd_function @@ -190,6 +190,79 @@ function gcd() { echo No match. } +function fcd() { + # gcd future fork + local cdto + { cdto=$( python -c ' +import sys,os,re,termios,tty +if len(sys.argv)==1: + print("Arguments: [dir/]pattern_to_match_folders") + sys.exit(0) +def G(): + return "\033[32;1m" +def R(): + return "\033[31;1m" +def Z(): + return "\033[0m" +def CS(): + return "1234567890qwertyuiop" +class getch: + def get(self): + fd = sys.stdin.fileno() + old_settings = termios.tcgetattr(fd) + try: + tty.setraw(sys.stdin.fileno()) + ch = sys.stdin.read(1) + finally: + termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) + return ch + +pattern=".".join(sys.argv[1:]) + +pat_dir=os.path.dirname(pattern) +if pat_dir=="": + pat_dir="." +pat_base=os.path.basename(pattern) +if not os.path.exists(pat_dir): + print(pat_dir+": "+R()+"No such folder"+Z()) + sys.exit(1) +current_folders=[d for d in os.listdir(pat_dir) if os.path.isdir(d)] +current_folders=[d for d in current_folders if not d.startswith(".")] +current_folders.sort() + +match_case=re.compile(".*"+pat_base+".*") +repl_case=re.compile("("+pat_base+")") + +matching_folders=[d for d in current_folders if match_case.match(d)] +matches=[repl_case.sub(G()+"\g<1>"+Z(),d) for d in matching_folders] + +if len(matching_folders)==0: + match_nocase=re.compile(".*"+pat_base+".*",re.I) + repl_nocase=re.compile(pat_base,re.I) + matching_folders=[d for d in current_folders if match_nocase.match(d)] + matches=[repl_case.sub(G()+pat_base+Z(),d) for d in matching_folders] + +if len(matching_folders)==1: + print(matches[0]) + sys.stderr.write(os.path.join(pat_dir,matching_folders[0])) + sys.exit(0) +if len(matching_folders)==0: + print(R()+"no matches"+Z()) + sys.exit(1) +for i,m in enumerate(matches[0:20]): + print(CS()[i]+": "+m) +ch=getch() +key_in=ch.get() +try: + key_index=CS().index(key_in) + key_match=matching_folders[key_index] +except ValueError,IndexError: + sys.exit(1) +sys.stderr.write(os.path.join(pat_dir,key_match)) +' "$@" 2>&1 1>&$out); } {out}>&1 ; \cd "$cdto" + +} + function rmv () { #mv with rsync local sources