From 70fd97d360c94a8594100c0be999fc98d4cab374 Mon Sep 17 00:00:00 2001 From: Kalyax Date: Thu, 8 Sep 2022 15:46:01 +0200 Subject: [PATCH] cli + todo: layer --- cli/cli.py | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ cli/colors.py | 44 +++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 cli/cli.py create mode 100644 cli/colors.py diff --git a/cli/cli.py b/cli/cli.py new file mode 100644 index 0000000..49a7bbd --- /dev/null +++ b/cli/cli.py @@ -0,0 +1,78 @@ +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 + self.y = y + + 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[1] - 2 #hauteur + self.y = size[0] #largeur + 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() + #for layer in layers: + # for y in range(layer.x): + # for x in range(layer.y): + # if (x, y) in layer.screen: + # content += layer.screen[(x, y)] + # else: + # content += " " + # content += "\n" + + #def draw(self): + # print("\033[H\033[J", end="") + # content = "" + # for y in range(self.x): + # for x in range(self.y): + # if (x, y) in self.screen: + # content += self.screen[(x, y)] + # else: + # content += " " + # content += "\n" + # print(content) + + +cli = CLI() +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(0.1) \ No newline at end of file diff --git a/cli/colors.py b/cli/colors.py new file mode 100644 index 0000000..6b49415 --- /dev/null +++ b/cli/colors.py @@ -0,0 +1,44 @@ +class Colors: + RESET = '\33[0m' + BOLD = '\33[1m' + ITALIC = '\33[3m' + URL = '\33[4m' + BLINK = '\33[5m' + BLINK2 = '\33[6m' + SELECTED = '\33[7m' + + BLACK = '\33[30m' + RED = '\33[31m' + GREEN = '\33[32m' + YELLOW = '\33[33m' + BLUE = '\33[34m' + VIOLET = '\33[35m' + BEIGE = '\33[36m' + WHITE = '\33[37m' + + BLACKBG = '\33[40m' + REDBG = '\33[41m' + GREENBG = '\33[42m' + YELLOWBG = '\33[43m' + BLUEBG = '\33[44m' + VIOLETBG = '\33[45m' + BEIGEBG = '\33[46m' + WHITEBG = '\33[47m' + + GREY = '\33[90m' + RED2 = '\33[91m' + GREEN2 = '\33[92m' + YELLOW2 = '\33[93m' + BLUE2 = '\33[94m' + VIOLET2 = '\33[95m' + BEIGE2 = '\33[96m' + WHITE2 = '\33[97m' + + GREYBG = '\33[100m' + REDBG2 = '\33[101m' + GREENBG2 = '\33[102m' + YELLOWBG2 = '\33[103m' + BLUEBG2 = '\33[104m' + VIOLETBG2 = '\33[105m' + BEIGEBG2 = '\33[106m' + WHITEBG2 = '\33[107m' \ No newline at end of file