From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754749AbcGEHry (ORCPT ); Tue, 5 Jul 2016 03:47:54 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:29151 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752109AbcGEHrx (ORCPT ); Tue, 5 Jul 2016 03:47:53 -0400 X-IBM-Helo: d23dlp02.au.ibm.com X-IBM-MailFrom: andrew.donnellan@au1.ibm.com X-IBM-RcptTo: linux-kernel@vger.kernel.org From: Andrew Donnellan To: linux-kernel@vger.kernel.org, gregkh@linuxfoundation.org, valentinrothberg@gmail.com, josh@joshtriplett.org Subject: [PATCH v2] checkkconfigsymbols.py: add --no-color option, don't print color to non-TTY Date: Tue, 5 Jul 2016 17:47:37 +1000 X-Mailer: git-send-email 2.8.1 In-Reply-To: <1467621570-970-1-git-send-email-andrew.donnellan@au1.ibm.com> References: <1467621570-970-1-git-send-email-andrew.donnellan@au1.ibm.com> X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16070507-1617-0000-0000-00000135F09D X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 16070507-1618-0000-0000-00004635E5A7 Message-Id: <1467704857-831-1-git-send-email-andrew.donnellan@au1.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2016-07-05_03:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1604210000 definitions=main-1607050072 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Only print the ANSI colour escape codes if stdout is a TTY. Useful if redirecting output to a file or piping to another script. Also add a new option, --no-color, if the user wants to disable colour output for whatever reason. Signed-off-by: Andrew Donnellan --- V1->V2: - automatically detect non-TTYs and disable colour output. Suggested by Josh Triplett --- scripts/checkkconfigsymbols.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/scripts/checkkconfigsymbols.py b/scripts/checkkconfigsymbols.py index df643f6..b140fc9 100755 --- a/scripts/checkkconfigsymbols.py +++ b/scripts/checkkconfigsymbols.py @@ -82,6 +82,11 @@ def parse_options(): default=False, help="Reset current Git tree even when it's dirty.") + parser.add_option('', '--no-color', dest='color', action='store_false', + default=True, + help="Don't print colored output. Default when not " + "outputting to a terminal.") + (opts, _) = parser.parse_args() if opts.commit and opts.diff: @@ -116,6 +121,9 @@ def main(): """Main function of this module.""" opts = parse_options() + global color + color = opts.color and sys.stdout.isatty() + if opts.sim and not opts.commit and not opts.diff: sims = find_sims(opts.sim, opts.ignore) if sims: @@ -202,14 +210,14 @@ def yel(string): """ Color %string yellow. """ - return "\033[33m%s\033[0m" % string + return "\033[33m%s\033[0m" % string if color else string def red(string): """ Color %string red. """ - return "\033[31m%s\033[0m" % string + return "\033[31m%s\033[0m" % string if color else string def execute(cmd): -- Andrew Donnellan OzLabs, ADL Canberra andrew.donnellan@au1.ibm.com IBM Australia Limited