|
| 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 | +# Distributed 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] |
| 68 | +OSC: Final[str] |
| 69 | +BEL: Final[str] |
| 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 | +def set_title(title: str) -> str: ... |
| 78 | +def clear_screen(mode: int = ...) -> str: ... |
| 79 | +def clear_line(mode: int = ...) -> str: ... |
| 80 | + |
| 81 | + |
| 82 | +def strip_ansi(value: str) -> str: ... |
| 83 | + |
| 84 | + |
| 85 | +class Colour(str): |
| 86 | + style: str |
| 87 | + reset: str |
| 88 | + stack: List[str] |
| 89 | + |
| 90 | + def __new__(cls, style: str, stack: List[str], reset: str) -> "Colour": ... |
| 91 | + def __enter__(self) -> None: ... |
| 92 | + def __exit__(self, exc_type, exc_val, exc_tb) -> None: ... |
| 93 | + def __call__(self, text) -> str: ... |
| 94 | + |
| 95 | + |
| 96 | +class AnsiCodes(ABC): |
| 97 | + _stack: List[str] |
| 98 | + _reset: str |
| 99 | + |
| 100 | + def __init__(self) -> None: ... |
| 101 | + |
| 102 | + |
| 103 | +class AnsiCursor: |
| 104 | + |
| 105 | + def UP(self, n: int = ...) -> str: ... |
| 106 | + def DOWN(self, n: int = ...) -> str: ... |
| 107 | + def FORWARD(self, n: int = ...) -> str: ... |
| 108 | + def BACK(self, n: int = ...) -> str: ... |
| 109 | + def POS(self, x: int = ..., y: int = ...) -> str: ... |
| 110 | + |
| 111 | + |
| 112 | +class AnsiFore(AnsiCodes): |
| 113 | + |
| 114 | + _stack = fore_stack |
| 115 | + _reset: str = ... |
| 116 | + |
| 117 | + BLACK: Colour |
| 118 | + RED: Colour |
| 119 | + GREEN: Colour |
| 120 | + YELLOW: Colour |
| 121 | + BLUE: Colour |
| 122 | + MAGENTA: Colour |
| 123 | + CYAN: Colour |
| 124 | + WHITE: Colour |
| 125 | + RESET: Colour |
| 126 | + |
| 127 | + # These are fairly well supported, but not part of the standard. |
| 128 | + LIGHTBLACK_EX: Colour |
| 129 | + LIGHTRED_EX: Colour |
| 130 | + LIGHTGREEN_EX: Colour |
| 131 | + LIGHTYELLOW_EX: Colour |
| 132 | + LIGHTBLUE_EX: Colour |
| 133 | + LIGHTMAGENTA_EX: Colour |
| 134 | + LIGHTCYAN_EX: Colour |
| 135 | + LIGHTWHITE_EX: Colour |
| 136 | + |
| 137 | + |
| 138 | +class AnsiBack(AnsiCodes): |
| 139 | + |
| 140 | + _stack: List[str] |
| 141 | + _reset: str |
| 142 | + |
| 143 | + BLACK: Colour |
| 144 | + RED: Colour |
| 145 | + GREEN: Colour |
| 146 | + YELLOW: Colour |
| 147 | + BLUE: Colour |
| 148 | + MAGENTA: Colour |
| 149 | + CYAN: Colour |
| 150 | + WHITE: Colour |
| 151 | + RESET: Colour |
| 152 | + |
| 153 | + # These are fairly well supported, but not part of the standard. |
| 154 | + LIGHTBLACK_EX: Colour |
| 155 | + LIGHTRED_EX: Colour |
| 156 | + LIGHTGREEN_EX: Colour |
| 157 | + LIGHTYELLOW_EX: Colour |
| 158 | + LIGHTBLUE_EX: Colour |
| 159 | + LIGHTMAGENTA_EX: Colour |
| 160 | + LIGHTCYAN_EX: Colour |
| 161 | + LIGHTWHITE_EX: Colour |
| 162 | + |
| 163 | + |
| 164 | +class AnsiStyle(AnsiCodes): |
| 165 | + |
| 166 | + _stack: List[str] |
| 167 | + _reset: str |
| 168 | + |
| 169 | + BRIGHT: Colour |
| 170 | + DIM: Colour |
| 171 | + NORMAL: Colour |
| 172 | + RESET_ALL: Colour |
| 173 | + |
| 174 | + |
| 175 | +Fore = AnsiFore() |
| 176 | +Back = AnsiBack() |
| 177 | +Style = AnsiStyle() |
| 178 | +Cursor = AnsiCursor() |
0 commit comments