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.

135 lines
4.0 KiB

import os
2 years ago
import platform
import shutil
2 years ago
""" ANSI color codes """
colors = {
'BLACK': "\033[0;30m",
'RED': "\033[0;31m",
'GREEN': "\033[0;32m",
'BROWN': "\033[0;33m",
'BLUE': "\033[0;34m",
'PURPLE': "\033[0;35m",
'CYAN': "\033[0;36m",
'LIGHT_GRAY': "\033[0;37m",
'DARK_GRAY': "\033[1;30m",
'LIGHT_RED': "\033[1;31m",
'LIGHT_GREEN': "\033[1;32m",
'YELLOW': "\033[1;33m",
'LIGHT_BLUE': "\033[1;34m",
'LIGHT_PURPLE': "\033[1;35m",
'LIGHT_CYAN': "\033[1;36m",
'LIGHT_WHITE': "\033[1;37m",
'END': "\033[0m"
2 years ago
}
weight = {
'BOLD': "\033[1m",
'FAINT': "\033[2m",
'ITALIC': "\033[3m",
'UNDERLINE': "\033[4m",
'END': "\033[0m"
}
2 years ago
class Cli():
"""Classe gérant l'interface graphique"""
def __init__(self, **kwargs):
self.width = shutil.get_terminal_size().columns
self.height = shutil.get_terminal_size().lines - 1
for key, value in kwargs.items():
if key == 'width':
self.width = value
elif key == 'height':
self.height = value-1
self.__set_dimensions()
self.screen = [[' ' for i in range(self.width)]
for j in range(self.height)]
def __clear(self):
if platform.system() == 'Linux':
os.system('clear')
else:
os.system('cls')
def __set_dimensions(self):
""" Défini les dimensions du terminal """
if platform.system() == 'Linux':
os.system('resize -s {} {}'.format(self.height + 1, self.width))
else:
os.system('mode con: cols={} lines={}'.format(
self.width, self.height + 1))
self.__clear()
def display(self):
"""affiche le contenu de self.screen"""
self.__clear()
for line in self.screen:
print(''.join(line))
def draw(self, content, x, y, **kwargs):
"""dessine aux coordonées"""
for i in range(len(str(content))):
try:
if content[i] != ' ':
self.screen[y][x+i] = content[i]
except(IndexError):
break
for key, value in kwargs.items():
if key == 'color':
try:
self.screen[y][x] = colors[value] + self.screen[y][x]
except:
pass
if key == 'weight':
try:
self.screen[y][x] = weight[value] + self.screen[y][x]
except:
pass
try:
self.screen[y][x+len(str(content))-1] += weight['END']
except:
pass
def draw_bar(self, percent, x, y, **kwargs):
"""dessine la barre de vie"""
length = 40
part_to_draw = length * percent // 100
for key, value in kwargs.items():
if key == 'color':
self.draw('|{}{}|'.format(''.join(['' for i in range(part_to_draw)]), ''.join(
[' ' for i in range(length - part_to_draw)])), x, y, color=value)
else:
self.draw('|{}{}|'.format(''.join(['' for i in range(part_to_draw)]), ''.join(
[' ' for i in range(length - part_to_draw)])), x, y, color='WHITE')
def wipe(self):
"""vide le contenu de self.screen"""
self.screen = [[' ' for i in range(self.width)]
for j in range(self.height)]
2 years ago
class Layer():
"""defini une zone indépendante dans l'écran pour ne pas tout recharger"""
def __init__(self, cli, x, y, shape):
"""shape doit être fournis sous forme de tableau"""
self.cli = cli
self.shape = shape
self.x = x
self.y = y
self.shape_width = max(map(len, self.shape))
self.shape_height = len(self.shape)
def refresh(self):
"""efface seulement le contenu du calque"""
for i in range(self.shape_height):
for j in range(self.shape_width):
try:
self.cli.screen[self.y+i][self.x+j] = ' '
except:
pass