From 47b9d50e9ea62c0fde5c4e9caaae5a01c5ceed12 Mon Sep 17 00:00:00 2001 From: Paco Hope Date: Tue, 14 Jun 2022 23:04:01 -0400 Subject: [PATCH] added graphical TUI --- menu.py | 100 +++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 2 +- 2 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 menu.py diff --git a/menu.py b/menu.py new file mode 100644 index 0000000..8b2c194 --- /dev/null +++ b/menu.py @@ -0,0 +1,100 @@ +from picotui.widgets import * +from picotui.menu import * +from picotui.context import Context +import os +import re +import cli + +fileToDo = None + +# case insensitive search for files ending in yaml or yml. +def yamlFilter(filename): + return( re.search(".ya?ml$", filename, flags=re.IGNORECASE) != None) + +# Dialog on the screen +d = None + +# This routine is called to redraw screen "in menu's background" +def screen_redraw(s, allow_cursor=False): + s.attr_color(C_WHITE, C_BLUE) + s.cls() + s.attr_reset() + d.redraw() + + +# We have two independent widgets on screen: dialog and main menu, +# so can't call their individual loops, and instead should have +# "main loop" to route events to currently active widget, and +# switch the active one based on special events. +def main_loop(): + while 1: + key = m.get_input() + + if isinstance(key, list): + # Mouse click + x, y = key + if m.inside(x, y): + m.focus = True + + if m.focus: + # If menu is focused, it gets events. If menu is cancelled, + # it loses focus. Otherwise, if menu selection is made, we + # quit with with menu result. + res = m.handle_input(key) + if res == ACTION_CANCEL: + m.focus = False + elif res is not None and res is not True: + return res + else: + # If menu isn't focused, it can be focused by pressing F9. + if key == KEY_F9: + m.focus = True + m.redraw() + continue + # Otherwise, dialog gets input + res = d.handle_input(key) + if res is not None and res is not True: + return res + +with Context(): + + d = Dialog(10, 5, 80, 24) + d.add(2, 21, WLabel("Press F9 for menu")) + d.add(2, 22, WLabel("Press Esc to Exit")) + + # Get all files in the current directory that are YAML files + d.add(4, 2, WLabel("Available YAML Files:")) + # dir_list = list(filter(yamlFilter, os.listdir('.'))) + dir_list = os.listdir('.') + print( dir_list ) + # Draw a list box with all those files in it + filelist = WListBox(26, 10, dir_list ) + d.add(4, 3, filelist ) + + b = WButton(8, "OK") + d.add(10, 19, b) + b.finish_dialog = ACTION_OK + + b = WButton(8, "Cancel") + d.add(23, 19, b) + b.finish_dialog = ACTION_CANCEL + + screen_redraw(Screen) + Screen.set_screen_redraw(screen_redraw) + + menu_file = WMenuBox([("Open...", "Open"), ("Save", "S"), ("Save as...", "Sa"), ("Exit", "ex")]) + menu_edit = WMenuBox([("Copy", "copy"), ("Paste", "paste")]) + m = WMenuBar([("File", menu_file), ("Edit", menu_edit), ("About", "About")]) + m.permanent = True + m.redraw() + + res = main_loop() + # We get here when the user finishes. + if( res != ACTION_CANCEL ): + fileToDo = filelist.content[filelist.choice] + +if( fileToDo != None ): + configfile = open( fileToDo, 'r', encoding='utf8') + cli.parseFile(configfile) +else: + print( "Cancelled" ) diff --git a/requirements.txt b/requirements.txt index 3d189cd..4ea908b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ pyyaml -argparse \ No newline at end of file +argparse