package WarnColor;

use warnings;
use strict;

sub BEGIN {

	sub port2color {
		my $port = shift;
		return "\e[1m0\e[0m" if $port == 0;

		my $c = ( $port % 6 ) + 31;
		return "\e[${c}m$port\e[0m";
	}

	$SIG{__WARN__} = sub {
		return unless @_;
		my $msg = join('', @_);
		if ( $msg =~ s{ line (\d+)\.}{ +$1} ) {
			$msg =~ s{^(.+)( at .+)}{\e[31m$1\e[0m$2} if $msg !~ m{^#};
		}
		$msg =~ s{\[(\d+)\]}{ '[' . port2color($1) . ']' }eg;
		print STDERR $msg unless $msg =~ m{^#} && ! $ENV{DEBUG};
		return 1;
	};

}

1;
