From 2e6e2c4fde2e4fec0ecf17f19f7549315b2b4016 Mon Sep 17 00:00:00 2001 From: Yuri D'Elia Date: Fri, 23 Apr 2021 12:26:15 +0200 Subject: [PATCH] Do not output colors when redirecting output --- lang/lang-check.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lang/lang-check.py b/lang/lang-check.py index 3ddbdc67d..c5bfbe9ab 100755 --- a/lang/lang-check.py +++ b/lang/lang-check.py @@ -15,12 +15,18 @@ """Check lang files.""" from argparse import ArgumentParser from traceback import print_exc -from sys import stderr +from sys import stdout, stderr import textwrap -red = lambda text: '\033[0;31m' + text + '\033[0m' -green = lambda text: '\033[0;32m' + text + '\033[0m' -yellow = lambda text: '\033[0;33m' + text + '\033[0m' +def color_maybe(color_attr, text): + if stdout.isatty(): + return '\033[0;' + str(color_attr) + 'm' + text + '\033[0m' + else: + return text + +red = lambda text: color_maybe(31, text) +green = lambda text: color_maybe(32, text) +yellow = lambda text: color_maybe(33, text) def parse_txt(lang, no_warning): """Parse txt file and check strings to display definition."""