import os from time import sleep from typing import Dict from colors import Colors class Layer: def __init__(self, x, y): self.screen = {} 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() 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 def combine_layers(self): layers = self.get_layers() 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)] 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 += " " content += "\n" print("\033[H\033[J", end="") print(content) 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)