-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
MoessnerFabian(Group)
committed
Jan 29, 2025
1 parent
90a1265
commit 0293dbf
Showing
2 changed files
with
35 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,46 @@ | ||
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file. | ||
''' | ||
""" | ||
This module generates ANSI character codes to printing colors to terminals. | ||
See: http://en.wikipedia.org/wiki/ANSI_escape_code | ||
This module contains only a subset of the original colorama library. Additional codes can be added in this module from | ||
https://github.com/tartley/colorama/blob/master/colorama/ansi.py. | ||
''' | ||
""" | ||
|
||
CSI = "\033[" | ||
OSC = "\033]" | ||
BEL = "\a" | ||
|
||
CSI = '\033[' | ||
OSC = '\033]' | ||
BEL = '\a' | ||
|
||
def code_to_chars(code): | ||
return CSI + str(code) + 'm' | ||
return CSI + str(code) + "m" | ||
|
||
|
||
class AnsiCodes(object): | ||
def __init__(self): | ||
# the subclasses declare class attributes which are numbers. | ||
# Upon instantiation we define instance attributes, which are the same | ||
# as the class attributes but wrapped with the ANSI escape sequence | ||
for name in dir(self): | ||
if not name.startswith('_'): | ||
if not name.startswith("_"): | ||
value = getattr(self, name) | ||
setattr(self, name, code_to_chars(value)) | ||
|
||
|
||
class AnsiFore(AnsiCodes): | ||
BLACK = 30 | ||
RED = 31 | ||
GREEN = 32 | ||
YELLOW = 33 | ||
BLUE = 34 | ||
MAGENTA = 35 | ||
CYAN = 36 | ||
WHITE = 37 | ||
RESET = 39 | ||
BLACK = 30 | ||
RED = 31 | ||
GREEN = 32 | ||
YELLOW = 33 | ||
BLUE = 34 | ||
MAGENTA = 35 | ||
CYAN = 36 | ||
WHITE = 37 | ||
RESET = 39 | ||
|
||
|
||
class AnsiBack(AnsiCodes): | ||
RESET = 49 | ||
RESET = 49 | ||
|
||
|
||
Fore = AnsiFore() | ||
Back = AnsiBack() | ||
Fore = AnsiFore() | ||
Back = AnsiBack() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
from logprep.util.ansi import Fore, Back | ||
|
||
|
||
class TestAnsiCodes: | ||
def test_basic_color_codes(self): | ||
assert Fore.BLACK == '\033[30m' | ||
assert Fore.RED == '\033[31m' | ||
assert Fore.GREEN == '\033[32m' | ||
assert Fore.YELLOW == '\033[33m' | ||
assert Fore.BLUE == '\033[34m' | ||
assert Fore.MAGENTA == '\033[35m' | ||
assert Fore.CYAN == '\033[36m' | ||
assert Fore.WHITE == '\033[37m' | ||
assert Fore.RESET == '\033[39m' | ||
assert Back.RESET == '\033[49m' | ||
assert Fore.BLACK == "\033[30m" | ||
assert Fore.RED == "\033[31m" | ||
assert Fore.GREEN == "\033[32m" | ||
assert Fore.YELLOW == "\033[33m" | ||
assert Fore.BLUE == "\033[34m" | ||
assert Fore.MAGENTA == "\033[35m" | ||
assert Fore.CYAN == "\033[36m" | ||
assert Fore.WHITE == "\033[37m" | ||
assert Fore.RESET == "\033[39m" | ||
assert Back.RESET == "\033[49m" |