#!/usr/bin/perl # This is an example of a script that filters all of its output, and colorizes # things that match specific patterns. use strict; use warnings; enable_color_filter(); # fork off a process that will filter our output # .... # .... # .... # .... # (normal script goes here... it should print some stuff out) # .... # .... # .... # .... ################################################################################ ####[ colorize all STDOUT ]##################################################### ################################################################################ our $filter_pid; sub enable_color_filter { if (($filter_pid = open(FILTER_STDOUT, '|-')) == 0) { colorize(); exit; } select FILTER_STDOUT; } END { if ($filter_pid) { close FILTER_STDOUT; waitpid($filter_pid, 0); } } use Term::ANSIColor; sub colorize { # for the col256() color-codes, see http://paperlined.org/apps/syntax_highlighting/256_color_mode.html while () { #col( qr/ExecuterDistributeSW/, 'black on_red' ); col256( qr/ExecuterDistributeSW/, 16, 31); col256( qr/4945 State:.*/, 16, 34); print; } } sub col { my ($pattern, $color) = @_; $_ =~ s/($pattern)/Term::ANSIColor::colored($1, $color)/eg; } sub col256 { my ($pattern, $fgcolor, $bgcolor) = @_; #$_ =~ s/($pattern)/fg256($fgcolor) . bg256($bgcolor) . $1 . fg256(255) . bg256(16)/eg; $_ =~ s/($pattern)/fg256($fgcolor) . bg256($bgcolor) . $1 . "\e[0m"/eg; } sub fg256 {"\e[38;5;$_[0]m"} sub bg256 {"\e[48;5;$_[0]m"}