Skip to content

Commit 6d66cf1

Browse files
committed
feat(output): color is optional, on by default
1 parent f2c0be1 commit 6d66cf1

File tree

2 files changed

+121
-36
lines changed

2 files changed

+121
-36
lines changed

conventional_pre_commit/output.py

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,55 @@ class Colors:
1010
RESTORE = "\033[0m"
1111
YELLOW = "\033[00;33m"
1212

13+
def __init__(self, enabled=True):
14+
self.enabled = enabled
1315

14-
def fail(commit_msg):
16+
@property
17+
def blue(self):
18+
return self.LBLUE if self.enabled else ""
19+
20+
@property
21+
def red(self):
22+
return self.LRED if self.enabled else ""
23+
24+
@property
25+
def restore(self):
26+
return self.RESTORE if self.enabled else ""
27+
28+
@property
29+
def yellow(self):
30+
return self.YELLOW if self.enabled else ""
31+
32+
33+
def fail(commit_msg, use_color=True):
34+
c = Colors(use_color)
1535
lines = [
16-
f"{Colors.LRED}[Bad commit message] >>{Colors.RESTORE} {commit_msg}"
17-
f"{Colors.YELLOW}Your commit message does not follow Conventional Commits formatting{Colors.RESTORE}",
18-
f"{Colors.LBLUE}https://www.conventionalcommits.org/{Colors.RESTORE}",
36+
f"{c.red}[Bad commit message] >>{c.restore} {commit_msg}"
37+
f"{c.yellow}Your commit message does not follow Conventional Commits formatting{c.restore}",
38+
f"{c.blue}https://www.conventionalcommits.org/{c.restore}",
1939
]
2040
return os.linesep.join(lines)
2141

2242

23-
def verbose_arg():
43+
def verbose_arg(use_color=True):
44+
c = Colors(use_color)
2445
lines = [
2546
"",
26-
f"{Colors.YELLOW}Use the {Colors.RESTORE}--verbose{Colors.YELLOW} arg for more information{Colors.RESTORE}",
47+
f"{c.yellow}Use the {c.restore}--verbose{c.yellow} arg for more information{c.restore}",
2748
]
2849
return os.linesep.join(lines)
2950

3051

