Skip to content

Instantly share code, notes, and snippets.

@kimmel
Created September 13, 2011 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimmel/1215251 to your computer and use it in GitHub Desktop.
Save kimmel/1215251 to your computer and use it in GitHub Desktop.
colodiff 1.0.9 parse config file. race condition/input kludge. If you turn banner or color_patches off in the first colordiffrc and then turn them back on in the 2nd file, the parser fails to set the variables to yes.
#!/usr/bin/perl
use warnings;
use strict;
my $show_banner = 1;
my $color_patch = 0;
# ANSI sequences for colours
my %colour;
$colour{white} = "\033[1;37m";
$colour{yellow} = "\033[1;33m";
$colour{green} = "\033[1;32m";
$colour{blue} = "\033[1;34m";
$colour{cyan} = "\033[1;36m";
$colour{red} = "\033[1;31m";
$colour{magenta} = "\033[1;35m";
$colour{black} = "\033[1;30m";
$colour{darkwhite} = "\033[0;37m";
$colour{darkyellow} = "\033[0;33m";
$colour{darkgreen} = "\033[0;32m";
$colour{darkblue} = "\033[0;34m";
$colour{darkcyan} = "\033[0;36m";
$colour{darkred} = "\033[0;31m";
$colour{darkmagenta} = "\033[0;35m";
$colour{darkblack} = "\033[0;30m";
$colour{off} = "\033[0;0m";
# Default colours if /etc/colordiffrc or ~/.colordiffrc do not exist
my $plain_text = $colour{white};
my $file_old = $colour{red};
my $file_new = $colour{blue};
my $diff_stuff = $colour{magenta};
my $cvs_stuff = $colour{green};
print "Show banner default: $show_banner\n";
use Data::Dumper::Names;
#print Dumper(\@break_input);
# Locations for personal and system-wide colour configurations
my $HOME = $ENV{HOME};
my $etcdir = '/etc';
my ( $setting, $value );
my @config_files = ("$etcdir/colordiffrc");
#push( @config_files, "$ENV{HOME}/.colordiffrc" ) if ( defined $ENV{HOME} );
push( @config_files, "./colordiffrc" );
my $config_file;
foreach $config_file (@config_files) {
if ( open( COLORDIFFRC, "<$config_file" ) ) {
while (<COLORDIFFRC>) {
my $colourval;
chop;
next if ( /^#/ || /^$/ );
s/\s+//g;
( $setting, $value ) = split('=');
print "setting: $setting $value\n";
if ( !defined $value ) {
print STDERR
"Invalid configuration line ($_) in $config_file\n";
next;
}
if ( $setting eq 'banner' ) {
if ( $value eq 'no' ) {
$show_banner = 0;
}
next;
}
if ( $setting eq 'color_patches' ) {
if ( $value eq 'yes' ) {
$color_patch = 1;
}
next;
}
$setting =~ tr/A-Z/a-z/;
$value =~ tr/A-Z/a-z/;
if ( ( $value eq 'normal' ) || ( $value eq 'none' ) ) {
$value = 'off';
}
if ( $value =~ m/[0-9]+/ && $value >= 0 && $value <= 255 ) {
# Numeric color
if ( $value < 8 ) {
$colourval = "\033[0;3${value}m";
}
elsif ( $value < 15 ) {
$colourval = "\033[0;9${value}m";
}
else {
$colourval = "\033[0;38;5;${value}m";
}
}
elsif ( defined( $colour{$value} ) ) {
$colourval = $colour{$value};
}
else {
print STDERR
"Invalid colour specification for setting $setting ($value) in $config_file\n";
next;
}
if ( $setting eq 'plain' ) {
$plain_text = $colourval;
}
elsif ( $setting eq 'oldtext' ) {
$file_old = $colourval;
}
elsif ( $setting eq 'newtext' ) {
$file_new = $colourval;
}
elsif ( $setting eq 'diffstuff' ) {
$diff_stuff = $colourval;
}
elsif ( $setting eq 'cvsstuff' ) {
$cvs_stuff = $colourval;
}
else {
print STDERR "Unknown option in $config_file: $setting\n";
}
}
close COLORDIFFRC;
}
}
print "show banner $show_banner\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment