Skip to content

Commit

Permalink
WIP9
Browse files Browse the repository at this point in the history
  • Loading branch information
MoessnerFabian(Group) committed Jan 29, 2025
1 parent 90a1265 commit 0293dbf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
43 changes: 24 additions & 19 deletions logprep/util/ansi.py
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()
21 changes: 11 additions & 10 deletions tests/unit/util/test_ansi_code.py
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"

0 comments on commit 0293dbf

Please sign in to comment.