Skip to content

Commit 9e31a46

Browse files
committed
only output colors in diff if coloring is enabled and we are not being piped or redirected
1 parent 38457cc commit 9e31a46

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

gitless/cli/pprint.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,23 @@ def _format_line(diff_line, padding, bold_delim=None):
289289
"""Format a standard diff line.
290290
291291
Returns:
292-
a colored version of the diff line using ANSI control characters.
292+
a padded and colored version of the diff line with line numbers
293293
"""
294294
# Color constants
295-
GREEN = '\033[32m'
296-
GREEN_BOLD = '\033[1;32m'
297-
RED = '\033[31m'
298-
RED_BOLD = '\033[1;31m'
299-
CLEAR = '\033[0m'
295+
# We only output colored lines if the coloring is enabled and we are not being
296+
# piped or redirected
297+
if colored.DISABLE_COLOR or not sys.stdout.isatty():
298+
GREEN = ''
299+
GREEN_BOLD = ''
300+
RED = ''
301+
RED_BOLD = ''
302+
CLEAR = ''
303+
else:
304+
GREEN = '\033[32m'
305+
GREEN_BOLD = '\033[1;32m'
306+
RED = '\033[31m'
307+
RED_BOLD = '\033[1;31m'
308+
CLEAR = '\033[0m'
300309

301310
formatted = ''
302311
st = diff_line.origin

0 commit comments

Comments
 (0)