[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 06/35] checkpatch: colorize output to terminal
From: |
Paolo Bonzini |
Subject: |
[Qemu-devel] [PULL 06/35] checkpatch: colorize output to terminal |
Date: |
Tue, 18 Dec 2018 00:16:31 +0100 |
Add optional colors to make seeing message types a bit easier.
The default is to show them on a tty.
Inspired by Linux commits 57230297116fa ("checkpatch: colorize output
to terminal") and 737c0767758b ("checkpatch: change format of --color
argument to --color[=WHEN]").
Signed-off-by: Paolo Bonzini <address@hidden>
Reviewed-by: Philippe Mathieu-Daudé <address@hidden>
---
scripts/checkpatch.pl | 49 ++++++++++++++++++++++++++++++++++++-------
1 file changed, 42 insertions(+), 7 deletions(-)
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 2184a481ac..df2da09b06 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -7,6 +7,7 @@
use strict;
use warnings;
+use Term::ANSIColor qw(:constants);
my $P = $0;
$P =~ address@hidden/@@g;
@@ -26,6 +27,7 @@ my $tst_only;
my $emacs = 0;
my $terse = 0;
my $file = undef;
+my $color = "auto";
my $no_warnings = 0;
my $summary = 1;
my $mailback = 0;
@@ -64,6 +66,8 @@ Options:
is all off)
--test-only=WORD report only warnings/errors containing WORD
literally
+ --color[=WHEN] Use colors 'always', 'never', or only when output
+ is a terminal ('auto'). Default is 'auto'.
-h, --help, --version display this help and exit
When FILE is - read standard input.
@@ -72,6 +76,14 @@ EOM
exit($exitcode);
}
+# Perl's Getopt::Long allows options to take optional arguments after a space.
+# Prevent --color by itself from consuming other arguments
+foreach (@ARGV) {
+ if ($_ eq "--color" || $_ eq "-color") {
+ $_ = "--color=$color";
+ }
+}
+
GetOptions(
'q|quiet+' => \$quiet,
'tree!' => \$tree,
@@ -89,6 +101,8 @@ GetOptions(
'debug=s' => \%debug,
'test-only=s' => \$tst_only,
+ 'color=s' => \$color,
+ 'no-color' => sub { $color = 'never'; },
'h|help' => \$help,
'version' => \$help
) or help(1);
@@ -144,6 +158,16 @@ if (!$chk_patch && !$chk_branch && !$file) {
die "One of --file, --branch, --patch is required\n";
}
+if ($color =~ /^always$/i) {
+ $color = 1;
+} elsif ($color =~ /^never$/i) {
+ $color = 0;
+} elsif ($color =~ /^auto$/i) {
+ $color = (-t STDOUT);
+} else {
+ die "Invalid color mode: $color\n";
+}
+
my $dbg_values = 0;
my $dbg_possible = 0;
my $dbg_type = 0;
@@ -371,7 +395,9 @@ if ($chk_branch) {
close($FILE);
$vname = substr($hash, 0, 12) . ' (' . $git_commits{$hash} .
')';
if ($num_patches > 1 && $quiet == 0) {
- print "$i/$num_patches Checking commit $vname\n";
+ my $prefix = "$i/$num_patches";
+ $prefix = BLUE . BOLD . $prefix . RESET if $color;
+ print "$prefix Checking commit $vname\n";
$vname = "Patch $i/$num_patches";
} else {
$vname = "Commit " . $vname;
@@ -1181,14 +1207,23 @@ sub possible {
my $prefix = '';
sub report {
- if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
+ my ($level, $msg) = @_;
+ if (defined $tst_only && $msg !~ /\Q$tst_only\E/) {
return 0;
}
- my $line = $prefix . $_[0];
- $line = (split('\n', $line))[0] . "\n" if ($terse);
+ my $output = '';
+ $output .= BOLD if $color;
+ $output .= $prefix;
+ $output .= RED if $color && $level eq 'ERROR';
+ $output .= MAGENTA if $color && $level eq 'WARNING';
+ $output .= $level . ':';
+ $output .= RESET if $color;
+ $output .= ' ' . $msg . "\n";
+
+ $output = (split('\n', $output))[0] . "\n" if ($terse);
- push(our @report, $line);
+ push(our @report, $output);
return 1;
}
@@ -1196,13 +1231,13 @@ sub report_dump {
our @report;
}
sub ERROR {
- if (report("ERROR: $_[0]\n")) {
+ if (report("ERROR", $_[0])) {
our $clean = 0;
our $cnt_error++;
}
}
sub WARN {
- if (report("WARNING: $_[0]\n")) {
+ if (report("WARNING", $_[0])) {
our $clean = 0;
our $cnt_warn++;
}
--
2.20.1
- [Qemu-devel] [PULL 00/35] Misc patches for 2018-12-18, Paolo Bonzini, 2018/12/17
- [Qemu-devel] [PULL 01/35] accel: Improve selection of the default accelerator, Paolo Bonzini, 2018/12/17
- [Qemu-devel] [PULL 02/35] vhost-user-bridge: fix "unknown type name" compilation error, Paolo Bonzini, 2018/12/17
- [Qemu-devel] [PULL 03/35] checkpatch: fix premature exit when no input or --mailback, Paolo Bonzini, 2018/12/17
- [Qemu-devel] [PULL 05/35] checkpatch: improve handling of multiple patches or files, Paolo Bonzini, 2018/12/17
- [Qemu-devel] [PULL 04/35] checkpatch: check Signed-off-by in --mailback mode, Paolo Bonzini, 2018/12/17
- [Qemu-devel] [PULL 06/35] checkpatch: colorize output to terminal,
Paolo Bonzini <=
- [Qemu-devel] [PULL 07/35] pam: wrap MemoryRegion initialization in a transaction, Paolo Bonzini, 2018/12/17
- [Qemu-devel] [PULL 08/35] memory: extract flat_range_coalesced_io_{del, add}, Paolo Bonzini, 2018/12/17
- [Qemu-devel] [PULL 09/35] memory: avoid unnecessary coalesced_io_del operations, Paolo Bonzini, 2018/12/17
- [Qemu-devel] [PULL 10/35] memory: update coalesced_range on transaction_commit, Paolo Bonzini, 2018/12/17
- [Qemu-devel] [PULL 11/35] hax: Support for Linux hosts, Paolo Bonzini, 2018/12/17
- [Qemu-devel] [PULL 13/35] block/iscsi: take iscsilun->mutex in iscsi_timed_check_events(), Paolo Bonzini, 2018/12/17
- [Qemu-devel] [PULL 12/35] block/iscsi: drop unused IscsiAIOCB->buf field, Paolo Bonzini, 2018/12/17
- [Qemu-devel] [PULL 15/35] block/iscsi: cancel libiscsi task when ABORT TASK TMF completes, Paolo Bonzini, 2018/12/17
- [Qemu-devel] [PULL 16/35] esp-pci: Fix status register write erase control, Paolo Bonzini, 2018/12/17
- [Qemu-devel] [PULL 14/35] block/iscsi: fix ioctl cancel use-after-free, Paolo Bonzini, 2018/12/17