From a228ccf608777204cad8a70032f4788f51c345ec Mon Sep 17 00:00:00 2001 From: Ricardo Date: Wed, 1 Jun 2022 02:36:35 +0000 Subject: [PATCH] Designed the windows and settled with ncurses --- ex1.py | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 75 +++++++++++++++++++++++++++------------------------- 2 files changed, 120 insertions(+), 36 deletions(-) create mode 100644 ex1.py diff --git a/ex1.py b/ex1.py new file mode 100644 index 0000000..5532e1d --- /dev/null +++ b/ex1.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 + +import curses +from curses import panel + + +class Menu(object): + def __init__(self, items, stdscreen): + self.window = stdscreen.subwin(0, 0) + self.window.keypad(1) + self.panel = panel.new_panel(self.window) + self.panel.hide() + panel.update_panels() + + self.position = 0 + self.items = items + self.items.append(("exit", "exit")) + + def navigate(self, n): + self.position += n + if self.position < 0: + self.position = 0 + elif self.position >= len(self.items): + self.position = len(self.items) - 1 + + def display(self): + self.panel.top() + self.panel.show() + self.window.clear() + + while True: + self.window.refresh() + curses.doupdate() + for index, item in enumerate(self.items): + if index == self.position: + mode = curses.A_REVERSE + else: + mode = curses.A_NORMAL + + msg = "%d. %s" % (index, item[0]) + self.window.addstr(1 + index, 1, msg, mode) + + key = self.window.getch() + + if key in [curses.KEY_ENTER, ord("\n")]: + if self.position == len(self.items) - 1: + break + else: + self.items[self.position][1]() + + elif key == curses.KEY_UP: + self.navigate(-1) + + elif key == curses.KEY_DOWN: + self.navigate(1) + + self.window.clear() + self.panel.hide() + panel.update_panels() + curses.doupdate() + + +class MyApp(object): + def __init__(self, stdscreen): + self.screen = stdscreen + curses.curs_set(0) + + submenu_items = [("beep", curses.beep), ("flash", curses.flash)] + submenu = Menu(submenu_items, self.screen) + + main_menu_items = [ + ("beep", curses.beep), + ("flash", curses.flash), + ("submenu", submenu.display), + ] + main_menu = Menu(main_menu_items, self.screen) + main_menu.display() + + +if __name__ == "__main__": + curses.wrapper(MyApp) diff --git a/main.py b/main.py index acfb811..19dc169 100644 --- a/main.py +++ b/main.py @@ -1,48 +1,51 @@ #!/usr/bin/env python3 -from blessed import Terminal - -menu = [["login to system"], ["create account"], ["disconnect"]] +import curses +from curses import wrapper -def display_screen(selection): - term = Terminal() - term.clear() - for (idx, m) in enumerate(menu): - if idx == selection: - print('{t.bold_red_reverse}{title}'.format(t=term, title=m[0])) - else: - print('{t.normal}{title}'.format(t=term, title=m[0])) +class MyApp(object): + def __init__(self, screen): + curses.curs_set(0) + curses.init_color(0,0,0,0) + screen.box() + + Cols22 = (curses.COLS - 2) * 0.22 + Cols22 = int(Cols22) + + Cols56 = (curses.COLS - 2) * 0.56 + Cols56 = int(Cols56) + 1 + + Lines = (curses.LINES -2) * 0.95 + Lines = int(Lines) + + title = "Sparks-Skraps" + " COLS -> " + str(curses.COLS) + " LINES -> " + str(Cols56) + titlePos = (curses.COLS / 2) - (len(title) / 2) + + screen.addstr(1, int(titlePos), title) + screen.refresh() -def run_selection(selection): - term.green_reverse('Running {}'.format(menu[selection][0])) + components = curses.newwin(Lines,Cols22,3,1) + components.box() + components.addstr(1, 1, "precious components") + components.refresh() + + parts = curses.newwin(Lines, Cols22, 3, 1+Cols22) + parts.box() + parts.addstr(1, 1, "My precious parts") + parts.refresh() + + info = curses.newwin(Lines, Cols56, 3, 1+Cols22*2) + info.box() + info.addstr(1, 1, "My precious info") + info.refresh() -if __name__ == '__main__': - term = Terminal() - with term.fullscreen(): - selection = 0 - display_screen(selection) - selection_inprogress = True - with term.cbreak(): - while selection_inprogress: - key = term.inkey() - if key.is_sequence: - if key.name == 'KEY_TAB': - selection += 1 - if key.name == 'KEY_DOWN': - selection += 1 - if key.name == 'KEY_UP': - selection -= 1 - if key.name == 'KEY_ENTER': - selection_inprogress = False - elif key: - print("got {0}.".format(key)) + c = screen.getch() - selection = selection % len(menu) - display_screen(selection) - run_selection(selection) +if __name__ == "__main__": + curses.wrapper(MyApp)