From 314353f796218dcfaea2ab1ebc82f30b8f863ed3 Mon Sep 17 00:00:00 2001 From: ville rantanen Date: Wed, 14 Nov 2012 10:17:31 +0200 Subject: [PATCH] Adding folder handling to foldermenu --- foldermenu.py | 73 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 23 deletions(-) diff --git a/foldermenu.py b/foldermenu.py index 5af296d..71f2f34 100755 --- a/foldermenu.py +++ b/foldermenu.py @@ -58,7 +58,7 @@ class getch: return ch def read_menu(): - ''' Read ''' + ''' Read menu file ''' entries=[] if os.path.exists(MENUFILE): f=file(MENUFILE,'r') @@ -76,23 +76,27 @@ def read_menu(): return entries def read_folder(): - ''' Read ''' - + ''' Read folder contents, return executable files and dirs ''' + + dirs=[] + dirs.append(['..','..']) executables=[] for f in os.listdir('.'): if os.path.isfile(f): if os.access(f,os.X_OK): executables.append([f,f]) + if os.path.isdir(f) and f[0]!='.': + dirs.append([f,f]) + dirs.sort(key=lambda x: x[0]) executables.sort(key=lambda x: x[0]) - return executables + return (executables,dirs) def print_help(): if not os.path.exists(MENUFILE): print('Consider having a '+MENUFILE+' file containing shell commands / line.') print('Command may be of format "My Description: my_command" or simply "my_command -switch"') - def ichr(i): ''' convert integer to 1-9, a-z, A-Z, omitting x ''' @@ -105,18 +109,22 @@ def ichr(i): i=i-122+64 return chr(i) -def drawmenu(entries,args=""): +def drawmenu(entries,dir_mode,args=""): maxrows,maxcolumns = termsize() maxrows-=5 maxcolumns-=10 twocol=False co=bc() - print(co.CLR+co.pos(1,3)+co.YEL+'FolderMenu x:exit -:args ('+co.END+args+co.YEL+')'+co.END) + if dir_mode: + helptext=".:execs" + else: + helptext='-:args ('+co.END+args+co.YEL+') .:folders' + + print(co.END+co.CLR+co.pos(1,3)+co.YEL+'FolderMenu x:exit '+helptext+co.END) if len(entries)>10: twocol=True maxrows=int(math.ceil(min(maxrows/2.0, len(entries)/2.0))) maxcolumns=int(math.ceil(maxcolumns/2.0)) - r=1 for e in range(len(entries)): if r>maxrows: @@ -138,11 +146,10 @@ def drawmenu(entries,args=""): r=1+r print(co.pos(maxrows+2,0)) - -def append_index(entries,offset=0,color=None,x=False): +def append_index(entries,offset=0,color=None,t='menu'): e=1+offset for el in range(len(entries)): - entries[el]=[ichr(e), entries[el][0], entries[el][1], color,x] + entries[el]=[ichr(e), entries[el][0], entries[el][1], color,t] e=e+1 return entries @@ -156,8 +163,11 @@ def launch(key,entries,args=""): bg=True if len(args)>0: command_str=command_str+" "+args - if entries[idx][4]: + if entries[idx][4]=='exec': command_str='./'+command_str + if entries[idx][4]=='dir': + os.chdir(command_str) + return try: print('#$ '+command_str) if bg: @@ -170,31 +180,48 @@ def launch(key,entries,args=""): print('Press any key...') ch=getch() inkey=ord(ch.get()) - -def main(): +def initialize(): entries=read_menu() - entries=append_index(entries, color=bc.CYA) - execs=read_folder() - execs=append_index(execs, offset=len(entries), color=bc.GRE,x=True) + entries=append_index(entries, color=bc.CYA,t='menu') + [execs,dirs]=read_folder() + execs=append_index(execs, offset=len(entries), color=bc.GRE,t='exec') entries.extend(execs) + dirs=append_index(dirs,color=bc.BLU+bc.WHI,t='dir') + return (entries,dirs) + + +def main(): + [entries,dirs]=initialize() + show_entries=entries + dir_mode=False + ch=getch() args="" - drawmenu(entries,args) + drawmenu(show_entries,dir_mode,args) while True: inkey=ord(ch.get()) #print('-'+str((inkey))+'-') if inkey in [120,27,3,24,4]: print_help() + print('Exited in: '+os.getcwd()) sys.exit(0) - if inkey==45: + if inkey==45: # - readline.set_startup_hook(lambda: readline.insert_text(args)) args=raw_input('args: ') readline.set_startup_hook(None) - if chr(inkey) in [x[0] for x in entries]: - launch(chr(inkey),entries,args) - - drawmenu(entries,args) + if inkey==46: # . + dir_mode = not dir_mode + + if chr(inkey) in [x[0] for x in show_entries]: + launch(chr(inkey),show_entries,args) + [entries,dirs]=initialize() + if dir_mode: + show_entries=dirs + else: + show_entries=entries + + drawmenu(show_entries,dir_mode,args) main()