summaryrefslogtreecommitdiffstats
path: root/scripts/colorcvs
blob: 41c7716d5fc518d60bcd28e59958aa55e9a2fc35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#! /usr/bin/env perl

# colorcvs
#
# based on colorgcc
#
# Requires the ANSIColor module from CPAN.
#
# Usage:
#
# In a directory that occurs in your PATH _before_ the directory
# where cvs lives, create a softlink to colorcvs:
#
#    cvs -> colorcvs
#
# That's it. When "cvs" is invoked, colorcvs is run instead.
#
# The default settings can be overridden with ~/.colorcvsrc.
# See the colorcvsrc-sample for more information.
#
# Note:
#
# colorcvs will only emit color codes if:
# 
#    (1) tts STDOUT is a tty.
#    (2) the value of $TERM is not listed in the "nocolor" option.
#    (3) the cvs command is not a commit or import (as the text editor
#    opened by cvs will often be hampered by colorcvs).
#
# If colorcvs colorizes the output, cvs's STDERR will be
# combined with STDOUT. Otherwise, colorcvs just passes the output from
# cvs through without modification.
#
# Copyright 2002 Neil Stevens <neil@qualityassistant.com>
#
# Copyright 1999 Jamie Moyers <jmoyers@geeks.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

use Term::ANSIColor;
use IPC::Open3;

sub initDefaults
{
	$cvsPath = "/usr/bin/cvs";

	$nocolor{"dumb"} = "true";

	$colors{"P"} = color("reset");
	$colors{"U"} = color("reset");
	$colors{"C"} = color("bold red");
	$colors{"M"} = color("bold yellow");
	$colors{"A"} = color("cyan");
	$colors{"R"} = color("cyan");
	$colors{"?"} = color("bold");
	$colors{"server"} = color("bold green");
	$colors{"warning"} = color("bold cyan");
}

sub loadPreferences
{
# Usage: loadPreferences("filename");

	my($filename) = @_;

	open(PREFS, "<$filename") || return;

	while(<PREFS>)
	{
		next if (m/^\#.*/);          # It's a comment.
		next if (!m/(.*):\s*(.*)/);  # It's not of the form "foo: bar".

		$option = $1;
		$value = $2;

		if ($option =~ /cvs/)
		{
			$cvsPath = $value;
		}
		elsif ($option eq "nocolor")
		{
			# The nocolor option lists terminal types, separated by
			# spaces, not to do color on.
			foreach $termtype (split(/\s+/, $value))
			{
				$nocolor{$termtype} = "true";
			}
		}
		else
		{
			$colors{$option} = color($value);
		}
	}
	close(PREFS);
}

#
# Main program
#

# Set up default values for colors and cvs path.
initDefaults();

# Read the configuration file, if there is one.
$configFile = $ENV{"HOME"} . "/.colorcvsrc";
if (-f $configFile)
{
	loadPreferences($configFile);
}

# Get the terminal type. 
$terminal = $ENV{"TERM"} || "dumb";

$commit = 0;
foreach (@ARGV)
{
	if(/^ci$/ || /^commit$/ || /^import$/)
	{
		$commit = 1;
	}
}

# If it's in the list of terminal types not to color, or if
# we're writing to something that's not a tty, don't do color.
if (! -t STDOUT || $commit == 1 || $nocolor{$terminal})
{
	exec $cvsPath, @ARGV
		or die("Couldn't exec");
}

# Keep the pid of the cvs process so we can get its return
# code and use that as our return code.
$cvs_pid = open3('<&STDIN', \*CVSOUT, \*CVSOUT, $cvsPath, @ARGV);
$cvsName = $cvsPath;
$cvsName =~ s,.*/(.*)$,\1,;

# Colorize the output from the cvs program.
while(<CVSOUT>)
{
	chomp;
	if (m/^(.) .+/) # S filename
	{
		print($colors{$1}, $_, color("reset"));
	}
	elsif (m/warning:/) # warning
	{
		print($colors{"warning"}, $_, color("reset"));
	}
	elsif (m/^$cvsName[^:]*: / || m/^cvs server: /) # server message
	{
		print($colors{"server"}, $_, color("reset"));
	}
	else # Anything else
	{
		# Print normally.
		print(color("reset"), $_);
	}
	print "\n";
}

# Get the return code of the cvs program and exit with that.
waitpid($cvs_pid, 0);
exit ($? >> 8);