You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.5 KiB
83 lines
2.5 KiB
2 years ago
|
import os
|
||
2 years ago
|
#from time import sleep
|
||
2 years ago
|
from typing import Dict
|
||
2 years ago
|
from cli.colors import Colors
|
||
2 years ago
|
|
||
|
class Layer:
|
||
|
def __init__(self, x, y):
|
||
|
self.screen = {}
|
||
2 years ago
|
self.x = x #largeur
|
||
2 years ago
|
self.y = y #hauteur
|
||
2 years ago
|
|
||
|
def clear(self):
|
||
|
self.screen = {}
|
||
|
|
||
|
def put_char(self, char, x, y, *colors):
|
||
|
if x > self.x or x < 0 or y > self.y or y < 0:
|
||
|
raise ValueError("unexcepted value")
|
||
|
self.screen[(x, y)] = "".join(colors) + char + Colors.RESET
|
||
|
|
||
|
def put_string(self, string, x, y, *colors):
|
||
|
if x > self.x or x < 0 or y > self.y or y < 0:
|
||
|
raise ValueError("unexcepted value")
|
||
|
line_y = 0
|
||
|
for line in string.split("\n"):
|
||
|
for char_x in range(len(line)):
|
||
|
self.screen[(x+char_x, y+line_y)] = "".join(colors) + line[char_x] + Colors.RESET
|
||
|
line_y += 1
|
||
|
|
||
|
|
||
|
class CLI:
|
||
|
def __init__(self):
|
||
|
size = os.get_terminal_size()
|
||
2 years ago
|
self.x = size.columns #largeur
|
||
|
self.y = size.lines - 2 #hauteur
|
||
2 years ago
|
self.screen = {}
|
||
|
self.__layers = {}
|
||
|
|
||
|
def set_layer(self, name: str, layer: Layer):
|
||
|
self.__layers[name] = layer
|
||
|
|
||
|
def get_layer(self, name: str) -> Layer:
|
||
|
return self.__layers[name]
|
||
|
|
||
|
def get_layers(self) -> Dict[str, Layer]:
|
||
|
return self.__layers
|
||
|
|
||
2 years ago
|
def combine_layers(self):
|
||
2 years ago
|
layers = self.get_layers()
|
||
2 years ago
|
self.screen = {}
|
||
|
for layer_name in layers:
|
||
|
for x in range(layers[layer_name].x):
|
||
|
for y in range(layers[layer_name].y):
|
||
|
if (x, y) in layers[layer_name].screen:
|
||
|
self.screen[(x, y)] = layers[layer_name].screen[(x, y)]
|
||
2 years ago
|
|
||
2 years ago
|
def draw(self):
|
||
2 years ago
|
self.combine_layers()
|
||
2 years ago
|
content = ""
|
||
|
for y in range(self.y):
|
||
|
#content += "\n"
|
||
|
for x in range(self.x):
|
||
|
if (x, y) in self.screen:
|
||
|
content += self.screen[(x, y)]
|
||
|
else:
|
||
2 years ago
|
content += " "
|
||
2 years ago
|
content += "\n"
|
||
|
print("\033[H\033[J", end="")
|
||
|
print(content)
|
||
2 years ago
|
|
||
2 years ago
|
#if __name__ == "__main__":
|
||
|
# cli = CLI()
|
||
|
# layer = Layer(cli.x, cli.y)
|
||
|
# layer.put_char("t", 5, 6, Colors.BLUEBG, Colors.BLACK)
|
||
|
# layer.put_string("""test""", 8, 9, Colors.BEIGEBG)
|
||
|
#
|
||
|
# cli.set_layer("test", layer)
|
||
|
# ##cli.put_char("t", 5, 6, Colors.BLUEBG, Colors.BLACK)
|
||
|
# #cli.put_string("tttaaaaaa ttt \n ttttttttttnnnnnn t", 8, 9, Colors.BEIGEBG)
|
||
|
# #cli.draw()
|
||
|
#
|
||
|
# while True:
|
||
|
# cli.draw()
|
||
|
# sleep(1)
|