Skip to content

Commit d44f53e

Browse files
authored
CM-12031 - text results printer - fix line color bug (#13)
get line color before secret obfuscation
1 parent 120a06c commit d44f53e

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

cli/printers/text_printer.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import click
22
import math
3-
from typing import List
3+
from typing import List, Optional
44
from cli.printers.base_printer import BasePrinter
55
from cli.models import DocumentDetections, Detection, Document
66
from cli.config import config
@@ -9,6 +9,9 @@
99

1010

1111
class TextPrinter(BasePrinter):
12+
RED_COLOR_NAME = 'red'
13+
WHITE_COLOR_NAME = 'white'
14+
GREEN_COLOR_NAME = 'green'
1215

1316
def print_results(self, context: click.Context, results: List[DocumentDetections]):
1417
if not results:
@@ -89,32 +92,37 @@ def _print_line(self, document: Document, line: str, line_number: int):
8992

9093
def _get_detection_line_style(self, line: str, is_git_diff: bool, scan_type: str, start_position: int, length: int,
9194
show_secret: bool = False):
95+
line_color = self._get_line_color(line, is_git_diff)
9296
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)
9498

9599
violation = line[start_position: start_position + length]
96100
if not show_secret:
97101
violation = obfuscate_text(violation)
98102
line_to_violation = line[0: start_position]
99103
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)}'
103107

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):
105113
if not is_git_diff:
106-
return click.style(line, fg='white', bold=False, underline=underline)
114+
return self.WHITE_COLOR_NAME
107115

108116
if line.startswith('+'):
109-
return click.style(line, fg='green', bold=False, underline=underline)
117+
return self.GREEN_COLOR_NAME
110118

111119
if line.startswith('-'):
112-
return click.style(line, fg='red', bold=False, underline=underline)
120+
return self.RED_COLOR_NAME
113121

114-
return click.style(line, fg='white', bold=False, underline=underline)
122+
return self.WHITE_COLOR_NAME
115123

116124
def _get_position_in_line(self, text: str, position: int) -> int:
117125
return position - text.rfind('\n', 0, position) - 1
118126

119127
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

Comments
 (0)