arrows support for foldermenu, and folder changing as command

This commit is contained in:
q
2019-01-29 21:03:33 +02:00
parent eaac92da74
commit 1274f814e8

View File

@@ -22,7 +22,8 @@ def setup_options():
parser=ArgumentParser(description="Prints folder specific shell commands stored in '"+MENUFILE+
"' 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.")
"If the command ends in '&' it is run in the background. "+
"If the command ends in '/' it is a folder, and selecting will enter.")
parser.add_argument("-1","--one-shot",action='store_true', dest='once',default=False,
help="Launch only once, then exit")
parser.add_argument("-b","--no-banner", action='store_false', dest='banner', default=True,
@@ -69,15 +70,17 @@ def ichr(i):
return chr(i)
class bc:
MAG = '\033[95m'
BLU = '\033[94m'
GRE = '\033[92m'
YEL = '\033[93m'
RED = '\033[91m'
CYA = '\033[96m'
MAG = '\033[35m'
BLU = '\033[34m'
GRE = '\033[32m'
YEL = '\033[33m'
RED = '\033[31m'
CYA = '\033[36m'
WHI = '\033[1m'
BG_BLK = '\033[40m'
END = '\033[0m'
CLR = '\033[2J'
INV = '\033[7m'
def disable(self):
self.MAG = ''
@@ -88,6 +91,8 @@ class bc:
self.CYA = ''
self.WHI = ''
self.END = ''
self.BG_BLK = ''
self.INV = ''
def pos(self,y,x):
return "\033[%s;%sH"%( y, x )
@@ -128,6 +133,8 @@ class entry_collection:
self.co = bc()
self.dir_mode = False
self.max_length = 0
self.selected = -1
self.rows = 0
self.initialize()
if not self.options.colors:
self.co.disable()
@@ -224,13 +231,14 @@ class entry_collection:
def menu(self):
""" draws the menu at the top of the screen """
if self.dir_mode:
helptext = "[.]executables"
helptext = "[.]commands"
my_entries = self.dirs
else:
helptext = '[.]folders [-]args (%s%s%s)'%(
helptext = '[.]folders [-]args %s(%s%s%s)'%(
self.co.END + self.co.MAG,
self.co.END,
self.args,
self.co.YEL
self.co.MAG
)
my_entries = self.entries
@@ -264,7 +272,7 @@ class entry_collection:
"%s%s%s [q/x]exit %s%s"%(
self.co.WHI,
cwd,
self.co.YEL,
self.co.MAG,
helptext,
self.co.END
)
@@ -282,7 +290,7 @@ class entry_collection:
maxcolumns = int(math.ceil(maxcolumns / pars))
r = 1 + blen
par = 1
for entry, key in zip(my_entries, self.menu_keys):
for index, (entry, key) in enumerate(zip(my_entries, self.menu_keys)):
if r - blen > rows:
par += 1
r = 1 + blen
@@ -295,15 +303,20 @@ class entry_collection:
else:
column = maxcolumns * ( par - 1 )
border = '| '
if self.selected == index:
highlight = self.co.INV + ">"
else:
highlight = ' '
self.co.posprint(
r+1,
column,
"%s%s%s%s %s%s%s"%(
"%s%s%s%s%s%s%s%s"%(
border,
self.co.WHI,
key,
self.co.END,
self.entry_color(entry.launcher),
highlight,
printline,
self.co.END
)
@@ -314,6 +327,7 @@ class entry_collection:
0,
"#"
)
self.rows = rows
def list(self):
""" draws the list at cursor """
@@ -388,14 +402,17 @@ class entry_collection:
bg = False
idx = self.menu_keys.index(key)
# note, no error checking here
if self.dir_mode:
command_str = self.dirs[idx].command
os.chdir(command_str)
return
# continue here if not changing folders
else:
command_str = self.entries[idx].command
if self.dir_mode or command_str[-1] == '/':
if os.path.isdir(command_str):
os.chdir(command_str)
self.selected = -1
return
if command_str[-1] == '&':
#Run the program in background
command_str = command_str[:-1]
@@ -432,6 +449,7 @@ class entry_collection:
def flip_mode(self):
self.dir_mode = not self.dir_mode
self.selected = 0
def is_key(self, key):
if self.dir_mode:
@@ -446,6 +464,19 @@ class entry_collection:
return (False, 'No such entry')
return (True, '')
def select_move(self, delta):
new_value = self.selected + delta
if new_value < 0:
return
if self.dir_mode:
max_value = len(self.dirs) - 1
else:
max_value = len(self.entries) - 1
if new_value > max_value:
return
self.selected = new_value
def start_engines():
options = setup_options()
@@ -468,7 +499,29 @@ def start_engines():
entries.menu()
inkey = ord(ch.get())
#print('-'+str((inkey))+'-')
if inkey in (113, 120, 27, 3, 24, 4): # esc, q, x
if inkey == 27:
inkey2 = ord(ch.get())
if inkey2 == 91:
inkey3 = ord(ch.get())
if inkey3 == 66:
entries.select_move(1)
if inkey3 == 65:
entries.select_move(-1)
if inkey3 == 67:
entries.select_move(entries.rows)
if inkey3 == 68:
entries.select_move(-entries.rows)
# 66 = down
# 65 = up
# 67 = right
# 68 = left
# 53 = pg up
# 54 = pg down
#
#~ print(inkey3)
#~ sys.exit(0)
if inkey in (113, 120, 3, 24, 4): # q, x
print('Exited in: ' + os.getcwd())
sys.exit(0)
if inkey == 45: # -
@@ -479,6 +532,8 @@ def start_engines():
readline.set_startup_hook(None)
if inkey == 46: # .
entries.flip_mode()
if inkey == 13:
inkey = ord(entries.menu_keys[entries.selected])
found, message = entries.is_key(chr(inkey))
if found:
entries.launch(chr(inkey))