add: preview

This commit is contained in:
starnakin 2023-05-27 16:44:31 +02:00
parent a76c484116
commit d75dabd174
2 changed files with 22 additions and 23 deletions

27
Menu.py
View File

@ -74,12 +74,10 @@ class Menu():
self._clear(); self._clear();
size = os.get_terminal_size(); size = os.get_terminal_size();
size_x = size.columns; size_x = size.columns;
size_y = size.lines - 2; size_y = size.lines;
preview = self._preview(); preview_size = int(self.preview_ratio * size_y);
content_size, preview_size = utils.get_sizes(self.size, content_size = size_y - preview_size
preview.count("\n") + 1, preview = self._preview(size_x, preview_size)
size_y,
self.preview_ratio)
min_y, max_y, display_pos = utils.render(self.cursor_pos, min_y, max_y, display_pos = utils.render(self.cursor_pos,
0, 0,
self.size, self.size,
@ -100,15 +98,18 @@ class Menu():
menu = menu + line; menu = menu + line;
self._display_screen(menu + preview) 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): if (len(self.preview_args) < self.cursor_pos):
return return ("")
title = "" if (self.preview_body_function == None):
if (self.preview_title_function != None): return ("");
if (self.preview_title_function == None):
title = "preview"
else:
title = self.preview_title_function(self.preview_args[self.cursor_pos]) 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])
body = self.preview_body_function(self.preview_args[self.cursor_pos])
return (title + "\n" + body); return (utils.preview_formater(title, body, size_x, size_y));
def _clear(self): def _clear(self):

View File

@ -19,13 +19,11 @@ def render(pos: int, min: int, max: int, render_distance: int, elements: list):
y = pos; y = pos;
return (start, stop, y); return (start, stop, y);
def get_sizes(size1: int, size2: int, total_size: int, aim_ratio: float): def preview_formater(title: str, body: str, size_x: int, size_y: int):
if (size1 + size2 < total_size): out: str = "";
return (size1, size2); out += f"┌── {title} " + "" * (size_x - 6 - len(title)) + "\n"
if (size1 > total_size * aim_ratio): lines = body.split("\n");
size1 = total_size * aim_ratio; for line in lines[:size_y - 2]:
if (size2 > total_size - size1): out += f"{line[: size_x - 4]}" + " " * (size_x - 3 - len(line)) + "\n"
size2 = total_size - size1; out += "" + "" * (size_x - 2) + "\n"
size1 = int(size1); return (out);
size2 = int(size2)
return (size1, size2);