Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/colorlog/escape_codes.py: 78%

18 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:35 +0000

1""" 

2Generates a dictionary of ANSI escape codes. 

3 

4http://en.wikipedia.org/wiki/ANSI_escape_code 

5 

6Uses colorama as an optional dependency to support color on Windows 

7""" 

8import sys 

9 

10try: 

11 import colorama 

12except ImportError: 

13 pass 

14else: 

15 if sys.platform == "win32": 

16 colorama.init(strip=False) 

17 

18__all__ = ("escape_codes", "parse_colors") 

19 

20 

21# Returns escape codes from format codes 

22def esc(*x): 

23 return "\033[" + ";".join(x) + "m" 

24 

25 

26# The initial list of escape codes 

27escape_codes = {"reset": esc("0"), "bold": esc("01"), "thin": esc("02")} 

28 

29# The color names 

30COLORS = ["black", "red", "green", "yellow", "blue", "purple", "cyan", "white"] 

31 

32PREFIXES = [ 

33 # Foreground without prefix 

34 ("3", ""), 

35 ("01;3", "bold_"), 

36 ("02;3", "thin_"), 

37 # Foreground with fg_ prefix 

38 ("3", "fg_"), 

39 ("01;3", "fg_bold_"), 

40 ("02;3", "fg_thin_"), 

41 # Background with bg_ prefix - bold/light works differently 

42 ("4", "bg_"), 

43 ("10", "bg_bold_"), 

44] 

45 

46for prefix, prefix_name in PREFIXES: 

47 for code, name in enumerate(COLORS): 

48 escape_codes[prefix_name + name] = esc(prefix + str(code)) 

49 

50 

51def parse_colors(sequence): 

52 """Return escape codes from a color sequence.""" 

53 return "".join(escape_codes[n] for n in sequence.split(",") if n)