31-
def fail_verbose(commit_msg: str, types=format.DEFAULT_TYPES, optional_scope=True, scopes: Optional[List[str]] = None):
52+
def fail_verbose(
53+
commit_msg: str, types=format.DEFAULT_TYPES, optional_scope=True, scopes: Optional[List[str]] = None, use_color=True
54+
):
55+
c = Colors(use_color)
3256
match = format.conventional_match(commit_msg, types, optional_scope, scopes)
3357
lines = [
3458
"",
35-
f"{Colors.YELLOW}Conventional Commit messages follow a pattern like:",
59+
f"{c.yellow}Conventional Commit messages follow a pattern like:",
3660
"",
37-
f"{Colors.RESTORE} type(scope): subject",
61+
f"{c.restore} type(scope): subject",
3862
"",
3963
" extended body",
4064
"",
@@ -51,35 +75,37 @@ def fail_verbose(commit_msg: str, types=format.DEFAULT_TYPES, optional_scope=Tru
5175
groups.pop("sep", None)
5276

5377
if groups.keys():
54-
lines.append(f"{Colors.YELLOW}Please correct the following errors:{Colors.RESTORE}")
78+
lines.append(f"{c.yellow}Please correct the following errors:{c.restore}")
5579
lines.append("")
5680
for group in [g for g, v in groups.items() if not v]:
5781
if group == "scope":
5882
if scopes:
59-
lines.append(f" - Expected value for 'scope' from: {','.join(scopes)}")
83+
scopt_opts = f"{c.yellow},{c.restore}".join(scopes)
84+
lines.append(f"{c.yellow} - Expected value for {c.restore}scope{c.yellow} from: {c.restore}{scopt_opts}")
6085
else:
61-
lines.append(" - Expected value for 'scope' but found none.")
86+
lines.append(f"{c.yellow} - Expected value for {c.restore}scope{c.yellow} but found none.{c.restore}")
6287
else:
63-
lines.append(f" - Expected value for '{group}' but found none.")
88+
lines.append(f"{c.yellow} - Expected value for {c.restore}{group}{c.yellow} but found none.{c.restore}")
6489

6590
lines.extend(
6691
[
6792
"",
68-
f"{Colors.YELLOW}Run:{Colors.RESTORE}",
93+
f"{c.yellow}Run:{c.restore}",
6994
"",
7095
" git commit --edit --file=.git/COMMIT_EDITMSG",
7196
"",
72-
f"{Colors.YELLOW}to edit the commit message and retry the commit.{Colors.RESTORE}",
97+
f"{c.yellow}to edit the commit message and retry the commit.{c.restore}",
7398
]
7499
)
75100
return os.linesep.join(lines)
76101

77102

78-
def unicode_decode_error():
103+
def unicode_decode_error(use_color=True):
104+
c = Colors(use_color)
79105
return f"""
80-
{Colors.LRED}[Bad commit message encoding]{Colors.RESTORE}
106+
{c.red}[Bad commit message encoding]{c.restore}
81107
82-
{Colors.YELLOW}conventional-pre-commit couldn't decode your commit message.
108+
{c.yellow}conventional-pre-commit couldn't decode your commit message.
83109
UTF-8 encoding is assumed, please configure git to write commit messages in UTF-8.
84-
See {Colors.LBLUE}https://git-scm.com/docs/git-commit/#_discussion{Colors.YELLOW} for more.{Colors.RESTORE}
110+
See {c.blue}https://git-scm.com/docs/git-commit/#_discussion{c.yellow} for more.{c.restore}
85111
"""

tests/test_output.py

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,121 @@
11
import os
2-
from conventional_pre_commit.output import fail, fail_verbose, unicode_decode_error
2+
from conventional_pre_commit.output import Colors, fail, fail_verbose, unicode_decode_error
3+
4+
5+
def test_colors():
6+
colors = Colors()
7+
8+
assert colors.blue == colors.LBLUE
9+
assert colors.red == colors.LRED
10+
assert colors.restore == colors.RESTORE
11+
assert colors.yellow == colors.YELLOW
12+
13+
colors = Colors(enabled=False)
14+
15+
assert colors.blue == ""
16+
assert colors.red == ""
17+
assert colors.restore == ""
18+
assert colors.yellow == ""
319

420

521
def test_fail():
622
output = fail("commit msg")
723

24+
assert Colors.LRED in output
25+
assert Colors.YELLOW in output
26+
assert Colors.LBLUE in output
27+
assert Colors.RESTORE in output
28+
829
assert "Bad commit message" in output
930
assert "commit msg" in output
1031
assert "Conventional Commits formatting" in output
1132
assert "https://www.conventionalcommits.org/" in output
1233

1334

35+
def test_fail__no_color():
36+
output = fail("commit msg", use_color=False)
37+
38+
assert Colors.LRED not in output
39+
assert Colors.YELLOW not in output
40+
assert Colors.LBLUE not in output
41+
assert Colors.RESTORE not in output
42+
43+
1444
def test_fail_verbose():
1545
output = fail_verbose("commit msg", optional_scope=False)
1646

47+
assert Colors.YELLOW in output
48+
assert Colors.RESTORE in output
49+
50+
output = output.replace(Colors.YELLOW, Colors.RESTORE).replace(Colors.RESTORE, "")
51+
1752
assert "Conventional Commit messages follow a pattern like" in output
1853
assert f"type(scope): subject{os.linesep}{os.linesep} extended body" in output
19-
assert "Expected value for 'type' but found none." in output
20-
assert "Expected value for 'delim' but found none." in output
21-
assert "Expected value for 'scope' but found none." in output
22-
assert "Expected value for 'subject' but found none." in output
54+
assert "Expected value for type but found none." in output
55+
assert "Expected value for delim but found none." in output
56+
assert "Expected value for scope but found none." in output
57+
assert "Expected value for subject but found none." in output
2358
assert "git commit --edit --file=.git/COMMIT_EDITMSG" in output
2459
assert "edit the commit message and retry the commit" in output
2560

2661

62+
def test_fail_verbose__no_color():
63+
output = fail_verbose("commit msg", use_color=False)
64+
65+
assert Colors.LRED not in output
66+
assert Colors.YELLOW not in output
67+
assert Colors.LBLUE not in output
68+
assert Colors.RESTORE not in output
69+
70+
2771
def test_fail_verbose__optional_scope():
28-
output = fail_verbose("commit msg", optional_scope=True)
72+
output = fail_verbose("commit msg", optional_scope=True, use_color=False)
2973

30-
assert "Expected value for 'scope' but found none." not in output
74+
assert "Expected value for scope but found none." not in output
3175

3276

3377
def test_fail_verbose__missing_subject():
34-
output = fail_verbose("feat(scope):", optional_scope=False)
78+
output = fail_verbose("feat(scope):", optional_scope=False, use_color=False)
3579

36-
assert "Expected value for 'subject' but found none." in output
37-
assert "Expected value for 'type' but found none." not in output
38-
assert "Expected value for 'scope' but found none." not in output
80+
assert "Expected value for subject but found none." in output
81+
assert "Expected value for type but found none." not in output
82+
assert "Expected value for scope but found none." not in output
3983

4084

41-
def test_fail_verbose_no_body_sep():
85+
def test_fail_verbose__no_body_sep():
4286
output = fail_verbose(
4387
"""feat(scope): subject
4488
body without blank line
4589
""",
4690
optional_scope=False,
91+
use_color=False,
4792
)
4893

49-
assert "Expected value for 'sep' but found none." in output
50-
assert "Expected value for 'multi' but found none." not in output
94+
assert "Expected value for sep but found none." in output
95+
assert "Expected value for multi but found none." not in output
5196

52-
assert "Expected value for 'subject' but found none." not in output
53-
assert "Expected value for 'type' but found none." not in output
54-
assert "Expected value for 'scope' but found none." not in output
97+
assert "Expected value for subject but found none." not in output
98+
assert "Expected value for type but found none." not in output
99+
assert "Expected value for scope but found none." not in output
55100

56101

57102
def test_unicode_decode_error():
58103
output = unicode_decode_error()
59104

105+
assert Colors.LRED in output
106+
assert Colors.YELLOW in output
107+
assert Colors.LBLUE in output
108+
assert Colors.RESTORE in output
109+
60110
assert "Bad commit message encoding" in output
61111
assert "UTF-8 encoding is assumed" in output
62112
assert "https://git-scm.com/docs/git-commit/#_discussion" in output
113+
114+
115+
def test_unicode_decode_error__no_color():
116+
output = unicode_decode_error(use_color=False)
117+
118+
assert Colors.LRED not in output
119+
assert Colors.YELLOW not in output
120+
assert Colors.LBLUE not in output
121+
assert Colors.RESTORE not in output

0 commit comments

Comments
 (0)