# -*- coding: utf-8 -*-1# This program is free software; you can redistribute it and/or modify2# it under the terms of the GNU General Public License as published by3# the Free Software Foundation; either version 2 of the License, or4# (at your option) any later version.5#6# This program is distributed in the hope that it will be useful,7# but WITHOUT ANY WARRANTY; without even the implied warranty of8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9# GNU General Public License for more details.10#11# You should have received a copy of the GNU General Public License12# along with this program; if not, write to the Free Software13# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,14# MA 02110-1301, USA.15#16# Author: Mauro Soria1718import re1920from colorama import init, Fore, Back, Style212223BACK_COLORS = {24"red": Back.RED,25"green": Back.GREEN,26"yellow": Back.YELLOW,27"blue": Back.BLUE,28"magenta": Back.MAGENTA,29"cyan": Back.CYAN,30"white": Back.WHITE,31"none": "",32}3334FORE_COLORS = {35"red": Fore.RED,36"green": Fore.GREEN,37"yellow": Fore.YELLOW,38"blue": Fore.BLUE,39"magenta": Fore.MAGENTA,40"cyan": Fore.CYAN,41"white": Fore.WHITE,42"none": "",43}4445STYLES = {46"bright": Style.BRIGHT,47"dim": Style.DIM,48"normal": ""49}5051# Credit: https://stackoverflow.com/a/1469378952_ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')5354init()555657def disable_color():58for style in STYLES:59STYLES[style] = STYLES["normal"]6061for table in (FORE_COLORS, BACK_COLORS):62for color in ("red", "green", "yellow", "blue", "magenta", "cyan", "white"):63table[color] = table["none"]646566def set_color(msg, fore="none", back="none", style="normal"):67msg = STYLES[style] + FORE_COLORS[fore] + BACK_COLORS[back] + msg68return msg + Style.RESET_ALL697071def clean_color(msg):72return _ansi_escape.sub("", msg)737475