Adding folder handling to foldermenu
This commit is contained in:
@@ -58,7 +58,7 @@ class getch:
|
|||||||
return ch
|
return ch
|
||||||
|
|
||||||
def read_menu():
|
def read_menu():
|
||||||
''' Read '''
|
''' Read menu file '''
|
||||||
entries=[]
|
entries=[]
|
||||||
if os.path.exists(MENUFILE):
|
if os.path.exists(MENUFILE):
|
||||||
f=file(MENUFILE,'r')
|
f=file(MENUFILE,'r')
|
||||||
@@ -76,23 +76,27 @@ def read_menu():
|
|||||||
return entries
|
return entries
|
||||||
|
|
||||||
def read_folder():
|
def read_folder():
|
||||||
''' Read '''
|
''' Read folder contents, return executable files and dirs '''
|
||||||
|
|
||||||
|
dirs=[]
|
||||||
|
dirs.append(['..','..'])
|
||||||
executables=[]
|
executables=[]
|
||||||
for f in os.listdir('.'):
|
for f in os.listdir('.'):
|
||||||
if os.path.isfile(f):
|
if os.path.isfile(f):
|
||||||
if os.access(f,os.X_OK):
|
if os.access(f,os.X_OK):
|
||||||
executables.append([f,f])
|
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])
|
executables.sort(key=lambda x: x[0])
|
||||||
|
|
||||||
return executables
|
return (executables,dirs)
|
||||||
|
|
||||||
def print_help():
|
def print_help():
|
||||||
if not os.path.exists(MENUFILE):
|
if not os.path.exists(MENUFILE):
|
||||||
print('Consider having a '+MENUFILE+' file containing shell commands / line.')
|
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"')
|
print('Command may be of format "My Description: my_command" or simply "my_command -switch"')
|
||||||
|
|
||||||
|
|
||||||
def ichr(i):
|
def ichr(i):
|
||||||
''' convert integer to 1-9, a-z, A-Z, omitting x '''
|
''' convert integer to 1-9, a-z, A-Z, omitting x '''
|
||||||
|
|
||||||
@@ -105,18 +109,22 @@ def ichr(i):
|
|||||||
i=i-122+64
|
i=i-122+64
|
||||||
return chr(i)
|
return chr(i)
|
||||||
|
|
||||||
def drawmenu(entries,args=""):
|
def drawmenu(entries,dir_mode,args=""):
|
||||||
maxrows,maxcolumns = termsize()
|
maxrows,maxcolumns = termsize()
|
||||||
maxrows-=5
|
maxrows-=5
|
||||||
maxcolumns-=10
|
maxcolumns-=10
|
||||||
twocol=False
|
twocol=False
|
||||||
co=bc()
|
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:
|
if len(entries)>10:
|
||||||
twocol=True
|
twocol=True
|
||||||
maxrows=int(math.ceil(min(maxrows/2.0, len(entries)/2.0)))
|
maxrows=int(math.ceil(min(maxrows/2.0, len(entries)/2.0)))
|
||||||
maxcolumns=int(math.ceil(maxcolumns/2.0))
|
maxcolumns=int(math.ceil(maxcolumns/2.0))
|
||||||
|
|
||||||
r=1
|
r=1
|
||||||
for e in range(len(entries)):
|
for e in range(len(entries)):
|
||||||
if r>maxrows:
|
if r>maxrows:
|
||||||
@@ -138,11 +146,10 @@ def drawmenu(entries,args=""):
|
|||||||
r=1+r
|
r=1+r
|
||||||
print(co.pos(maxrows+2,0))
|
print(co.pos(maxrows+2,0))
|
||||||
|
|
||||||
|
def append_index(entries,offset=0,color=None,t='menu'):
|
||||||
def append_index(entries,offset=0,color=None,x=False):
|
|
||||||
e=1+offset
|
e=1+offset
|
||||||
for el in range(len(entries)):
|
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
|
e=e+1
|
||||||
return entries
|
return entries
|
||||||
|
|
||||||
@@ -156,8 +163,11 @@ def launch(key,entries,args=""):
|
|||||||
bg=True
|
bg=True
|
||||||
if len(args)>0:
|
if len(args)>0:
|
||||||
command_str=command_str+" "+args
|
command_str=command_str+" "+args
|
||||||
if entries[idx][4]:
|
if entries[idx][4]=='exec':
|
||||||
command_str='./'+command_str
|
command_str='./'+command_str
|
||||||
|
if entries[idx][4]=='dir':
|
||||||
|
os.chdir(command_str)
|
||||||
|
return
|
||||||
try:
|
try:
|
||||||
print('#$ '+command_str)
|
print('#$ '+command_str)
|
||||||
if bg:
|
if bg:
|
||||||
@@ -171,30 +181,47 @@ def launch(key,entries,args=""):
|
|||||||
ch=getch()
|
ch=getch()
|
||||||
inkey=ord(ch.get())
|
inkey=ord(ch.get())
|
||||||
|
|
||||||
|
def initialize():
|
||||||
|
entries=read_menu()
|
||||||
|
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():
|
def main():
|
||||||
entries=read_menu()
|
[entries,dirs]=initialize()
|
||||||
entries=append_index(entries, color=bc.CYA)
|
show_entries=entries
|
||||||
execs=read_folder()
|
dir_mode=False
|
||||||
execs=append_index(execs, offset=len(entries), color=bc.GRE,x=True)
|
|
||||||
entries.extend(execs)
|
|
||||||
ch=getch()
|
ch=getch()
|
||||||
args=""
|
args=""
|
||||||
drawmenu(entries,args)
|
drawmenu(show_entries,dir_mode,args)
|
||||||
while True:
|
while True:
|
||||||
inkey=ord(ch.get())
|
inkey=ord(ch.get())
|
||||||
#print('-'+str((inkey))+'-')
|
#print('-'+str((inkey))+'-')
|
||||||
if inkey in [120,27,3,24,4]:
|
if inkey in [120,27,3,24,4]:
|
||||||
print_help()
|
print_help()
|
||||||
|
print('Exited in: '+os.getcwd())
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
if inkey==45:
|
if inkey==45: # -
|
||||||
readline.set_startup_hook(lambda: readline.insert_text(args))
|
readline.set_startup_hook(lambda: readline.insert_text(args))
|
||||||
args=raw_input('args: ')
|
args=raw_input('args: ')
|
||||||
readline.set_startup_hook(None)
|
readline.set_startup_hook(None)
|
||||||
if chr(inkey) in [x[0] for x in entries]:
|
if inkey==46: # .
|
||||||
launch(chr(inkey),entries,args)
|
dir_mode = not dir_mode
|
||||||
|
|
||||||
drawmenu(entries,args)
|
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()
|
main()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user