Подпишитесь на наш Telegram-канал, чтобы всегда быть в курсе важных обновлений! Перейти

Окрас терминала

  • Автор темы Автор темы mafia007
  • Дата начала Дата начала
Государственная служба РФ
Пользователь
Пользователь
Статус
Оффлайн
Регистрация
26 Дек 2018
Сообщения
363
Реакции
70
есть такой код:
Python:
Expand Collapse Copy
# !  API:
# ?      1. (str) - text: your any text
# ?      2. (str) - color: none (color off), black, red, green, yellow, blue, purple, cyan, white.
# ?      3. (list) - settings_print: none (settings off), bold, faded, italic, underline, s-blink (slow-blinked), f-blink (fast-blinked), change back (changed background on selected color).
# ?      4. (int) - bright: 1 (on), 0 (off).

# todo: term('Hello world!', 'cyan', ['italic', 'underline', 'bold'], 0)


def term(text: str, color: str, settings_print: list, bright: int):
    colors_correct = settings_correct = True
    term_settings = setup_error = ''

    colors = {
        'none': '[0',
        'black': '[30',
        'red': '[31',
        'green': '[32',
        'yellow': '[33',
        'blue': '[34',
        'purple': '[35',
        'cyan': '[36',
        'white': '[37'
    }

    settings = {
        'none': '\033[0m',
        'bold': '\033[1m',
        'faded': '\033[2m',
        'italic': '\033[3m',
        'underline': '\033[4m',
        's-blink': '\033[5m',
        'f-blink': '\033[6m',
        'change back': '\033[7m'
    }


    if color in colors.keys():
        colors.get(color)
    else:
        colors_correct = False
        print('\nColor term:', term('this color is not in the data.', 'red', ['change back', 'italic', 'bold'], 0))

    for i in settings_print:
        if i in settings.keys():
            term_settings += settings.get(i)
        else:
            if 'none' in settings_print:
                settings_correct = True
            else:
                settings_correct = False
                setup_error += ' [' + i + ']'

    if settings_correct == False:
        print('\nColor term:', term(f'these settings are not contained in the data -{setup_error}', 'red', ['change back', 'italic', 'bold'], 0))

    if colors_correct and settings_correct == True:
        if 'none' in settings_print and bright == 1:
            result = '\033' + colors.get(color) + ';1m{}\033[0m'
        elif 'none' in settings_print and bright == 0:
            result = '\033' + colors.get(color) + 'm{}\033[0m'
        elif not 'none' in settings_print and bright == 1:
            result = '\033' + colors.get(color) + ';1m' + term_settings + '{}\033[0m'
        elif not 'none' in settings_print and bright == 0:
            result = '\033' + colors.get(color) + 'm' + term_settings + '{}\033[0m'
        elif bright != 0 or 1:
            result = ''
            print('Color term:', term('invalid value in bright.', 'red', ['change back', 'italic', 'bold'], 0))

        return result.format(text)

    else:
        return ''


я хотел бы сократить вот это:
Python:
Expand Collapse Copy
    if colors_correct and settings_correct == True:
        if 'none' in settings_print and bright == 1:
            result = '\033' + colors.get(color) + ';1m{}\033[0m'
        elif 'none' in settings_print and bright == 0:
            result = '\033' + colors.get(color) + 'm{}\033[0m'
        elif not 'none' in settings_print and bright == 1:
            result = '\033' + colors.get(color) + ';1m' + term_settings + '{}\033[0m'
        elif not 'none' in settings_print and bright == 0:
            result = '\033' + colors.get(color) + 'm' + term_settings + '{}\033[0m'
        elif bright != 0 or 1:
            result = ''
            print('Color term:', term('invalid value in bright.', 'red', ['change back', 'italic', 'bold'], 0))
no kak?
 
Python:
Expand Collapse Copy
if colors_correct and settings_correct == True:
        if bright != 0 and bright != 1:
            result = ''
            print('Color term:', term('invalid value in bright.', 'red', ['change back', 'italic', 'bold'], 0))
        else:
            result = '\033' + colors.get(color) + (';1' if bright == 1 else '') + 'm' + (term_settings if not 'none' in settings_print else '') + '{}\033[0m'
 
Назад
Сверху Снизу