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

25
Menu.py
View File

@ -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);
return (utils.preview_formater(title, body, size_x, size_y));
def _clear(self):

View File

@ -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);