From a76c4841160bbe1701c071d55130d9e104380a59 Mon Sep 17 00:00:00 2001 From: starnakin Date: Sat, 27 May 2023 16:03:10 +0200 Subject: [PATCH] add: menu doesn't clear the terminal now --- Menu.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/Menu.py b/Menu.py index 05b2b15..7776201 100644 --- a/Menu.py +++ b/Menu.py @@ -19,6 +19,7 @@ class Menu(): self.skip_empty_option = skip_empty_option; self.size = len(options) self.preview_ratio = preview_ratio; + self._nb_printed_lines = 0; def _up(self): if (self.circular == False): @@ -65,9 +66,11 @@ class Menu(): elif (key == "right"): self._right() elif (key == "enter"): + self._clear(); return (self.cursor_pos) def _display_menu(self): + menu: str = ""; self._clear(); size = os.get_terminal_size(); size_x = size.columns; @@ -93,8 +96,9 @@ class Menu(): line = self.cursor + element[min_x:max_x] else: line = " " * len(self.cursor) + element - print(line) - print(preview) + line = line + "\n" + menu = menu + line; + self._display_screen(menu + preview) def _preview(self): if (len(self.preview_args) < self.cursor_pos): @@ -108,7 +112,15 @@ class Menu(): def _clear(self): - os.system("clear") + UP = "\x1B[3A" + CLR = "\x1B[0K" + for i in range(self._nb_printed_lines): + print(CLR) + print(UP) + + def _display_screen(self, screen: str): + print(screen) + self._nb_printed_lines = screen.count("\n") + 1 def _get_input(self) -> str: flag_have_getch = False