Coverage for src/prints/colors.py: 100.00%
49 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-10-18 15:02 +0000
« prev ^ index » next coverage.py v7.15.4, created at 2026-10-18 15:02 +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# Components
16from ..system.platform import Platform
18# Colors class, pylint: disable=too-few-public-methods
19class Colors:
21 # Attributes
22 ALL: List[str] = []
23 BOLD = ''
24 CYAN = ''
25 GREEN = ''
26 GREY = ''
27 RED = ''
28 RESET = ''
29 YELLOW = ''
30 YELLOW_LIGHT = ''
32 # Symbols
33 ARROW = ''
35 # Enabled
36 @staticmethod
37 def enabled() -> bool:
39 # Result
40 try:
41 return bool(Colored('').enabled())
42 except (NameError, TypeError): # pragma: no cover
43 return False
45 # Prepare
46 @staticmethod
47 def prepare() -> None:
49 # Colors enabled
50 if Colors.enabled():
51 Colors.RESET = Colored('reset').attribute()
52 Colors.BOLD = Colors.RESET + Colored('bold').attribute()
53 Colors.CYAN = Colors.BOLD + Colored('cyan').foreground()
54 Colors.GREEN = Colors.BOLD + Colored('green').foreground()
55 Colors.GREY = Colors.BOLD + Colored('light_gray').foreground()
56 Colors.RED = Colors.BOLD + Colored('red').foreground()
57 Colors.YELLOW = Colors.BOLD + Colored('yellow').foreground()
58 Colors.YELLOW_LIGHT = Colors.BOLD + Colored('light_yellow').foreground()
59 Colors.ALL = [
60 Colors.CYAN,
61 Colors.GREEN,
62 Colors.GREY,
63 Colors.RED,
64 Colors.YELLOW,
65 Colors.YELLOW_LIGHT,
66 Colors.BOLD,
67 Colors.RESET,
68 ]
70 # Colors disabled
71 else:
72 Colors.BOLD = ''
73 Colors.CYAN = ''
74 Colors.GREEN = ''
75 Colors.GREY = ''
76 Colors.RED = ''
77 Colors.RESET = ''
78 Colors.YELLOW = ''
79 Colors.YELLOW_LIGHT = ''
80 Colors.ALL = []
82 # UTF8 symbols
83 if Platform.IS_TTY_UTF8:
84 Colors.ARROW = '‣'
86 # ASCII symbols
87 else:
88 Colors.ARROW = '>'
90 # Strip
91 @staticmethod
92 def strip(string: str) -> str:
94 # Strip all colors
95 for item in Colors.ALL:
96 string = string.replace(item, '')
98 # Result
99 return string