fix:
This commit is contained in:
parent
d75dabd174
commit
e2870e2ced
63
Menu.py
63
Menu.py
@ -1,5 +1,34 @@
|
|||||||
import os;
|
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():
|
class Menu():
|
||||||
|
|
||||||
@ -75,10 +104,12 @@ class Menu():
|
|||||||
size = os.get_terminal_size();
|
size = os.get_terminal_size();
|
||||||
size_x = size.columns;
|
size_x = size.columns;
|
||||||
size_y = size.lines;
|
size_y = size.lines;
|
||||||
preview_size = int(self.preview_ratio * size_y);
|
preview_size = int(self.preview_ratio * size_y)
|
||||||
content_size = size_y - preview_size
|
content_size = size_y - preview_size - 2
|
||||||
preview = self._preview(size_x, preview_size)
|
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,
|
0,
|
||||||
self.size,
|
self.size,
|
||||||
content_size // 2,
|
content_size // 2,
|
||||||
@ -86,17 +117,18 @@ class Menu():
|
|||||||
for i, option in enumerate(self.options[min_y:max_y]):
|
for i, option in enumerate(self.options[min_y:max_y]):
|
||||||
element = option
|
element = option
|
||||||
if (i == display_pos):
|
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,
|
0,
|
||||||
len(element),
|
size_x,
|
||||||
len(element) // 2,
|
size_x // 2,
|
||||||
option)
|
option)
|
||||||
line = self.cursor + element[min_x:max_x]
|
line = self.cursor + element[min_x:max_x]
|
||||||
else:
|
else:
|
||||||
line = " " * len(self.cursor) + element
|
line = " " * len(self.cursor) + element
|
||||||
line = line + "\n"
|
line = line[:size_x - len(self.cursor)] + "\n"
|
||||||
menu = menu + line;
|
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):
|
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):
|
||||||
@ -108,14 +140,16 @@ class Menu():
|
|||||||
else:
|
else:
|
||||||
title = self.preview_title_function(self.preview_args[self.cursor_pos])
|
title = self.preview_title_function(self.preview_args[self.cursor_pos])
|
||||||
body = self.preview_body_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):
|
def _clear(self):
|
||||||
UP = "\x1B[3A"
|
UP = "\x1B[3A"
|
||||||
CLR = "\x1B[0K"
|
CLR = "\x1B[0K"
|
||||||
for i in range(self._nb_printed_lines):
|
for _ in range(self._nb_printed_lines):
|
||||||
print(CLR)
|
print(CLR)
|
||||||
print(UP)
|
print(UP)
|
||||||
|
|
||||||
@ -164,7 +198,6 @@ class Menu():
|
|||||||
print('\nErr:\tcan\'t get input \nFix:\tpip install getch')
|
print('\nErr:\tcan\'t get input \nFix:\tpip install getch')
|
||||||
exit()
|
exit()
|
||||||
first_char = getch.getch()
|
first_char = getch.getch()
|
||||||
print (first_char)
|
|
||||||
if first_char == '\x1b': #arrow keys
|
if first_char == '\x1b': #arrow keys
|
||||||
a=getch.getch()
|
a=getch.getch()
|
||||||
b=getch.getch()
|
b=getch.getch()
|
||||||
@ -173,9 +206,3 @@ class Menu():
|
|||||||
return 'enter'
|
return 'enter'
|
||||||
if ord(first_char) == 32:
|
if ord(first_char) == 32:
|
||||||
return 'space'
|
return 'space'
|
||||||
|
|
||||||
def preview(lst):
|
|
||||||
return ("je suis la")
|
|
||||||
|
|
||||||
menu = Menu("fdddddddddddddddddddddddddddddddddddddddddddddddddddddddda", preview_body_function=preview);
|
|
||||||
menu.show()
|
|
||||||
|
29
utils.py
29
utils.py
@ -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);
|
|
Loading…
Reference in New Issue
Block a user