|
1 | 1 | import click |
2 | 2 | import math |
3 | | -from typing import List |
| 3 | +from typing import List, Optional |
4 | 4 | from cli.printers.base_printer import BasePrinter |
5 | 5 | from cli.models import DocumentDetections, Detection, Document |
6 | 6 | from cli.config import config |
|
9 | 9 |
|
10 | 10 |
|
11 | 11 | class TextPrinter(BasePrinter): |
| 12 | + RED_COLOR_NAME = 'red' |
| 13 | + WHITE_COLOR_NAME = 'white' |
| 14 | + GREEN_COLOR_NAME = 'green' |
12 | 15 |
|
13 | 16 | def print_results(self, context: click.Context, results: List[DocumentDetections]): |
14 | 17 | if not results: |
@@ -89,32 +92,37 @@ def _print_line(self, document: Document, line: str, line_number: int): |
89 | 92 |
|
90 | 93 | def _get_detection_line_style(self, line: str, is_git_diff: bool, scan_type: str, start_position: int, length: int, |
91 | 94 | show_secret: bool = False): |
| 95 | + line_color = self._get_line_color(line, is_git_diff) |
92 | 96 | if scan_type != SECRET_SCAN_TYPE or start_position < 0 or length < 0: |
93 | | - return self._get_line_style(line, is_git_diff) |
| 97 | + return self._get_line_style(line, is_git_diff, line_color) |
94 | 98 |
|
95 | 99 | violation = line[start_position: start_position + length] |
96 | 100 | if not show_secret: |
97 | 101 | violation = obfuscate_text(violation) |
98 | 102 | line_to_violation = line[0: start_position] |
99 | 103 | line_from_violation = line[start_position + length:] |
100 | | - return f'{self._get_line_style(line_to_violation, is_git_diff)}' \ |
101 | | - f'{self._get_line_style(violation, is_git_diff, underline=True)}' \ |
102 | | - f'{self._get_line_style(line_from_violation, is_git_diff)}' |
| 104 | + return f'{self._get_line_style(line_to_violation, is_git_diff, line_color)}' \ |
| 105 | + f'{self._get_line_style(violation, is_git_diff, line_color, underline=True)}' \ |
| 106 | + f'{self._get_line_style(line_from_violation, is_git_diff, line_color)}' |
103 | 107 |
|
104 | | - def _get_line_style(self, line: str, is_git_diff: bool, underline: bool = False): |
| 108 | + def _get_line_style(self, line: str, is_git_diff: bool, color: Optional[str] = None, underline: bool = False): |
| 109 | + color = color or self._get_line_color(line, is_git_diff) |
| 110 | + return click.style(line, fg=color, bold=False, underline=underline) |
| 111 | + |
| 112 | + def _get_line_color(self, line: str, is_git_diff: bool): |
105 | 113 | if not is_git_diff: |
106 | | - return click.style(line, fg='white', bold=False, underline=underline) |
| 114 | + return self.WHITE_COLOR_NAME |
107 | 115 |
|
108 | 116 | if line.startswith('+'): |
109 | | - return click.style(line, fg='green', bold=False, underline=underline) |
| 117 | + return self.GREEN_COLOR_NAME |
110 | 118 |
|
111 | 119 | if line.startswith('-'): |
112 | | - return click.style(line, fg='red', bold=False, underline=underline) |
| 120 | + return self.RED_COLOR_NAME |
113 | 121 |
|
114 | | - return click.style(line, fg='white', bold=False, underline=underline) |
| 122 | + return self.WHITE_COLOR_NAME |
115 | 123 |
|
116 | 124 | def _get_position_in_line(self, text: str, position: int) -> int: |
117 | 125 | return position - text.rfind('\n', 0, position) - 1 |
118 | 126 |
|
119 | 127 | def _get_line_number_style(self, line_number: int): |
120 | | - return f'{click.style(str(line_number), fg="white", bold=False)} {click.style("|", fg="red", bold=False)}' |
| 128 | + return f'{click.style(str(line_number), fg=self.WHITE_COLOR_NAME, bold=False)} {click.style("|", fg=self.RED_COLOR_NAME, bold=False)}' |
0 commit comments