Don't use ANSI color codes when not desired

Which is when not supported by terminal or when not in a tty.
This commit is contained in:
Ivan Habunek 2019-09-05 22:35:29 +02:00
parent 1d3ff87ffa
commit 367ced2eef
No known key found for this signature in database
GPG key ID: CDBD63C43A30BB95
2 changed files with 24 additions and 2 deletions

View file

@ -2,6 +2,7 @@
0.24.0:
date: TBA
changes:
- "CLI: Don't use ANSI colors if not supported by terminal or when not in a tty"
- "TUI: Implement deleting own status messages"
0.23.1:

View file

@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
import sys
import os
import re
import sys
from datetime import datetime
from textwrap import wrap
@ -46,7 +47,27 @@ def strip_tags(text):
return text
USE_ANSI_COLOR = "--no-color" not in sys.argv
def use_ansi_color():
"""Returns True if ANSI color codes should be used."""
# Windows doesn't support color unless ansicon is installed
# See: http://adoxa.altervista.org/ansicon/
if sys.platform == 'win32' and 'ANSICON' not in os.environ:
return False
# Don't show color if stdout is not a tty, e.g. if output is piped on
if not sys.stdout.isatty():
return False
# Don't show color if explicitly specified in options
if "--no-color" in sys.argv:
return False
return True
USE_ANSI_COLOR = use_ansi_color()
QUIET = "--quiet" in sys.argv