Skip to content

Commit ba83660

Browse files
authored
Improve diff readability and color formatting (#7)
1 parent fbd15dc commit ba83660

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ runs:
3232
- ${{ inputs.config }}
3333
- --exclude
3434
- ${{ inputs.exclude }}
35+
- --color
36+
- always
3537
- ${{ inputs.source }}

cmake-format/output.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class ColorCodes:
88
RED = "\x1b[31m"
99
GREEN = "\x1b[32m"
1010
CYAN = "\x1b[36m"
11+
YELLOW = "\x1b[33m"
1112

1213
def print_trouble(message: str, use_colors: bool = False) -> None:
1314
"""Print an error message to stderr.
@@ -32,10 +33,19 @@ def print_diff(diff_lines: List[str], use_color: bool = False) -> None:
3233
if use_color:
3334
diff_lines = list(colorize(diff_lines))
3435

36+
if diff_lines:
37+
header = "\nRequired formatting changes:\n" + "=" * 50 + "\n"
38+
if use_color:
39+
header = f"{ColorCodes.YELLOW}{header}{ColorCodes.RESET}"
40+
sys.stdout.write(header)
41+
3542
if sys.version_info[0] < 3:
3643
sys.stdout.writelines((line.encode("utf-8") for line in diff_lines))
3744
else:
3845
sys.stdout.writelines(diff_lines)
46+
47+
if diff_lines:
48+
sys.stdout.write("\n")
3949

4050
def colorize(diff_lines: List[str]) -> Iterator[str]:
4151
"""Add ANSI color codes to diff lines.
@@ -54,12 +64,12 @@ def colorize(diff_lines: List[str]) -> Iterator[str]:
5464
"""
5565
for line in diff_lines:
5666
if line.startswith(("--- ", "+++ ")):
57-
yield f"{ColorCodes.BOLD}{line}{ColorCodes.RESET}"
58-
elif line.startswith("@@ "):
59-
yield f"{ColorCodes.CYAN}{line}{ColorCodes.RESET}"
67+
yield f"{ColorCodes.YELLOW}{ColorCodes.BOLD}{line}{ColorCodes.RESET}\n"
68+
elif line.startswith("@@"):
69+
yield f"{ColorCodes.CYAN}{line}{ColorCodes.RESET}\n"
6070
elif line.startswith("+"):
61-
yield f"{ColorCodes.GREEN}{line}{ColorCodes.RESET}\n"
71+
yield f"{ColorCodes.GREEN}{line.rstrip()}{ColorCodes.RESET}\n"
6272
elif line.startswith("-"):
63-
yield f"{ColorCodes.RED}{line}{ColorCodes.RESET}\n"
73+
yield f"{ColorCodes.RED}{line.rstrip()}{ColorCodes.RESET}\n"
6474
else:
6575
yield f"{line}\n"

0 commit comments

Comments
 (0)