From 3ff76bf95a3f7c168e4f77ea6e98e31217f9b21f Mon Sep 17 00:00:00 2001 From: ville rantanen Date: Mon, 7 Jan 2013 13:37:09 +0200 Subject: [PATCH] fixing problems with huge lists --- foldermenu.py | 56 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/foldermenu.py b/foldermenu.py index d78273a..76a3b54 100755 --- a/foldermenu.py +++ b/foldermenu.py @@ -13,6 +13,7 @@ readline.parse_and_bind('set editing-mode vi') MENUFILE='.foldermenu' DEFAULTFILE = os.path.expanduser(os.path.join('~','.config','foldermenu','default')) +VERSION="0.3" def setup_options(): ''' Setup the command line options ''' @@ -22,20 +23,23 @@ def setup_options(): "' file, and in addition the executables in the current folder. "+ "Menufile format for each line: 'description:command'. "+ "If the command ends in '&' it is run in the background.") + parser.add_argument("-1","--one-shot",action='store_true', dest='once',default=False, + help="Launch only once, then exit") parser.add_argument("-d","--no-defaults",action='store_false', dest='defaults',default=True, help="Do not show default entries from "+DEFAULTFILE) parser.add_argument("-x","--no-exec",action='store_false', dest='executables',default=True, help="Do not show executables in the listing.") - parser.add_argument("--columns",'-f',type=int,action='store', dest='columns',default=0, + parser.add_argument("--columns",'-f','-C',type=int,action='store', dest='columns',default=0, help="Number of columns. 0 for automatic") parser.add_argument("-l","--list",action='store_true', dest='list',default=False, help="Print the list, don't wait for keypress.") parser.add_argument("--command",'-c',type=str,action='store', dest='command', help="Command to run (1-9a-z..), any argumets after -- are forwarded to the command ") - parser.add_argument("--no-colors",action="store_false",dest="colors",default=True, + parser.add_argument("--no-colors",'--nc',action="store_false",dest="colors",default=True, help="Disable colored output") - parser.add_argument("--horizontal",action="store_true",dest="horizontal",default=False, + parser.add_argument("--horizontal",'-H',action="store_true",dest="horizontal",default=False, help="Horizontal order of items, only valid for -l listing.") + parser.add_argument("--version",action='version', version=VERSION) parser.add_argument("args",type=str,action="store",default="",nargs="?", help="Arguments for the command, if -c used. The string will be re-parsed with shutils. Use '--' to skip local parsing.") options=parser.parse_args() @@ -132,6 +136,8 @@ class entry_collection: self.read_menu(DEFAULTFILE) self.read_menu() self.read_folder() + self.entries=self.entries[0:60] + self.dirs=self.dirs[0:60] if len(self.entries)>0: self.max_length=max([len(e.description) for e in self.entries])+1 @@ -201,21 +207,27 @@ class entry_collection: my_entries=self.entries maxrows,maxcolumns = termsize() - maxrows-=5 + rows=maxrows - 5 maxcolumns-=10 if self.options.columns==0: - pars=float(1) + pars=1 if len(my_entries)>9: - pars=float(2) + pars=2 + if len(my_entries)>30: + pars=3 + pars=float(pars) else: pars=float(self.options.columns) print(self.co.END+self.co.CLR+self.co.pos(1,3)+self.co.YEL+'FolderMenu x:exit '+helptext+self.co.END) - maxrows=int(math.ceil(min(maxrows/pars, len(my_entries)/pars))) + rows=int(math.ceil(len(my_entries)/pars)) + while rows > maxrows: + pars+=1 + rows=int(math.ceil(len(my_entries)/pars)) maxcolumns=int(math.ceil(maxcolumns/pars)) r=1 par=1 for e,i in zip(my_entries,self.menu_keys): - if r>maxrows: + if r>rows: par=1+par r=1 printline=e.description @@ -226,12 +238,12 @@ class entry_collection: else: print(self.co.pos(r+1,maxcolumns*(par-1))+'| '+self.co.WHI+i+self.co.END+' '+self.entry_color(e.launcher)+printline+self.co.END) r=1+r - print(self.co.pos(maxrows+2,0)) + print(self.co.pos(rows+2,0)) def list(self): """ draws the list at cursor """ maxrows,maxcolumns = termsize() - maxrows-=5 + rows=maxrows-5 maxcolumns-=10 # heuristics for guessing column count if self.options.columns==0: @@ -244,8 +256,8 @@ class entry_collection: pars-=1 else: pars=float(self.options.columns) - maxrows=int(math.ceil(min(maxrows/pars, len(self.entries)/pars))) - maxcolumns=int(math.ceil(maxcolumns/pars))-2 + rows=int(math.ceil(len(self.entries)/float(pars))) + maxcolumns=int(math.floor(maxcolumns/pars))-2 # If names won't fit the columns, make sure at least 3 characters are visible if maxcolumns<6: origmaxrows,origmaxcolumns = termsize() @@ -253,16 +265,16 @@ class entry_collection: origmaxcolumns-=10 while maxcolumns<6: pars=pars-1 - maxrows=int(math.ceil(min(origmaxrows/pars, len(self.entries)/pars))) - maxcolumns=int(math.ceil(origmaxcolumns/pars))-2 + rows=int(math.ceil(len(self.entries)/float(pars))) + maxcolumns=int(math.floor(origmaxcolumns/pars))-2 self.max_length=min(maxcolumns,self.max_length) if self.options.horizontal: foo=pars - pars=maxrows - maxrows=foo + pars=rows + rows=foo formatted=[] - for r in range(int(maxrows)): + for r in range(int(rows)): formatted.append([]) for p in range(int(pars)): formatted[r].append(' '*(self.max_length)) @@ -271,7 +283,7 @@ class entry_collection: r=0 par=0 for e,i in zip(self.entries,self.menu_keys): - if r>=maxrows: + if r>=rows: par=1+par r=0 printline=e.description[:(maxcolumns-3)] @@ -289,6 +301,7 @@ class entry_collection: ''' launch the given entry ''' bg=False + wait=True #Run the program in background idx=self.menu_keys.index(key) # note, no error checking here @@ -317,7 +330,10 @@ class entry_collection: except: print('Unable to run: "'+command_str+'"') - if not (self.options.command or bg): + if (self.options.command or self.options.once or bg): + wait=False + + if wait: print('Press any key...') ch=getch() inkey=ord(ch.get()) @@ -373,6 +389,8 @@ def start_engines(): found,message=entries.is_key(chr(inkey)) if found: entries.launch(chr(inkey)) + if options.once and not entries.dir_mode: + sys.exit(0) entries.initialize() start_engines()