From patchwork Tue Dec 18 20:46:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 56 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 55C6260B23 for ; Tue, 18 Dec 2018 21:45:24 +0100 (CET) Received: from avalon.bb.dnainternet.fi (dfj612ybrt5fhg77mgycy-3.rev.dnainternet.fi [IPv6:2001:14ba:21f5:5b00:2e86:4862:ef6a:2804]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id C87CD53A for ; Tue, 18 Dec 2018 21:45:19 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1545165919; bh=0VoY01dXeq80F1bln/7FulDHCCh+T3RxzJFsWGDR1zg=; h=From:To:Subject:Date:From; b=VqwcOH3tJ4wHQluZl+Q8nnq+YyyNFsYwjV5UGC4KCT+um6g+XmVPYzUdglon9Q45u R0BGirRiTEgWJX8DiB3PeXPf9mmfpDLeLTmVpIukYlXoKOSO0DQXfWPe7mXgtSWcUm 6cMrsutoo5tjco1PQVwQanL47H8xsD+iVRd1EtpM= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Tue, 18 Dec 2018 22:46:08 +0200 Message-Id: <20181218204609.22902-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.19.2 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 1/2] utils: checkstyle.py: Highlight trailing white space at end of line X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 18 Dec 2018 20:45:24 -0000 In order to facilitate interpretation of diffs, highlight trailing white space at end of lines with a red background. Signed-off-by: Laurent Pinchart --- utils/checkstyle.py | 67 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 16 deletions(-) diff --git a/utils/checkstyle.py b/utils/checkstyle.py index b7c4d876a145..dadb55dfb8c1 100755 --- a/utils/checkstyle.py +++ b/utils/checkstyle.py @@ -40,18 +40,43 @@ source_extensions = ( class Colours: Default = 0 + Black = 0 Red = 31 Green = 32 + Yellow = 33 + Blue = 34 + Magenta = 35 Cyan = 36 + LightGrey = 37 + DarkGrey = 90 + LightRed = 91 + LightGreen = 92 + Lightyellow = 93 + LightBlue = 94 + LightMagenta = 95 + LightCyan = 96 + White = 97 + + @staticmethod + def fg(colour): + if sys.stdout.isatty(): + return '\033[%um' % colour + else: + return '' -for attr in Colours.__dict__.keys(): - if attr.startswith('_'): - continue + @staticmethod + def bg(colour): + if sys.stdout.isatty(): + return '\033[%um' % (colour + 10) + else: + return '' - if sys.stdout.isatty(): - setattr(Colours, attr, '\033[0;%um' % getattr(Colours, attr)) - else: - setattr(Colours, attr, '') + @staticmethod + def reset(): + if sys.stdout.isatty(): + return '\033[0m' + else: + return '' class DiffHunkSide(object): @@ -82,20 +107,30 @@ class DiffHunk(object): def __repr__(self): s = '%s@@ -%u,%u +%u,%u @@\n' % \ - (Colours.Cyan, + (Colours.fg(Colours.Cyan), self.__from.start, len(self.__from), self.__to.start, len(self.__to)) for line in self.lines: if line[0] == '-': - s += Colours.Red + s += Colours.fg(Colours.Red) elif line[0] == '+': - s += Colours.Green - else: - s += Colours.Default + s += Colours.fg(Colours.Green) + + if line[0] == '-': + spaces = 0 + for i in range(len(line)): + if line[-i-1].isspace(): + spaces += 1 + else: + break + spaces = len(line) - spaces + line = line[0:spaces] + Colours.bg(Colours.Red) + line[spaces:] + s += line + s += Colours.reset() + s += '\n' - s += Colours.Default return s def append(self, line): @@ -111,7 +146,7 @@ class DiffHunk(object): self.__to.touched.append(self.__to_line) self.__to_line += 1 - self.lines.append(line) + self.lines.append(line.rstrip('\n')) def intersects(self, lines): for line in lines: @@ -180,8 +215,8 @@ def check_file(top_level, commit, filename): if len(formatted_diff) == 0: return 0 - print('%s---' % Colours.Red, filename) - print('%s+++' % Colours.Green, filename) + print('%s---' % Colours.fg(Colours.Red), filename) + print('%s+++' % Colours.fg(Colours.Green), filename) for hunk in formatted_diff: print(hunk)