|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# terminal_colours.py |
| 4 | +""" |
| 5 | +Functions for adding colours to terminal print statements. |
| 6 | +
|
| 7 | +This module generates ANSI character codes to printing colors to terminals. |
| 8 | +See: http://en.wikipedia.org/wiki/ANSI_escape_code |
| 9 | +""" |
| 10 | +# |
| 11 | +# Copyright © 2020 Dominic Davis-Foster <[email protected]> |
| 12 | +# |
| 13 | +# This program is free software; you can redistribute it and/or modify |
| 14 | +# it under the terms of the GNU Lesser General Public License as published by |
| 15 | +# the Free Software Foundation; either version 3 of the License, or |
| 16 | +# (at your option) any later version. |
| 17 | +# |
| 18 | +# This program is distributed in the hope that it will be useful, |
| 19 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | +# GNU Lesser General Public License for more details. |
| 22 | +# |
| 23 | +# You should have received a copy of the GNU Lesser General Public License |
| 24 | +# along with this program; if not, write to the Free Software |
| 25 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 26 | +# MA 02110-1301, USA. |
| 27 | +# |
| 28 | +# Based on colorama |
| 29 | +# https://github.com/tartley/colorama |
| 30 | +# Copyright Jonathan Hartley 2013 |
| 31 | +# Distrubuted under the BSD 3-Clause license. |
| 32 | +# | Redistribution and use in source and binary forms, with or without |
| 33 | +# | modification, are permitted provided that the following conditions are met: |
| 34 | +# | |
| 35 | +# | * Redistributions of source code must retain the above copyright notice, this |
| 36 | +# | list of conditions and the following disclaimer. |
| 37 | +# | |
| 38 | +# | * Redistributions in binary form must reproduce the above copyright notice, |
| 39 | +# | this list of conditions and the following disclaimer in the documentation |
| 40 | +# | and/or other materials provided with the distribution. |
| 41 | +# | |
| 42 | +# | * Neither the name of the copyright holders, nor those of its contributors |
| 43 | +# | may be used to endorse or promote products derived from this software without |
| 44 | +# | specific prior written permission. |
| 45 | +# | |
| 46 | +# | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 47 | +# | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 48 | +# | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 49 | +# | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 50 | +# | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 51 | +# | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 52 | +# | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 53 | +# | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 54 | +# | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 55 | +# | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 56 | +# |
| 57 | +# Includes modifications to colorama made by Bram Geelen in |
| 58 | +# https://github.com/tartley/colorama/pull/141/files |
| 59 | + |
| 60 | +# stdlib |
| 61 | +from abc import ABC |
| 62 | +from typing import List |
| 63 | + |
| 64 | +# 3rd party |
| 65 | +from typing_extensions import Final |
| 66 | + |
| 67 | +CSI: Final[str] = '\033[' |
| 68 | +OSC: Final[str] = '\033]' |
| 69 | +BEL: Final[str] = '\a' |
| 70 | + |
| 71 | +fore_stack: List[str] = [] |
| 72 | +back_stack: List[str] = [] |
| 73 | +style_stack: List[str] = [] |
| 74 | + |
| 75 | + |
| 76 | +def code_to_chars(code) -> str: ... |
| 77 | + |
| 78 | +def set_title(title: str) -> str: ... |
| 79 | + |
| 80 | +def clear_screen(mode: int = 2) -> str: ... |
| 81 | + |
| 82 | +def clear_line(mode: int = 2) -> str: ... |
| 83 | + |
| 84 | + |
| 85 | +class Color(str): |
| 86 | + style: str |
| 87 | + reset: str |
| 88 | + stack: List[str] |
| 89 | + |
| 90 | + def __new__(cls, style: str, stack: List[str], reset: str) -> "Color": ... |
| 91 | + |
| 92 | + def __enter__(self) -> None: ... |
| 93 | + |
| 94 | + def __exit__(self, exc_type, exc_val, exc_tb) -> None: ... |
| 95 | + |
| 96 | + def __call__(self, text) -> str: ... |
| 97 | + |
| 98 | + |
| 99 | +class AnsiCodes(ABC): |
| 100 | + _stack: List[str] |
| 101 | + _reset: str |
| 102 | + |
| 103 | + def __init__(self) -> None: ... |
| 104 | + |
| 105 | + |
| 106 | +class AnsiCursor: |
| 107 | + |
| 108 | + def UP(self, n: int = 1) -> str: ... |
| 109 | + |
| 110 | + def DOWN(self, n: int = 1) -> str: ... |
| 111 | + |
| 112 | + def FORWARD(self, n: int = 1) -> str: ... |
| 113 | + |
| 114 | + def BACK(self, n: int = 1) -> str: ... |
| 115 | + |
| 116 | + def POS(self, x: int = 1, y: int = 1) -> str: ... |
| 117 | + |
| 118 | + |
| 119 | +class AnsiFore(AnsiCodes): |
| 120 | + |
| 121 | + _stack = fore_stack |
| 122 | + _reset = "\033[39m" |
| 123 | + |
| 124 | + BLACK: Color |
| 125 | + RED: Color |
| 126 | + GREEN: Color |
| 127 | + YELLOW: Color |
| 128 | + BLUE: Color |
| 129 | + MAGENTA: Color |
| 130 | + CYAN: Color |
| 131 | + WHITE: Color |
| 132 | + RESET: Color |
| 133 | + |
| 134 | + # These are fairly well supported, but not part of the standard. |
| 135 | + LIGHTBLACK_EX: Color |
| 136 | + LIGHTRED_EX: Color |
| 137 | + LIGHTGREEN_EX: Color |
| 138 | + LIGHTYELLOW_EX: Color |
| 139 | + LIGHTBLUE_EX: Color |
| 140 | + LIGHTMAGENTA_EX: Color |
| 141 | + LIGHTCYAN_EX: Color |
| 142 | + LIGHTWHITE_EX: Color |
| 143 | + |
| 144 | + |
| 145 | +class AnsiBack(AnsiCodes): |
| 146 | + |
| 147 | + _stack = back_stack |
| 148 | + _reset = "\033[49m" |
| 149 | + |
| 150 | + BLACK: Color |
| 151 | + RED: Color |
| 152 | + GREEN: Color |
| 153 | + YELLOW: Color |
| 154 | + BLUE: Color |
| 155 | + MAGENTA: Color |
| 156 | + CYAN: Color |
| 157 | + WHITE: Color |
| 158 | + RESET: Color |
| 159 | + |
| 160 | + # These are fairly well supported, but not part of the standard. |
| 161 | + LIGHTBLACK_EX: Color |
| 162 | + LIGHTRED_EX: Color |
| 163 | + LIGHTGREEN_EX: Color |
| 164 | + LIGHTYELLOW_EX: Color |
| 165 | + LIGHTBLUE_EX: Color |
| 166 | + LIGHTMAGENTA_EX: Color |
| 167 | + LIGHTCYAN_EX: Color |
| 168 | + LIGHTWHITE_EX: Color |
| 169 | + |
| 170 | + |
| 171 | +class AnsiStyle(AnsiCodes): |
| 172 | + |
| 173 | + _stack = style_stack |
| 174 | + _reset = "\033[22m" |
| 175 | + |
| 176 | + BRIGHT: Color |
| 177 | + DIM: Color |
| 178 | + NORMAL: Color |
| 179 | + RESET_ALL: Color |
| 180 | + |
| 181 | + |
| 182 | +Fore = AnsiFore() |
| 183 | +Back = AnsiBack() |
| 184 | +Style = AnsiStyle() |
| 185 | +Cursor = AnsiCursor() |
| 186 | + |
| 187 | +fore_stack.append(Fore.RESET) |
| 188 | +back_stack.append(Back.RESET) |
| 189 | +style_stack.append(Style.NORMAL) |
0 commit comments