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.

82 lines
2.4 KiB

import os
from time import sleep
from typing import Dict
from colors import Colors
class Layer:
def __init__(self, x, y):
self.screen = {}
2 years ago
self.x = x #largeur
self.y = y #hauteur
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
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):
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
def draw(self):
cli.combine_layers()
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:
content += "a"
content += "\n"
print("\033[H\033[J", end="")
print(content)
cli = CLI()
2 years ago
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()
2 years ago
sleep(1)