Skip to content

Commit b428eea

Browse files
dhatureTailung42
andauthored
feat(console): add color to standard output and standard error (#86)
* feat(console): add color to standard output and standard error * test: write updated test for console.py --------- Co-authored-by: Tailung42 <tailung42@gmail.com>
1 parent b475c54 commit b428eea

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/commitlint/console.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,24 @@
33
44
NOTE: If any future changes are made to the output implementation,
55
they will be done from here.
6-
7-
TODO: Add color on success and error (#5).
86
"""
97

108
import sys
119

1210
from .config import config
1311

12+
GREEN = "\033[92m"
13+
RED = "\033[91m"
14+
RESET = "\033[0m"
15+
16+
17+
def green(text: str) -> str:
18+
return f"{GREEN}{text}{RESET}"
19+
20+
21+
def red(text: str) -> str:
22+
return f"{RED}{text}{RESET}"
23+
1424

1525
def success(message: str) -> None:
1626
"""
@@ -22,7 +32,7 @@ def success(message: str) -> None:
2232
if config.quiet:
2333
return
2434

25-
sys.stdout.write(f"{message}\n")
35+
sys.stdout.write(f"{green(message)}\n")
2636

2737

2838
def error(message: str) -> None:
@@ -35,7 +45,7 @@ def error(message: str) -> None:
3545
if config.quiet:
3646
return
3747

38-
sys.stderr.write(f"{message}\n")
48+
sys.stderr.write(f"{red(message)}\n")
3949

4050

4151
def verbose(message: str) -> None:

tests/test_console.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
def test_success(mock_stdout: MagicMock, _mock_config: MagicMock):
1212
message = "Success message"
1313
console.success(message)
14-
mock_stdout.write.assert_called_once_with(f"{message}\n")
14+
mock_stdout.write.assert_called_once_with(f"{console.green(message)}\n")
1515

1616

1717
@patch("commitlint.console.config", quiet=True)
@@ -27,7 +27,7 @@ def test_success_for_quiet(mock_stdout: MagicMock, _mock_config: MagicMock):
2727
def test_error(mock_stderr: MagicMock, _mock_config: MagicMock):
2828
message = "Error message"
2929
console.error(message)
30-
mock_stderr.write.assert_called_once_with(f"{message}\n")
30+
mock_stderr.write.assert_called_once_with(f"{console.red(message)}\n")
3131

3232

3333
@patch("commitlint.console.config", quiet=True)

0 commit comments

Comments
 (0)