monotone

monotone Mtn Source Tree

Root/contrib/colorize

  • Property mtn:execute set to true
1#!/usr/bin/perl
2use strict;
3
4our $VERSION = 0.5;
5
6# $Id: colorize,v 1.5 2004/07/19 14:50:38 cbouvi Exp $
7#
8# Copyright (C) 2004 Cédric Bouvier
9#
10# This program is free software; you can redistribute it and/or modify it
11# under the terms of the GNU General Public License as published by the Free
12# Software Foundation; either version 2 of the License, or (at your option)
13# any later version.
14#
15# This program is distributed in the hope that it will be useful, but WITHOUT
16# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18# more details.
19#
20# You should have received a copy of the GNU General Public License along with
21# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22# Place, Suite 330, Boston, MA 02111-1307 USA
23
24# $Log: colorize,v $
25# Revision 1.5 2004/07/19 14:50:38 cbouvi
26# Updated the POD
27#
28# Revision 1.4 2004/07/19 14:19:25 cbouvi
29# Added ability to store patterns in a config file, called with --config
30#
31# Revision 1.3 2004/07/16 08:55:52 cbouvi
32# Move the test of $opt{pattern} outside the main loop.
33#
34# Revision 1.2 2004/07/15 15:58:26 cbouvi
35# Introduced "use strict;".
36# Now uses Getopt::Long instead of -s
37#
38
39sub read_conf {
40
41 my $filename = shift;
42 open my $fh, "<$filename" or die "Cannot read $filename: $!\n";
43 my @pattern;
44 while ( <$fh> ) {
45 chomp;
46 next if /^\s*(?:#.*)?$/;
47 my ($delim) = /(\S)/;
48 $delim = quotemeta $delim;
49 eval "/^\\s*$delim((?:\\\\.|[^\\\\$delim])*?)$delim\\s+(.*)\$/ and push \@pattern, [\$1, \$2]";
50 }
51 return @pattern;
52}
53
54use Getopt::Long;
55use Pod::Usage;
56
57my %opt;
58GetOptions \%opt, qw/ help|h version|v tail|tail-f|t pattern|p config|f=s /
59 or pod2usage -verbose => 0, -message => "Try $0 --help";
60
61$opt{help} and pod2usage -verbose => 1;
62if ( $opt{version} ) {
63 print "colorize version $VERSION\n";
64 exit 0;
65}
66
67require Term::ANSIColor;
68if ( $^O eq 'MSWin32' ) {
69 require Win32::Console::ANSI;
70 import Win32::Console::ANSI;
71}
72
73my @pattern = (
74 [ q/(?i)\bERROR\b/, 'bold red' ],
75 [ q/(?i)\bWARNING\b/, 'bold yellow' ],
76 [ q/(?i)\bINFO\b/, 'bold green' ],
77 [ q/(?i)\bDEBUG\b/, 'bold cyan' ],
78);
79
80@pattern = read_conf($opt{config}) if $opt{config};
81
82foreach ( @pattern ) {
83 $$_[0] = qr/$$_[0]/;
84 $$_[1] = Term::ANSIColor::color($$_[1]);
85}
86
87my $reset = Term::ANSIColor::color('reset');
88
89if ( $opt{tail} ) {
90 open STDIN, "tail -f $ARGV[0] |" or die "Cannot run tail -f $ARGV[0]: $!";
91 @ARGV = ();
92}
93
94my $color_cref = $opt{pattern}
95 ? sub { $` . $_[0] . $& . $reset . $' }
96 : sub { $_[0] . $_ . $reset };
97
98while ( <> ) {
99 chomp;
100 foreach my $pat ( @pattern ) {
101 next unless /$$pat[0]/;
102 $_ = $color_cref->($$pat[1]);
103 last;
104 }
105}
106continue {
107 print "$_\n";
108}
109
110=head1 NAME
111
112colorize - adds ANSI escape sequences to colorize lines in a file
113
114=head1 SYNOPSIS
115
116 colorize [-p] [-f config] file1 file2 file3 ...
117 colorize [-f config] -t file
118
119=head1 DESCRIPTION
120
121This very simple program reads lines from text files and prints them out to the
122standard output. It tries to match each line against a series of regular
123expressions, and if one of them matches, the line is displayed in color.
124
125When no file names are given on the command line, the standard input is read
126instead.
127
128=head2 Configuring Patterns and Colors
129
130The configuration file is given by means of the C<--config> command-line
131switch.
132
133Blank lines and comment lines are ignored. Comments must stand out on a line by
134themselves and start with a hash sign (C<#>).
135
136Each line that is not blank nor a comment should contain a regular expression
137and a list of color and/or attribute keywords, separated by whitespace. If a
138line of the input file matches the regular expression, the line will be
139rendered in the color defined by the keywords. See L<Term::ANSIColor> for the
140full list of available keywords.
141
142The regular expression can be any Perl 5 compatible regular expression. It must
143be enclosed in slashes C<//>.
144
145Example (this is the built-in default):
146
147 /(?i)\bERROR\b/ bold red
148 /(?i)\bWARNING\b/ bold yellow
149 /(?i)\bINFO\b/ bold green
150 /(?i)\bDEBUG\b/ bold cyan
151
152=head2 Options
153
154=over 4
155
156=item B<-f> I<FILE>, B<--config>=I<FILE>
157
158Use I<FILE> as the configuration file.
159
160=item B<-p>, B<--pattern>
161
162Only colorizes the portion of the line that matches the pattern, not the whole
163line.
164
165=item B<-t>, B<--tail>, B<--tail-f>
166
167Runs the input file through C<tail -f>, so that C<colorize> does not stop at
168the end of the file, but keeps on waiting for new lines to arrive. There must
169be only one input file in that case, all the others will be ignored.
170
171=item B<-v>, B<--version>
172
173Prints the version and exits
174
175=item B<-h>, B<--help>
176
177Prints help message and exits.
178
179=end
180
181=head2 Note for Microsoft® Windows® users
182
183The "Dos Prompt" (F<cmd.exe>) does not support the ANSI terminal escape
184sequence. This program thus requires the module C<Win32::Console::ANSI> if it
185detects that it is running on such an OS.
186
187=head1 SEE ALSO
188
189perlre(1), L<Term::ANSIColor>, L<Win32::Console::ANSI>
190
191=head1 AUTHOR
192
193Copyright © 2004
194
195Cédric Bouvier <cbouvi@cpan.org>
196
197=cut

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status