| 1 | #======================================================================== | ||
| 2 | # | ||
| 3 | # Badger::Rainbow | ||
| 4 | # | ||
| 5 | # DESCRIPTION | ||
| 6 | # Colour-related functionality, used primarily for debugging. | ||
| 7 | # | ||
| 8 | # AUTHOR | ||
| 9 | # Andy Wardley <abw@wardley.org> | ||
| 10 | # | ||
| 11 | #======================================================================== | ||
| 12 | |||
| 13 | package Badger::Rainbow; | ||
| 14 | |||
| 15 | use Carp; | ||
| 16 | #use Badger::Debug ':debug'; | ||
| 17 | use Badger::Class | ||
| 18 | version => 0.01, | ||
| 19 | base => 'Badger::Exporter', | ||
| 20 | constants => 'DELIMITER ARRAY REFS PKG ALL', | ||
| 21 | exports => { | ||
| 22 | any => 'ANSI_escape ANSI_colours strip_ANSI_escapes', | ||
| 23 | hooks => { | ||
| 24 | ANSI => \&_export_ANSI_colours, | ||
| 25 | }, | ||
| 26 | }, | ||
| 27 | constant => { | ||
| 28 | ANSI_colours => { | ||
| 29 | bold => 1, | ||
| 30 | dark => 2, | ||
| 31 | black => 30, | ||
| 32 | red => 31, | ||
| 33 | green => 32, | ||
| 34 | yellow => 33, | ||
| 35 | blue => 34, | ||
| 36 | magenta => 35, | ||
| 37 | cyan => 36, | ||
| 38 | grey => 37, | ||
| 39 | white => 38, | ||
| 40 | }, | ||
| 41 | }; | ||
| 42 | |||
| 43 | # can't import this via Badger::Debug as it depends on Badger::Rainbow | ||
| 44 | our $DEBUG = 0 unless defined $DEBUG; | ||
| 45 | |||
| 46 | sub _export_ANSI_colours { | ||
| 47 | my ($class, $target, $symbol, $more_symbols) = @_; | ||
| 48 | my $ansi = ANSI_colours; | ||
| 49 | my $cols = shift(@$more_symbols) | ||
| 50 | || croak "You didn't specify any ANSI colours to import"; | ||
| 51 | |||
| 52 | no strict REFS; | ||
| 53 | |||
| 54 | $cols = [ keys %$ansi ] | ||
| 55 | if $cols eq ALL; | ||
| 56 | |||
| 57 | $cols = [ split(DELIMITER, $cols) ] | ||
| 58 | unless ref $cols eq ARRAY; | ||
| 59 | |||
| 60 | foreach my $col (@$cols) { | ||
| 61 | # $class->debug("Exporting ANSI clolour $col to $target\n") if $DEBUG; | ||
| 62 | my $val = $ansi->{ $col } | ||
| 63 | || croak "Invalid ANSI colour specified to import: $col"; | ||
| 64 | *{ $target.PKG.$col } = sub(@) { | ||
| 65 | ANSI_escape($val, @_) | ||
| 66 | }; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | #----------------------------------------------------------------------- | ||
| 72 | # ANSI_escape($attr, $text) | ||
| 73 | # | ||
| 74 | # Adds ANSI escape codes to each line of text to colour output. | ||
| 75 | #----------------------------------------------------------------------- | ||
| 76 | |||
| 77 | sub ANSI_escape { | ||
| 78 | my $attr = shift; | ||
| 79 | my $text = join('', grep { defined $_ } @_); | ||
| 80 | return join("\n", | ||
| 81 | map { | ||
| 82 | # look for an existing escape start sequence and add new | ||
| 83 | # attribute to it, otherwise add escape start/end sequences | ||
| 84 | s/ \e \[ ([1-9][\d;]*) m/\e[$1;${attr}m/gx | ||
| 85 | ? $_ | ||
| 86 | : "\e[${attr}m" . $_ . "\e[0m"; | ||
| 87 | } | ||
| 88 | split(/\n/, $text, -1) # -1 prevents it from ignoring trailing fields | ||
| 89 | ); | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | sub strip_ANSI_escapes { | ||
| 94 | my $text = join('', grep { defined $_ } @_); | ||
| 95 | $text =~ s/ \e .*? m//gx; | ||
| 96 | return $text; | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | |||
| 102 | 1; | ||
| 103 | |||
| 104 | __END__ |