diff --git a/Menu.py b/Menu.py index 3a4d9df..ac5b57b 100644 --- a/Menu.py +++ b/Menu.py @@ -1,5 +1,34 @@ import os; -import utils + +def render(pos: int, min: int, max: int, render_distance: int, elements: list): + start = pos - render_distance; + if (start < min): + start = min; + stop = pos + render_distance + 1; + if (stop > max): + stop = max; + i = 0; + y = pos - start; + if (y < min): + y = pos; + if (stop - start != render_distance * 2 + 1): + if (pos - start != render_distance): + stop = stop + render_distance - start - pos + # elif (stop + render_distance > max): + # start = start - (stop + render_distance - max) + y = pos - start; + if (y < min): + y = pos; + return (start, stop, y); + +def preview_formater(title: str, body: str, size_x: int, size_y: int): + out: str = ""; + out += f"┌── {title} " + "─" * (size_x - 6 - len(title)) + "┐\n" + lines = body.split("\n"); + for line in lines[:size_y - 2]: + out += f"│ {line[: size_x - 4]}" + " " * (size_x - 3 - len(line)) + "│\n" + out += "└" + "─" * (size_x - 2) + "┘" + return (out); class Menu(): @@ -75,10 +104,12 @@ class Menu(): size = os.get_terminal_size(); size_x = size.columns; size_y = size.lines; - preview_size = int(self.preview_ratio * size_y); - content_size = size_y - preview_size + preview_size = int(self.preview_ratio * size_y) + content_size = size_y - preview_size - 2 preview = self._preview(size_x, preview_size) - min_y, max_y, display_pos = utils.render(self.cursor_pos, + if (self.title != None): + menu += self.title[:size_x] + "\n"; + min_y, max_y, display_pos = render(self.cursor_pos, 0, self.size, content_size // 2, @@ -86,17 +117,18 @@ class Menu(): for i, option in enumerate(self.options[min_y:max_y]): element = option if (i == display_pos): - min_x, max_x, display_pos_x = utils.render(self.cursor_pos_x, + min_x, max_x, display_pos_x = render(self.cursor_pos_x, 0, - len(element), - len(element) // 2, + size_x, + size_x // 2, option) line = self.cursor + element[min_x:max_x] else: line = " " * len(self.cursor) + element - line = line + "\n" + line = line[:size_x - len(self.cursor)] + "\n" menu = menu + line; - self._display_screen(menu + preview) + menu = menu[:-1] + self._display_screen(menu + "\n" + preview) def _preview(self, size_x: int, size_y: int): if (len(self.preview_args) < self.cursor_pos): @@ -108,14 +140,16 @@ class Menu(): else: title = self.preview_title_function(self.preview_args[self.cursor_pos]) body = self.preview_body_function(self.preview_args[self.cursor_pos]) + if (body == None or title == None): + return ("") - return (utils.preview_formater(title, body, size_x, size_y)); + return (preview_formater(title, body, size_x, size_y)); def _clear(self): UP = "\x1B[3A" CLR = "\x1B[0K" - for i in range(self._nb_printed_lines): + for _ in range(self._nb_printed_lines): print(CLR) print(UP) @@ -164,7 +198,6 @@ class Menu(): print('\nErr:\tcan\'t get input \nFix:\tpip install getch') exit() first_char = getch.getch() - print (first_char) if first_char == '\x1b': #arrow keys a=getch.getch() b=getch.getch() @@ -173,9 +206,3 @@ class Menu(): return 'enter' if ord(first_char) == 32: return 'space' - -def preview(lst): - return ("je suis la") - -menu = Menu("fdddddddddddddddddddddddddddddddddddddddddddddddddddddddda", preview_body_function=preview); -menu.show() diff --git a/utils.py b/utils.py deleted file mode 100644 index 0cc68f2..0000000 --- a/utils.py +++ /dev/null @@ -1,29 +0,0 @@ -def render(pos: int, min: int, max: int, render_distance: int, elements: list): - start = pos - render_distance; - if (start < min): - start = min; - stop = pos + render_distance + 1; - if (stop > max): - stop = max; - i = 0; - y = pos - start; - if (y < min): - y = pos; - if (stop - start != render_distance * 2 + 1): - if (pos - start != render_distance): - stop = stop + render_distance - start - pos - # elif (stop + render_distance > max): - # start = start - (stop + render_distance - max) - y = pos - start; - if (y < min): - y = pos; - return (start, stop, y); - -def preview_formater(title: str, body: str, size_x: int, size_y: int): - out: str = ""; - out += f"┌── {title} " + "─" * (size_x - 6 - len(title)) + "┐\n" - lines = body.split("\n"); - for line in lines[:size_y - 2]: - out += f"│ {line[: size_x - 4]}" + " " * (size_x - 3 - len(line)) + "│\n" - out += "└" + "─" * (size_x - 2) + "┘\n" - return (out);