Coverage for src/prints/colors.py: 100.00%
44 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-21 21:20 +0000
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-21 21:20 +0000
1#!/usr/bin/env python3
3# Standard libraries
4from typing import List
6# Modules libraries
7try:
8 try: # colored>=2.0.0
9 from colored import Colored
10 except ImportError: # colored<2.0.0 # pragma: no cover
11 from colored import colored as Colored # type: ignore[assignment]
12except ModuleNotFoundError: # pragma: no cover
13 pass
15# Colors class, pylint: disable=too-few-public-methods
16class Colors:
18 # Attributes
19 ALL: List[str] = []
20 BOLD = ''
21 CYAN = ''
22 GREEN = ''
23 GREY = ''
24 RED = ''
25 RESET = ''
26 YELLOW = ''
27 YELLOW_LIGHT = ''
29 # Enabled
30 @staticmethod
31 def enabled() -> bool:
33 # Result
34 try:
35 return bool(Colored('').enabled())
36 except (NameError, TypeError): # pragma: no cover
37 return False
39 # Prepare
40 @staticmethod
41 def prepare() -> None:
43 # Colors enabled
44 if Colors.enabled():
45 Colors.RESET = Colored('reset').attribute()
46 Colors.BOLD = Colors.RESET + Colored('bold').attribute()
47 Colors.CYAN = Colors.BOLD + Colored('cyan').foreground()
48 Colors.GREEN = Colors.BOLD + Colored('green').foreground()
49 Colors.GREY = Colors.BOLD + Colored('light_gray').foreground()
50 Colors.RED = Colors.BOLD + Colored('red').foreground()
51 Colors.YELLOW = Colors.BOLD + Colored('yellow').foreground()
52 Colors.YELLOW_LIGHT = Colors.BOLD + Colored('light_yellow').foreground()
53 Colors.ALL = [
54 Colors.CYAN,
55 Colors.GREEN,
56 Colors.GREY,
57 Colors.RED,
58 Colors.YELLOW,
59 Colors.YELLOW_LIGHT,
60 Colors.BOLD,
61 Colors.RESET,
62 ]
64 # Colors disabled
65 else:
66 Colors.BOLD = ''
67 Colors.CYAN = ''
68 Colors.GREEN = ''
69 Colors.GREY = ''
70 Colors.RED = ''
71 Colors.RESET = ''
72 Colors.YELLOW = ''
73 Colors.YELLOW_LIGHT = ''
74 Colors.ALL = []
76 # Strip
77 @staticmethod
78 def strip(string: str) -> str:
80 # Strip all colors
81 for item in Colors.ALL:
82 string = string.replace(item, '')
84 # Result
85 return string