File Coverage

blib/lib/autobox/Colors.pm
Criterion Covered Total %
statement 20 27 74.0
branch 0 2 0.0
condition n/a
subroutine 9 10 90.0
pod n/a
total 29 39 74.3


line stmt bran cond sub pod time code
1             #
2             # This file is part of autobox-Colors
3             #
4             # This software is copyright (c) 2013 by Chris Weyl.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9             package autobox::Colors;
10             BEGIN {
11 1     1   22967 $autobox::Colors::AUTHORITY = 'cpan:RSRCHBOY';
12             }
13             {
14             $autobox::Colors::VERSION = '0.001';
15             }
16             # git description: 107a2d1
17              
18              
19             # ABSTRACT: Simple string coloring via autobox
20              
21 1     1   10 use strict;
  1         2  
  1         40  
22 1     1   7 use warnings;
  1         1  
  1         31  
23              
24 1     1   1033 use parent 'autobox';
  1         342  
  1         6  
25              
26             sub import {
27 1     1   10 my $class = shift @_;
28              
29 1         8 $class->SUPER::import(
30             STRING => 'autobox::Colors::STRING',
31             @_,
32             );
33             }
34              
35             my %effects = (
36             'bold' => 1,
37             'dark' => 2,
38             'faint' => 2,
39             'underline' => 4,
40             'underscore' => 4,
41             'blink' => 5,
42             'reverse' => 7,
43             'concealed' => 8,
44             );
45              
46             my %colors = (
47             'black' => 30,
48             'red' => 31,
49             'green' => 32,
50             'yellow' => 33,
51             'blue' => 34,
52             'magenta' => 35,
53             'cyan' => 36,
54             'white' => 37,
55              
56             'bright_black' => 90,
57             'bright_red' => 91,
58             'bright_green' => 92,
59             'bright_yellow' => 93,
60             'bright_blue' => 94,
61             'bright_magenta' => 95,
62             'bright_cyan' => 96,
63             'bright_white' => 97,
64             );
65              
66             my %grounds = (
67             'on_black' => 40,
68             'on_red' => 41,
69             'on_green' => 42,
70             'on_yellow' => 43,
71             'on_blue' => 44,
72             'on_magenta' => 45,
73             'on_cyan' => 46,
74             'on_white' => 47,
75              
76             'on_bright_black' => 100,
77             'on_bright_red' => 101,
78             'on_bright_green' => 102,
79             'on_bright_yellow' => 103,
80             'on_bright_blue' => 104,
81             'on_bright_magenta' => 105,
82             'on_bright_cyan' => 106,
83             'on_bright_white' => 107,
84             );
85              
86             my %all = (%effects, %colors, %grounds);
87             my @colors_keys = keys %colors;
88             my $colors_num = scalar @colors_keys;
89              
90             {
91             package autobox::Colors::STRING;
92             BEGIN {
93 1     1   11445 $autobox::Colors::STRING::AUTHORITY = 'cpan:RSRCHBOY';
94             }
95             {
96             $autobox::Colors::STRING::VERSION = '0.001';
97             }
98             # git description: 107a2d1
99              
100             while ( my ($color, $code) = each %all ) {
101 1     1   11 no strict 'refs';
  1         3  
  1         268  
102 1     1   21 *{__PACKAGE__ . '::' . $color} = sub { "\e[${code}m$_[0]\e[0m" };
103             }
104              
105 1     1   10 sub decolorize { local $_ = $_[0]; s/\e\[\d+m//g; $_ }
  1         15  
  1         5  
106              
107             sub rainbow {
108 0     0     my @chars = split //, shift;
109 0           my @colored;
110              
111 0           for my $char (@chars) {
112 0           my $code = $colors{ $colors_keys[ int rand($colors_num) ] };
113             # other than spaces
114 0 0         $code = 0 if $char =~ /\s/;
115 0           push(@colored, "\e[${code}m$char\e[0m");
116             }
117 0           return join '', @colored;
118             }
119             }
120              
121             !!42;
122              
123             __END__