monotone

monotone Mtn Source Tree

Root/contrib/colortone.pl

1#!/usr/bin/env perl
2use strict;
3use warnings;
4
5# colortone.pl - colors monotone output such as status, log and diff
6#
7# Reads input from STDIN and adds ANSI escape codes before outputting
8# to STDOUT. Passing filenames on the command line is also supported.
9#
10# Windows systems need Win32::Console::ANSI installed to support showing
11# color within cmd.exe.
12#
13# Usage
14# mtn diff | colortone.pl
15# mtn diff > diff.txt && colortone.pl diff.txt
16#
17# Windows Usage
18# To make it easier to use, create a new file 'myname.bat' with the
19# following contents (keeping quotes).
20#
21# @"C:\Program Files\monotone\mtn.exe" %* | perl "C:\Full\Path\To\colortone.pl"
22#
23# This allows you to use 'myname' as a replacement for mtn and output will
24# be colored automatically. Only use it if your key passphrase is not
25# required: diff, status, and log are fine.
26#
27# The piping has to be done through perl manually due to
28# http://support.microsoft.com/kb/321788.
29#
30# Copyright 2011 Richard Hopkins
31#
32# This program is free software: you can redistribute it and/or modify
33# it under the terms of the GNU General Public License as published by
34# the Free Software Foundation, either version 2 of the License, or
35# (at your option) any later version.
36#
37# This program is distributed in the hope that it will be useful,
38# but WITHOUT ANY WARRANTY; without even the implied warranty of
39# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40# GNU General Public License for more details.
41#
42# You should have received a copy of the GNU General Public License
43# along with this program. If not, see <http://www.gnu.org/licenses/>.
44
45use Term::ANSIColor;
46
47if ( $^O eq 'MSWin32' ) {
48 require Win32::Console::ANSI;
49 import Win32::Console::ANSI;
50}
51
52# pattern => attributes
53#
54# pattern should contain a grouping as only the group will be colored.
55#
56# attributes is a single string containing any attributes supported by
57# Term::ANSIColor separated with a space.
58# eg. 'red on_white bold'
59my %patterns = (
60 q/(^#.*)/ => 'yellow', # diff header
61 q/(^@@.*)/ => 'magenta', # diff encloser
62 q/(^\+.*)/ => 'green', # diff added
63 q/(^\|\s+\+.*)/ => 'green', # diff added (with graph)
64 q/(^-.*)/ => 'red', # diff removed
65 q/(^\|\s+-.*)/ => 'red', # diff removed (with graph)
66 q/([a-f0-9]{40})/ => 'magenta bold', # revision or file id
67 q/(^\| +\w+:)/ => 'white bold', # log header (with graph)
68 q/(^\w+:)/ => 'white bold', # status header
69 q/(^\*\*\*.+\*\*\*$)/ => 'yellow bold', # status warning
70 q/( added)/ => 'green', # status added
71 q/( patched)/ => 'cyan bold', # status patched
72 q/( dropped)/ => 'red', # status dropped
73 q/( renamed)/ => 'blue bold', # status renamed
74);
75
76my $reset = color('reset');
77
78while (<>) {
79
80 # colorize any matching patterns in the current line.
81 foreach my $pattern ( keys %patterns ) {
82 my $color = color( $patterns{$pattern} );
83 $_ =~ s/$pattern/$color$1$reset/;
84 }
85
86 print colored $_;
87}
88
89exit(0);

Archive Download this file

Branches

Tags

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