From d75dabd174ac14e57ecb37993914a8343bc44aee Mon Sep 17 00:00:00 2001 From: starnakin Date: Sat, 27 May 2023 16:44:31 +0200 Subject: [PATCH] add: preview --- Menu.py | 27 ++++++++++++++------------- utils.py | 18 ++++++++---------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/Menu.py b/Menu.py index 7776201..3a4d9df 100644 --- a/Menu.py +++ b/Menu.py @@ -74,12 +74,10 @@ class Menu(): self._clear(); size = os.get_terminal_size(); size_x = size.columns; - size_y = size.lines - 2; - preview = self._preview(); - content_size, preview_size = utils.get_sizes(self.size, - preview.count("\n") + 1, - size_y, - self.preview_ratio) + size_y = size.lines; + preview_size = int(self.preview_ratio * size_y); + content_size = size_y - preview_size + preview = self._preview(size_x, preview_size) min_y, max_y, display_pos = utils.render(self.cursor_pos, 0, self.size, @@ -100,15 +98,18 @@ class Menu(): menu = menu + line; self._display_screen(menu + preview) - def _preview(self): + def _preview(self, size_x: int, size_y: int): if (len(self.preview_args) < self.cursor_pos): - return - title = "" - if (self.preview_title_function != None): + return ("") + if (self.preview_body_function == None): + return (""); + if (self.preview_title_function == None): + title = "preview" + else: title = self.preview_title_function(self.preview_args[self.cursor_pos]) - elif (self.preview_body_function != None): - body = self.preview_body_function(self.preview_args[self.cursor_pos]) - return (title + "\n" + body); + body = self.preview_body_function(self.preview_args[self.cursor_pos]) + + return (utils.preview_formater(title, body, size_x, size_y)); def _clear(self): diff --git a/utils.py b/utils.py index f98657c..0cc68f2 100644 --- a/utils.py +++ b/utils.py @@ -19,13 +19,11 @@ def render(pos: int, min: int, max: int, render_distance: int, elements: list): y = pos; return (start, stop, y); -def get_sizes(size1: int, size2: int, total_size: int, aim_ratio: float): - if (size1 + size2 < total_size): - return (size1, size2); - if (size1 > total_size * aim_ratio): - size1 = total_size * aim_ratio; - if (size2 > total_size - size1): - size2 = total_size - size1; - size1 = int(size1); - size2 = int(size2) - return (size1, size2); +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);