Skip to content

Commit d329a8d

Browse files
authored
feat(output): allow forcing colored/coloured output (#218)
Fixes #182
1 parent 17d293d commit d329a8d

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

radon/cli/colors.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,23 @@
22
terminal.
33
'''
44

5+
import os
56
import sys
67

8+
9+
def color_enabled():
10+
COLOR_ENV = os.getenv('COLOR', 'auto')
11+
if COLOR_ENV == 'auto' and sys.stdout.isatty():
12+
return True
13+
if COLOR_ENV == 'yes':
14+
return True
15+
return False
16+
17+
718
try:
819
import colorama
920

10-
colorama.init(strip=(not sys.stdout.isatty()))
21+
colorama.init(strip=not color_enabled())
1122
GREEN, YELLOW, RED = (
1223
colorama.Fore.GREEN,
1324
colorama.Fore.YELLOW,

radon/tests/test_cli_colors.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import radon.cli.colors as colors
2+
3+
4+
def test_color_enabled_yes(monkeypatch):
5+
monkeypatch.setenv("COLOR", "yes")
6+
assert colors.color_enabled()
7+
8+
9+
def test_color_enabled_no(monkeypatch):
10+
monkeypatch.setenv("COLOR", "no")
11+
assert not colors.color_enabled()
12+
13+
14+
def test_color_enabled_auto(monkeypatch, mocker):
15+
monkeypatch.setenv("COLOR", "auto")
16+
isatty_mock = mocker.patch('sys.stdout.isatty')
17+
18+
isatty_mock.return_value = True
19+
assert colors.color_enabled()
20+
21+
isatty_mock.return_value = False
22+
assert not colors.color_enabled()

0 commit comments

Comments
 (0)